From b424e0fb87253a827707064afc94f42aa2e2f560 Mon Sep 17 00:00:00 2001 From: Rodrigo Ramalho Date: Fri, 3 May 2019 02:21:12 -0300 Subject: [PATCH] injecting key-user header in every request to services --- README.md | 5 +++ src/app/interceptor/httpconfig.interceptor.ts | 35 +++++++++++++++++++ src/environments/environment.ts | 16 ++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/app/interceptor/httpconfig.interceptor.ts diff --git a/README.md b/README.md index 7e5589c..b78d68e 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,8 @@ Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protrac ## Further help To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). + +## API Gateway + +To every request for mission/incident and responder services a user-key header is being added to the request. +To disable it, just open environment.ts and set the **isGatewayEnabled** to false. diff --git a/src/app/interceptor/httpconfig.interceptor.ts b/src/app/interceptor/httpconfig.interceptor.ts new file mode 100644 index 0000000..2b20539 --- /dev/null +++ b/src/app/interceptor/httpconfig.interceptor.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { + HttpInterceptor, + HttpRequest, + HttpResponse, + HttpHandler, + HttpEvent, + +} from '@angular/common/http'; + +import { Observable, throwError } from 'rxjs'; +import { map, catchError } from 'rxjs/operators'; +import { environment } from '../../environments/environment'; + +@Injectable() export class HttpConfigInterceptor implements HttpInterceptor { + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + if(environment.isGatewayEnabled){ + let service = request.url.substr(0, request.url.indexOf("/")); + if (environment.servicesNames.includes(service)){ + request = request.clone({ + setHeaders: { + "user-key": environment.service.get(service) + } + }); + } + } + + return next.handle(request).pipe( + map((event: HttpEvent) => { + return event; + })); + } + +} diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 012182e..3f0c70a 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -2,8 +2,22 @@ // `ng build ---prod` replaces `environment.ts` with `environment.prod.ts`. // The list of file replacements can be found in `angular.json`. +export var servicesMap = new Map(); +servicesMap.set('incident-service', 'd3fa642d9b83043af5b92805ebdaf7d2'); +servicesMap.set('alert-service', 'xxx'), +servicesMap.set('responder-service', '884e490b9c52e6c6354b8c6e7cb8ee14'), +servicesMap.set('mission-service', '3865ce20837908ebae41c32a8ae3e829'); + export const environment = { - production: false + production: false, + isGatewayEnabled: true, + service: servicesMap, + servicesNames: [ + 'incident-service', + 'alert-service', + 'responder-service', + 'mission-service', + ] }; /*