-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
374 lines (318 loc) · 9.22 KB
/
index.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const program = require('commander');
const fs = require('fs');
const path = require('path');
const fsutils = require('~framework/utils/fs-utils');
const serverConfig = require('~config/server.json');
const fastify = require('fastify')({
logger: true,
disableRequestLogging: !serverConfig.logging.request,
ignoreTrailingSlash: serverConfig.strictRouting,
maxParamLength: serverConfig.parsers.maxParamLength,
caseSensitive: serverConfig.parsers.caseSensitive,
bodyLimit: serverConfig.parsers.bodyLimit
});
const app = {
log: require('~framework/logging/log'),
meta: require('./package'),
port: process.env.PORT || 8000,
schemas: {},
utils: {
processRouteHandler: p =>
{
app.log.info(`trying '${p}'`);
const m = require(p);
const route = new m(fastify);
app.log.info(`registering ${route.config.url} for ${route.config.method}`);
//check if route has a path alias
if (route.config.hasOwnProperty('alias'))
{
app.log.info(`processing aliases ${route.config.alias}...`);
const aliasedOptions = Object.assign({}, route.config);
aliasedOptions.handler = route.handler;
switch (typeof route.config.alias)
{
case 'string':
aliasedOptions.url = aliasedOptions.alias;
delete aliasedOptions.alias;
fastify.route(aliasedOptions);
break;
case 'object':
if (Array.isArray(aliasedOptions.alias))
{
for (const alias of aliasedOptions.alias)
{
const eachAliasOption = Object.assign({}, aliasedOptions);
delete eachAliasOption.alias;
eachAliasOption.url = alias;
fastify.route(eachAliasOption);
}
}
break;
default:
app.log.warn(`Unknown route.config.alias type: ${typeof route.config.alias}`);
}
}
//process routes without alias
const options = route.config;
options.handler = route.handler;
fastify.route(options);
},
parseRouteStore: pathRoutes =>
{
const files = fs.readdirSync(pathRoutes, {withFileTypes: true});
//walk through all the items in the path sepcified
for (const file of files)
{
//build the absolute path
const p = path.join(pathRoutes, file.name);
if (file.isDirectory())
{
//if directory, we run the parser for all the items within the directory
app.utils.parseRouteStore(p);
}
else if (file.isFile())
{
//process router
app.utils.processRouteHandler(p);
}
}
},
loadRouteHandlers: () =>
{
const pathRoutes = path.join(__dirname, '/routes/');
if (!fs.existsSync(pathRoutes))
{
fs.mkdir(pathRoutes);
}
app.utils.parseRouteStore(pathRoutes);
},
loadSchemas: () =>
{
const schemasPath = path.join(__dirname, 'schemas');
if (fs.existsSync(schemasPath))
{
const schemasList = fs.readdirSync(schemasPath, {withFileTypes: true});
for (const schema of schemasList)
{
if (schema.isFile())
{
const schemaContent = fsutils.readFile(path.join(schemasPath, schema.name));
fastify.addSchema(JSON.parse(schemaContent));
}
}
}
}
},
init: async () =>
{
process.title = `${app.meta.name} on port ${app.port}`;
program
.version(`${app.meta.version}`, '-v, --version')
.usage('-port -path')
.option('-p, --port <port>', 'port')
.parse(process.argv);
if (!program.port)
{
app.log.info(`port is not set, will be using ${app.port} as default`);
}
else
{
app.port = program.port;
}
//if specified, allow serving static content
if (serverConfig.static && serverConfig.static.root)
{
const staticPath = path.join(__dirname, serverConfig.static.root);
if (fs.existsSync(staticPath))
{
fastify.register(require('fastify-static'), {
root: staticPath
});
}
else
{
app.log.warn(`Static content path '${serverConfig.static.root}' is not found`);
}
}
//cookies support
if (serverConfig.cookies)
{
fastify.register(require('fastify-cookie'));
app.log.info('cookies support: enabled');
}
//enable view engine
if (serverConfig.viewEngine)
{
switch (serverConfig.viewEngine.engine.toLowerCase())
{
case 'handlebars':
//preload partials
const partialsBitsPath = serverConfig.viewEngine.partialsDir || 'ui/partials';
const partialsDir = path.join(__dirname, partialsBitsPath);
const partials = {};
if (fs.existsSync(partialsDir))
{
const files = fs.readdirSync(partialsDir, {withFileTypes: true});
for (const partial of files)
{
if (partial.isFile())
{
const partialName = path.parse(partial.name).name;
app.log.info(`Found partial: ${partialName}`);
partials[partialName] = fs.readFileSync(path.join(partialsDir, partial.name)).toString();
}
}
}
fastify.render = (template, data) =>
{
const hbs = require('handlebars');
//register partials
hbs.registerPartial({
applicationName: app.meta.name,
applicationVersion: app.meta.version
});
hbs.registerPartial(partials);
hbs.registerHelper({
applicationName: app.meta.name,
applicationVersion: app.meta.version
});
//preload framework helpers
const helpersPath = path.join(__dirname, '/framework/handlebars-helpers');
if (fs.existsSync(helpersPath))
{
const helpersFiles = fs.readdirSync(helpersPath, {withFileTypes: true});
for (const helperFile of helpersFiles)
{
hbs.registerHelper(require(path.join(helpersPath, helperFile.name)));
}
}
//render whole thing
try
{
let ctx;
switch (typeof template)
{
case 'string':
ctx = hbs.compile(template, serverConfig.viewEngine.config);
break;
case 'object':
const templatesDir = serverConfig.viewEngine.templatesDir || 'ui';
const filePath = path.join(__dirname, path.join(templatesDir, template.file));
if (!fs.existsSync(filePath))
{
return new Error(`Template ${template.file} is not found`);
}
try
{
const templateContent = fs.readFileSync(filePath);
ctx = hbs.compile(templateContent.toString(), serverConfig.viewEngine.config);
}
catch (e)
{
return e;
}
break;
default:
return new Error(`Template ${template.file} type is unknown`);
}
if (ctx)
{
return ctx(data);
}
}
catch (e)
{
app.log.error(e.message);
return {error: {message: e.message}};
}
return new Error(`Template ${template.file} failed to be rendered`);
};
break;
default:
app.log.warn(`view engine ${serverConfig.viewEngine.engine} is not supported`);
break;
}
}
const corsFile = path.join(__dirname, 'config/cors.json');
if (fs.existsSync(corsFile))
{
app.log.info('Processing CORS options');
fastify.register(require('fastify-cors'), require(corsFile));
}
//handle 'not found' requests
fastify.register(async (instance, options, next) =>
{
instance.setNotFoundHandler((req, res) =>
{
res.send({
error: {
message: 'Resource not found',
url: req.raw.url
}
});
});
next();
});
fastify.setErrorHandler((error, req, res) =>
{
res.status(error.statusCode).send({error: {message: error.message}});
});
//Add accepts parser to fastify
if (serverConfig.parsers.accepts)
{
fastify.register(require('fastify-accepts'));
}
app.utils.loadSchemas();
app.utils.loadRouteHandlers();
fastify.addHook('onRequest', (req, res, next) =>
{
res.header('server', `${app.meta.name} ${app.meta.version}`);
if (Number(req.headers['upgrade-insecure-requests']) === 1 && serverConfig.upgradeInsecureRequests)
{
const secureUrl = `https://${req.hostname}${req.raw.url}`;
res.header('Vary', 'Upgrade-Insecure-Requests');
res.redirect(302, secureUrl);
}
/*
Remove the X-Powered-By header to make it slightly harder for attackers to see what potentially-vulnerable
technology powers your site.
*/
res.header('x-powered-by', app.meta.name);
/*
Helps prevent browsers from trying to guess (“sniff”) the MIME type, which can have security implications.
*/
res.header('X-Content-Type-Options', 'nosniff');
/*
The X-DNS-Prefetch-Control header tells browsers whether they should do DNS prefetching. Turning it on may
not work—not all browsers support it in all situations—but turning it off should disable it on all
supported browsers.
*/
res.header('X-DNS-Prefetch-Control', 'off');
/*
The X-Frame-Options header tells browsers to prevent your webpage from being put in an iframe. When
browsers load iframes, they’ll check the value of the X-Frame-Options header and abort loading
if it’s not allowed
*/
res.header('X-Frame-Options', 'DENY');
next();
});
fastify.ready().then(() =>
{
app.log.info('successfully booted!');
}, err =>
{
app.log.error(`ready() error: ${err.message}`);
});
try
{
await fastify.listen(app.port, '0.0.0.0');
app.log.info(`server listening on ${fastify.server.address().port}`);
}
catch (err)
{
fastify.log.error(err);
process.exit(1);
}
}
};
app.init();