-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathapp.js
More file actions
89 lines (67 loc) · 2.28 KB
/
Copy pathapp.js
File metadata and controls
89 lines (67 loc) · 2.28 KB
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
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
if (!process.env.__ALREADY_BOOTSTRAPPED_ENVS) require('dotenv').config();
const fs = require('fs');
const { createServer } = require('@app-core/server');
const { createConnection } = require('@app-core/mongoose');
const { createQueue } = require('@app-core/queue');
const canLogEndpointInformation = process.env.CAN_LOG_ENDPOINT_INFORMATION;
createConnection({
uri: process.env.MONGODB_URI,
});
createQueue();
const server = createServer({
port: process.env.PORT,
JSONLimit: '150mb',
enableCors: true,
});
const ENDPOINT_CONFIGS = [
{
path: './endpoints/onboarding/',
},
];
function logEndpointMetaData(endpointConfigs) {
const endpointData = [];
const storageDirName = './endpoint-data';
const EXEMPTED_ENDPOINTS_REGEX = /onboarding/;
endpointConfigs.forEach((endpointConfig) => {
const { path: basePath, options } = endpointConfig;
const dirs = fs.readdirSync(basePath);
dirs.forEach((file) => {
const handler = require(`${basePath}${file}`);
if (!EXEMPTED_ENDPOINTS_REGEX.test(basePath) && handler.middlewares?.length) {
const entry = { method: handler.method, endpoint: handler.path };
entry.name = file.replaceAll('-', ' ').replace('.js', '');
entry.display_name = `can ${entry.name}`;
if (options?.pathPrefix) {
entry.endpoint = `${options.pathPrefix}${entry.endpoint}`;
entry.name = `${entry.name} (${options.pathPrefix.replace('/', '')})`;
}
endpointData.push(entry);
}
});
});
if (!fs.existsSync(storageDirName)) {
fs.mkdirSync(storageDirName);
}
fs.writeFileSync(`${storageDirName}/endpoints.json`, JSON.stringify(endpointData, null, 2), {
encoding: 'utf-8',
});
}
if (canLogEndpointInformation) {
logEndpointMetaData(ENDPOINT_CONFIGS);
}
function setupEndpointHandlers(basePath, options = {}) {
const dirs = fs.readdirSync(basePath);
dirs.forEach((file) => {
const handler = require(`${basePath}${file}`);
if (options.pathPrefix) {
handler.path = `${options.pathPrefix}${handler.path}`;
}
server.addHandler(handler);
});
}
ENDPOINT_CONFIGS.forEach((config) => {
setupEndpointHandlers(config.path, config.options);
});
server.startServer();