-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
88 lines (75 loc) · 2.13 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
"use strict";
require("dotenv").config();
const { createConnection } = require("typeorm");
const hapi = require("@hapi/hapi");
const inert = require("@hapi/inert");
const vision = require("@hapi/vision");
const hapiSwagger = require("hapi-swagger");
const pack = require("./package");
const routes = require("./src/routes");
const ormConfig = require("./src/db/ormConfig");
const { AzureAuthMiddleware } = require("./src/middleware/azureAuth.middleware");
const responseHandlerPlugin = require("./src/interceptor/response.interceptor");
const EnvironmentService = require("./src/shared/environment.service");
const init = async () => {
const swaggerOptions = {
info: {
title: "Nutrients Management Planning Api",
version: pack.version,
},
schemes: ["http","https"],
documentationPath: EnvironmentService.APPLICATION_SWAGGER_PATH() || "/docs",
grouping: "tags",
securityDefinitions: {
Bearer: {
type: "apiKey",
name: "Authorization",
in: "header",
description:
"Enter the Bearer Authorization string as following: Bearer [Generated-JWT-Token].",
},
},
security: [
{
Bearer: [],
},
],
};
await createConnection(ormConfig);
const azureAuthMiddleware = new AzureAuthMiddleware();
const server = hapi.server({
port: process.env.PORT ?? EnvironmentService.APPLICATION_PORT(),
// Use the port provided by IIS,
// host: "localhost",
});
await server.register([
inert,
vision,
{
plugin: hapiSwagger,
options: swaggerOptions,
},
responseHandlerPlugin,
]);
server.ext("onPreHandler", (request, h) => {
return azureAuthMiddleware.use(request, h);
});
server.route(routes);
server.events.on("response", function (request) {
console.log(
request.info.remoteAddress +
": " +
request.method.toUpperCase() +
" " +
request.path +
" --> " +
request.response.statusCode
);
});
await server.start();
console.log("Server running on %s", server.info.uri);
};
process.on("unhandledRejection", (err) => {
process.exit(1);
});
init();