This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
131 lines (131 loc) · 3.71 KB
/
server.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
'use strict';
//
// What port should be use?
const port = process.env.PORT || 3000;
//
// Basic required libraries.
const HttpStatusCodes = require('http-status-codes');
const bodyParser = require('body-parser');
const chalk = require('chalk');
const express = require('express');
const http = require('http');
const path = require('path');
//
// Importing DRTools.
const {
ConfigsManager,
EndpointsManager,
ExpressConnector,
LoadersManager,
MiddlewaresManager,
RoutesManager,
PluginsManager,
TasksManager,
} = require('drtools');
//
// Creating an express application.
global.expressApp = express();
//
// Loading steps.
const loadingSteps = [];
//
// Loading DRTools configs.
loadingSteps.push(async () => {
global.configs = new ConfigsManager(path.join(__dirname, 'configs'));
global.env = global.configs.get('environment');
global.envName = global.configs.environmentName();
global.expressApp.use(global.configs.publishExports());
});
//
// Setting up a logs manager.
loadingSteps.push(async () => {
require('./includes/logger');
});
//
// Loading parser.
loadingSteps.push(async () => {
global.expressApp.use(bodyParser.json());
global.expressApp.use(bodyParser.urlencoded({ extended: false }));
});
//
// Loading DRTools ExpressJS connector.
loadingSteps.push(async () => {
ExpressConnector.attach(global.expressApp, {
webUi: global.configs.get('environment').drtools.webUi,
});
});
//
// Loading DRTools loaders.
loadingSteps.push(async () => {
const manager = new LoadersManager(path.join(__dirname, 'includes/loaders'), {}, global.configs);
await manager.load();
});
//
// Loading DRTools middlewares.
loadingSteps.push(async () => {
const manager = new MiddlewaresManager(global.expressApp, path.join(__dirname, 'includes/middlewares'), {}, global.configs);
await manager.load();
});
//
// Loading DRTools plugins.
loadingSteps.push(async () => {
const manager = new PluginsManager(path.join(__dirname, 'plugins'), {}, global.configs);
await manager.load();
});
//
// Loading DRTools routes.
loadingSteps.push(async () => {
const manager = new RoutesManager(global.expressApp, path.join(__dirname, 'includes/routes'), {}, global.configs);
await manager.load();
});
//
// Loading DRTools tasks.
loadingSteps.push(async () => {
const manager = new TasksManager(path.join(__dirname, 'includes/tasks'), {}, global.configs);
await manager.load();
});
//
// Loading DRTools mock-endpoints.
loadingSteps.push(async () => {
const endpoints = new EndpointsManager({
directory: path.join(__dirname, 'includes/mock-endpoints'),
uri: 'mock-api/v1.0',
}, global.configs);
global.expressApp.use(endpoints.provide());
});
//
// Setting static folders.
loadingSteps.push(async () => {
global.expressApp.use(express.static(path.join(__dirname, 'public')));
});
//
// Setting default routes.
loadingSteps.push(async () => {
global.expressApp.use(function (req, res, next) {
if (req.xhr || req.headers['accept'] === 'application/json' || req.headers['content-type'] === 'application/json') {
res.status(HttpStatusCodes.NOT_FOUND).json({
message: 'Not Found',
uri: req.url,
isAjax: req.xhr,
});
} else {
res.sendFile(path.join(__dirname, '/public/index.html'));
}
});
});
//
// Steps loaders.
(async () => {
try {
for (const step of loadingSteps) {
await step();
}
//
// Starting server.
http.createServer(global.expressApp).listen(port, () => {
console.log(`\nListening on port '${port}'...`);
});
} catch (err) {
console.error(chalk.red(err));
}
})();