This repository was archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (57 loc) · 1.72 KB
/
index.js
File metadata and controls
73 lines (57 loc) · 1.72 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
// npm packages
const initFastify = require('fastify');
const fastifyAuth = require('fastify-auth');
const cors = require('cors');
const {dbLoaded} = require('./db');
// our packages
const logger = require('./logger');
// init docker service
const {initDocker} = require('./docker/init');
// init plugins system
const {initPlugins} = require('./plugins');
// config
const {getConfig, waitForConfig, faasFolder} = require('./config');
// paths
const setupAuth = require('./auth');
const routes = require('./routes');
const {setup: faas} = require('exoframe-faas');
exports.startServer = async (port = 8080) => {
// create server
const fastify = initFastify().register(fastifyAuth);
// enable cors if needed
await waitForConfig();
const config = getConfig();
if (config.cors) {
logger.warn('cors is enabled with config:', config.cors);
// if it's just true - simply enable it
if (typeof config.cors === 'boolean') {
fastify.use(cors());
} else {
// otherwise pass config object to cors
fastify.use(cors(config.cors));
}
}
// add custom parser that just passes stream on
fastify.addContentTypeParser('*', (req, done) => done());
// register plugins
await setupAuth(fastify)
.register(routes)
.register(faas({faasFolder}))
.ready();
// start server
await fastify.listen(port, '0.0.0.0');
logger.info(`Server running at: ${fastify.server.address().port}`);
return fastify;
};
// export start function
exports.start = async port => {
logger.info('Starting exoframe ...');
// load database
await dbLoaded;
// init plugins
await initPlugins();
// init required docker service
await initDocker();
// init and return server
return exports.startServer(port);
};