-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (106 loc) · 3.05 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
/**
* API test Main entry point
* connects to DB, and starts the server
*/
// check env vars for starting
if (!process.env.MONGO_URI) {
throw new Error('Env vars not set. Exiting');
}
const _env = process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'test' ? 'development' : process.env.NODE_ENV;
const fastify = require('fastify');
const { errorTypes } = require('./src/error');
// load routes
const dbModels = require('./src/db/_db');
// Simplifed package version fetch for now
const { version } = require('./package.json');
// Init logger
console.info('Service starting');
// Init server
const server = fastify(
{
caseSensitive: false,
trustProxy: true,
// add req/res logger in development
logger: _env === 'development' ? console : false,
},
);
// set development options
if (_env === 'development') {
const swaggerOption = {
routePrefix: '/swagger',
swagger: {
info: {
title: 'Test Microservice swagger',
description: 'Test microservice API details',
version,
},
// host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
},
exposeRoute: true,
};
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
server.register(require('fastify-swagger'), swaggerOption);
}
const start = async () => {
// Init DB connections on startup
console.info('Connecting to DB');
await dbModels.connect();
// Load routes after DB is connected
// eslint-disable-next-line global-require
const filesRoutes = require('./src/routes/files');
// eslint-disable-next-line global-require
const foldersRoutes = require('./src/routes/folders');
server.register(filesRoutes);
server.register(foldersRoutes);
// Register default routes
server.get('/', async (request, reply) => {
reply
.code(200)
.send('Test Microservice running');
});
// add alive and ready routes
server.get('/health', async (request, reply) => {
reply
.code(200)
.send({ alive: true });
});
server.get('/readiness', async (request, reply) => {
if (dbModels.isConnected()) {
return reply
.code(200)
.send({ ready: true });
}
return reply
.code(503)
.send({ ready: false });
});
try {
console.info('Starting Server');
// print routes in development
await server.listen(process.env.PORT || 3000, _env === 'development' ? 'localhost' : '0.0.0.0');
console.info(`Server listening on ${server.server.address().address}:${server.server.address().port}`);
if (_env === 'development') {
server.ready((err) => {
if (err) throw err;
server.swagger();
});
}
} catch (err) {
console.error(err.stack, { type: errorTypes.SERVER_ERROR.name });
process.exit(1);
}
return server;
};
// start the service if not in test mode
if (process.env.NODE_ENV !== 'test') {
start();
}
process.on('SIGINT', async () => {
console.log('stopping server');
await server.close();
process.exit(0);
});
module.exports = start;