-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (48 loc) · 1.31 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
//load Elastic APM
//var apm = require('elastic-apm-node').start({ logLevel: 'trace' });
require('dotenv').config();
//import adn initialize database connections
//const { knex, mongoose } = require('./config/connection');
//Require fatify framework and instantiate it
const fastify = require('fastify')({
trustProxy: true,
logger: {
level: process.env.LOG_LEVEL || 'error',
prettifier: require('pino-pretty'),
prettyPrint: {
errorProps: 'hint, detail',
levelFirst: true,
crlf: true
}
}
});
// Import Swagger Options
const swagger = require('./config/swagger');
// Register Swagger
fastify.register(require('fastify-swagger'), swagger.options);
//Import JWT plugin for fastify
fastify.register(require('fastify-jwt'), {
secret: process.env.JWTSECRET
});
fastify.post('/signup', (req, reply) => {
// some code
const payload = { name: 'Omotayo Ishola', role: 'admin' };
const token = fastify.jwt.sign({ payload });
reply.send({ token });
});
const routes = require('./routes/');
routes.forEach((route, index) => {
fastify.route(route);
});
// Run server function
const start = async () => {
try {
await fastify.listen(process.env.PORT);
fastify.swagger();
} catch (error) {
fastify.log.error(error);
apm.captureError(error);
process.exit(1);
}
};
start();