-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server.ts
262 lines (211 loc) · 8.01 KB
/
http-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { injectable, inject } from 'inversify';
import { Request, Response, NextFunction, Express } from 'express';
import * as bodyParser from 'body-parser';
import * as httpStatus from 'http-status';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { v4 as uuid } from 'uuid';
import * as timeout from 'connect-timeout';
import { HealthManager } from './health';
import { Config } from './config';
import { HttpMethod, ServiceHandlerFunction, ServiceResponse, QueryMapping, UrlMapping } from './service';
import { Logger } from './logger';
import { Context } from './context';
import { deepSet } from './utils';
export interface RegisteredServices {
[key: string]: HttpMethod;
}
export interface HttpService {
url: string;
method: HttpMethod;
handler: ServiceHandlerFunction;
unauthenticated?: boolean;
queryMapping?: QueryMapping;
urlMapping?: UrlMapping;
}
@injectable()
export class HttpServer {
public health = new BehaviorSubject(false);
protected registeredUrls: RegisteredServices = {};
protected server: Express;
constructor(
@inject('express') private express: () => Express | Object,
private config: Config,
private logger: Logger,
healthManager: HealthManager
) {
healthManager.registerCheck('HTTP server', this.health);
// Setup express and a json body parser
this.server = <Express>(this.express());
this.server.use(bodyParser.json({
type: (request) => {
let contentType = '';
if (request.headers && request.headers['content-type']) {
if (Array.isArray(request.headers['content-type'])) {
contentType = request.headers['content-type'][0] || '';
} else {
contentType = <string>request.headers['content-type'];
}
}
return contentType.startsWith('application/json');
}
}));
// Register health check endpoint
const healthUrl = this.normalizeURL((<any>this.config)['healthCheckURL'] || '/check');
this.server.get(healthUrl, (_: Request, response: Response) => {
const report = healthManager.getReport();
if (healthManager.healthy) {
response.status(httpStatus.OK).send(report);
} else {
response.status(httpStatus.SERVICE_UNAVAILABLE).send(report);
}
});
}
// Register an endpoint with the server
public registerService(service: HttpService) {
// Normalize methodType to express method function
const method: string = (service.method || 'GET').toLowerCase();
const url = this.normalizeURL(service.url);
if (this.registeredUrls[url] && this.registeredUrls[url] === service.method) {
const error = `Trying to register url: ${url} with the same HttpMethod (${service.method}) twice`;
this.logger.fatal(error);
throw new Error(error);
}
this.logger.info(`Registering HTTP handler: ${service.method || method} ${url}`);
this.registeredUrls[url] = service.method;
(this.server as any)[method](url, (request: Request, response: Response) => {
this.handleRequest(service, request, response);
});
}
// Starts the http server
public start() {
// Set a 30 seconds request timeout
const connectTimeout: number = (<any>this.config)['connectTimeout'] || 30000;
this.server.use(timeout(`${connectTimeout}ms`));
// 404 middleware
this.server.use((request: Request, response: Response, _: NextFunction) => {
this.logger.warn(`Unknown endpoint called: ${request.url}`);
response.status(httpStatus.NOT_FOUND).send({
message: `Unknown endpoint: ${request.url}`
});
});
// Error middleware
this.server.use((error: any, request: Request, response: Response, _: NextFunction) => {
this.logger.error(`Express error middleware error for ${request.url}`, error);
console.error(error);
response.status(httpStatus.INTERNAL_SERVER_ERROR).send({
message: 'Something went terribly wrong....'
});
});
this.server.listen((<any>this.config)['httpPort'], () => {
this.logger.info(`Http server starting listening on: ${(<any>this.config)['httpPort']} `);
this.health.next(true);
});
}
private handleRequest(service: HttpService, request: Request, response: Response) {
// Build up context object
const context = this.createContext(request);
const startTime = new Date();
this.logger.info(`Http request: '${request.url}' started`, context);
if (!service.unauthenticated && !context.token) {
this.logger.audit(`Unauthenticated request on: ${service.url} `);
response.status(httpStatus.FORBIDDEN).send('Unauthenticated');
return;
}
let body = request.body || {};
// See if we need to map query string or url parameters
body = this.getQueryParams(service, request, body);
body = this.getUrlParams(service, request, body);
// Call the httpHandler
service.handler(context, body)
.then((serviceResponse: ServiceResponse | void) => {
if (!serviceResponse) {
throw new Error('Response is void, aborting');
}
const status = serviceResponse.status || httpStatus.OK;
const content = serviceResponse.content;
response.status(status).send(content);
const duration = new Date().getTime() - startTime.getTime();
this.logger.info(`Http request '${request.url}' ended: ${status}, duration: ${duration} ms`, { context });
})
.catch((error: ServiceResponse = {}) => {
this.logger.error(error.content);
const status = error.status || httpStatus.INTERNAL_SERVER_ERROR;
const content = error.content || 'Internal server error';
response.status(status).send(content);
const duration = new Date().getTime() - startTime.getTime();
this.logger.info(`Http request '${request.url}' ended: ${status}, duration: ${duration} ms`, { context });
});
}
private normalizeURL(url: string) {
// Urls need to start with a slash
if (url.charAt(0) !== '/') {
url = `/${url}`;
}
// Check for root in config and prepend to the url
if ((<any>this.config)['httpRoot']) {
let httpRoot = (<any>this.config)['httpRoot'];
// Should start with an slash
if (httpRoot.charAt(0) !== '/') {
httpRoot = `/${httpRoot}`;
}
// Should not end with an slash
if (httpRoot.charAt(httpRoot.length - 1) === '/') {
httpRoot = httpRoot.substring(0, httpRoot.length - 1);
}
url = `${httpRoot}${url}`;
}
return url;
}
private getQueryParams(service: HttpService, request: Request, body: any): any {
if (service.queryMapping) {
for (const param in service.queryMapping) {
if (service.queryMapping.hasOwnProperty(param)) {
const value = request.query[param];
const path = service.queryMapping[param];
if (value) {
deepSet(body, path, value);
}
}
}
}
return body;
}
private getUrlParams(service: HttpService, request: Request, body: any): any {
if (service.urlMapping) {
for (const param in service.urlMapping) {
if (service.urlMapping.hasOwnProperty(param)) {
const value = request.params[param];
const path = service.urlMapping[param];
if (value) {
deepSet(body, path, value);
}
}
}
}
return body;
}
private createContext(request: Request): Context {
let token;
let requestId;
let user;
if (request.headers['authorization']) {
token = request.headers['authorization'].toString().split('Token ')[1];
}
if (request.headers['x-request-id']) {
requestId = request.headers['x-request-id'].toString();
} else {
requestId = uuid();
}
if (request.headers['remoteuser']) {
user = request.headers['remoteuser'].toString();
}
// if (token === undefined || user === undefined) {
// throw new Error('Could not create context, lacking token or user');
// }
return {
token: <string>token,
requestId: requestId,
user: <string>user
}
}
}