-
Notifications
You must be signed in to change notification settings - Fork 17
/
dispatcherRouter.js
118 lines (105 loc) · 3.91 KB
/
dispatcherRouter.js
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
'use strict';
var _ = require('lodash');
var Router = require('koa-router');
var logger = require('logger');
var request = require('request');
var DispatcherService = require('services/dispatcherService');
var ServiceNotFound = require('errors/serviceNotFound');
var NotAuthorized = require('errors/notAuthorized');
var router = new Router({});
var restCo = require('lib/restCo');
var fs = require('fs');
var unlink = function(file) {
return function(callback) {
fs.unlink(file, callback);
};
};
var ALLOWED_HEADERS = [
'access-control-allow-origin',
'access-control-allow-headers',
'cache-control',
'charset',
'location'
];
var getHeadersFromResponse = function(response) {
var validHeaders = {};
_.each(response.headers, function(value, key) {
if (ALLOWED_HEADERS.indexOf(key.toLowerCase()) > -1) {
validHeaders[key] = value;
}
});
return validHeaders;
};
class DispatcherRouter {
static * dispatch() {
logger.info('Dispatch url', this.request.url, ' and method ', this.request.method);
let requestConfig = null;
try {
requestConfig = yield DispatcherService.getRequest(this.request.path, this.request.method, this.request.body, this.request.headers, this.request.search, this.request.body.files, (this.req.user || this.req.microservice || this.state.user));
} catch (e) {
logger.error(e);
if (e instanceof ServiceNotFound) {
logger.debug('Service not found');
this.throw(404, 'Endpoint not found');
return;
} if (e instanceof NotAuthorized) {
logger.debug('Not authorized');
this.throw(401, e.message);
return;
} else {
this.throw(500, 'Unexpected error');
return;
}
}
try {
logger.debug('Send request', requestConfig);
if(!requestConfig.binary){
requestConfig.followRedirects = false;
let request = restCo(requestConfig);
let result = yield request;
this.set(getHeadersFromResponse(result.response));
this.status = result.response.statusCode;
this.body = result.body;
this.response.type = result.response.headers['content-type'];
} else {
logger.debug('Is binary');
this.body = request(requestConfig);
}
} catch (e) {
logger.error('Error to request', e);
if(e.errors && e.errors.length > 0 && e.errors[0].status >= 400 && e.errors[0].status <= 500 ){
this.status = e.errors[0].status;
this.body = e;
} else {
if (process.env.NODE_ENV === 'prod') {
this.throw(500, 'Unexpected error');
return;
}
let message = '';
if(e.message){
message += e.message;
}
if (e.exception) {
message += ' --- ' + e.exception;
}
this.throw(500, message);
return;
}
} finally {
if(this.request.body.files){
logger.debug('Removing files');
let files = Object.keys(this.request.body.files);
for( let i=0, length= files.length; i < length; i++){
logger.debug('Removing file %s', this.request.body.files[files[i]].path);
yield unlink(this.request.body.files[files[i]].path);
}
}
}
}
}
router.get('/*', DispatcherRouter.dispatch);
router.post('/*', DispatcherRouter.dispatch);
router.delete('/*', DispatcherRouter.dispatch);
router.put('/*', DispatcherRouter.dispatch);
router.patch('/*', DispatcherRouter.dispatch);
module.exports = router;