-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
165 lines (154 loc) · 6.3 KB
/
express.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
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
const {contextForPath, registerContext, resolveBy, saver: contextSaver} = require('./context');
const {setParameters} = require("./administration");
const {logToConsole, rootLogger} = require("./logger");
const {readFile, saveFile} = require('./fsWatcher');
const {addUser, saver: usersSaver} = require('./users');
const {} = require('./api');
const pathOf = req => req._parsedUrl.pathname;
const parsePath = req => {
const {url} = req;
const qIndex = url.indexOf('?');
const endIndex = qIndex === -1 ? url.length - 1 : qIndex - 1;
const endCharacter = url.charAt(endIndex);
const newLength = endIndex > 0 && endCharacter === '/' ? endIndex : endIndex + 1;
return url.length === newLength ? url : url.substr(0, newLength);
};
const keyOf = {
POST: "onPost",
GET: "onGet",
PUT: "onPut",
DELETE: "onDelete"
};
const parseUrl = url => {
const protocolIndex = url.indexOf('://');
const protocol = protocolIndex > 0 ? url.substr(0, protocolIndex + 1).toLowerCase() : undefined;
const rest = protocolIndex >= 0 ? url.substr(protocolIndex+3) : url;
const contextIndex = rest.indexOf('/');
const host = contextIndex >= 0 ? contextIndex > 0 ? rest.substr(0, contextIndex).toLowerCase() : undefined : rest;
const context = contextIndex >= 0 ? rest.substr(contextIndex) : '/';
let wsUrl;
let webUrl;
if (host) {
const urlWithoutProtocol = `//${host}${context}`;
if (protocol) {
wsUrl = `"${protocol === 'https:' ? 'wss:' : 'ws:'}${urlWithoutProtocol}"`;
webUrl = `"${protocol}${urlWithoutProtocol}"`;
} else {
wsUrl = `(location.protocol === "https:" ? "wss:" : "ws:") +"${urlWithoutProtocol}"`;
webUrl = `location.protocol + "${urlWithoutProtocol}"`;
}
} else {
wsUrl = `(location.protocol === "https:" ? "wss:" : "ws:") + "//" + location.host + "${context}"`;
webUrl = `location.protocol + "//" + location.host + "${context}"`;
}
return {wsUrl, webUrl, host, context};
};
const administrationExpressApp = (app, url = '/administrationApi', {timeout} = {}) => {
const {wsUrl, webUrl, host, context} = parseUrl(url);
const rootDir = `${__dirname}`;
const options = {path: `${rootDir}/administrationApi`};
if (host) {
options.headers = {host};
}
if (context.length > 1) {
options.url = context;
}
setParameters({wsUrl, webUrl, host, url: context, timeout});
registerContext(options, {allowAdministration: true}).catch(e => rootLogger.error(`can not add administration context: ${e}`));
};
const extendExpressApp = async (app, options) => {
const extendedExpress = {};
let usingAdmin = false;
extendedExpress.useAdministrationApi = url => {
if (!usingAdmin) {
administrationExpressApp(app, url, options);
usingAdmin = true;
}
return extendedExpress;
};
extendedExpress.logToConsole = logToConsole;
if (options) {
const {administrationApi, contexts, consoleLogger, usersJson, contextsJson} = options;
if (administrationApi != null) {
extendedExpress.useAdministrationApi(administrationApi);
}
if (contexts && Array.isArray(contexts)) {
await Promise.all(contexts.map(async options => await registerContext(options)));
}
if (consoleLogger != null) {
logToConsole(consoleLogger);
}
if (usersJson && typeof usersJson === 'string') {
const data = await readFile(usersJson);
if (!data) {
throw new Error(`Can not start app because file '${usersJson}' from property 'usersJson' can not be read`);
}
try {
const users = JSON.parse(data);
for (const user of users) {
addUser(user);
}
usersSaver(async (users, user) => {
if (user) {
await saveFile(usersJson, JSON.stringify(users, null, 2))
}
});
} catch (e) {
throw new Error(`Can not start app because: ${e}`);
}
}
if (contextsJson && typeof contextsJson === 'string') {
const data = await readFile(contextsJson);
if (!data) {
throw new Error(`Can not start app because file '${contextsJson}' from property 'contextsJson' can not be read`);
}
const contexts = JSON.parse(data);
await Promise.all(contexts.map(async options => await registerContext(options)));
let waiter = null;
contextSaver(async (allOptions, options) => {
if (waiter) {
await waiter;
}
let onFinish = null;
waiter = new Promise(resolve => onFinish = resolve);
if (options) {
await saveFile(contextsJson, JSON.stringify(allOptions, null, 2))
}
if (onFinish) {
onFinish();
}
waiter = null;
});
}
}
app.use((req, res, next) => {
const context = contextForPath(resolveBy(req));
if (context) {
const {moduleAt} = context;
if (moduleAt) {
let pathname;
try {
pathname = pathOf(req);
} catch (e) {
pathname = parsePath(req);
}
const module = moduleAt(pathname);
if (module && !module.isPrivate) {
const method = module[keyOf[req.method]] || module.onRequest;
if (method) {
req.remoteAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
method(req, res, next);
return;
}
} else {
context.logger.debug(`unknown module for ${req.method} request '${req.url}' ${JSON.stringify(req.headers)}`);
}
}
} else {
rootLogger.debug(`unknown context for ${req.method} request '${req.url}' ${JSON.stringify(req.headers)}`);
}
next();
});
return extendedExpress;
};
module.exports = {extendExpressApp};