forked from mukeshsharma1426/hapiv17-mysql-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
95 lines (77 loc) · 2.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
/***********************************************************
************** Author - Mukesh Sharma **************
************** Date - 2018/07/22 **************
***********************************************************/
/**
* Logger file startup
*/
require('./modules/logger/winstonLogger.js');
const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const HapiSwagger = require('hapi-swagger');
const AuthBearer = require('hapi-auth-bearer-token');
const Libs = require('./modules/libs/commFunctions')
global.Config = require('./config')
global.Constant = require('./constant')
const Routes = require('./modules/routes')
require('./bootstrap') // Database Initialization
/**
* serverSetup function to write all the server bootstraping things in one place
**/
async function serverSetup() {
try {
/**
* server host and port configuration for server setup
**/
const serverConfig = Config.serverSetupConfig
const server = new Hapi.Server(serverConfig);
await server.register(AuthBearer)
server.auth.strategy('skelton', 'bearer-access-token', {
allowQueryToken: true, // optional, false by default
validate: async (request, token, h) => {
const isValid = await Libs.verifyToken(token);
const credentials = { token };
let artifacts = {};
if(isValid){
artifacts = isValid.data;
}
return { isValid, credentials, artifacts };
}
});
/**
* swagger setup for routes documentation
**/
const swaggerOptions = Config.swaggerSetupConfig
/**
* registeration of inert and vision as per swagger dependencies.
**/
await server.register([
Inert,
Vision,
{
plugin: HapiSwagger,
options: swaggerOptions
}
]);
/**
* Routes registerations in server
*/
server.route(Routes);
/************************
**** server started ****
************************/
await server.start();
winstonLogger.info('Server is running at -------------------------------> ', server.info.uri);
} catch (error) {
winstonLogger.error('Unable to start server -----------------------------> ', error);
/**
* Shutdown Process if error
*/
process.exit(1);
}
}
process.on('uncaughtException', (err) => {
winstonLogger.error('uncaught Exception Occurred ------------> ', err.stack);
});
serverSetup();