-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
189 lines (167 loc) · 8.05 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const config = require('config');
const fs = require('fs');
const hapi = require('@hapi/hapi');
const HapiSwagger = require('hapi-swagger');
const http2 = require('http2');
const Inert = require('@hapi/inert');
const Vision = require('@hapi/vision');
const Routes = require('./api/index');
const hapiPlugins = [];
let env;
/**
* Default environment configuration options
* WARNING! NOT ENVIRONMENT VARIABLES LIKE process.env.NODE_ENV
* If needed elsewhere, this will need to be redone
* @type {[type]}
*/
try {
env = config.get(process.env.NODE_ENV || 'development');
} catch (error) {
console.warn("Environment file not defined. Attempting to look for environment variables or set defaults");
env = {};
env.HOST = process.env.HOST || 'localhost';
env.PORT = process.env.PORT || 3000;
env.ORIGIN = !Array.isArray(process.env?.ORIGIN) ? [process.env.ORIGIN || '*'] : process.env.ORIGIN;
env.SWAGGER_ENABLED = process.env.SWAGGER_ENABLED || true;
}
/**
* TLS Configuration with Self Signed Certs
* allowHTTP1 is needed for Software that does not support
* HTTP2 like Postman :(
* @type {Object}
*/
const listener = http2.createSecureServer({
allowHTTP1: true,
cert: fs.readFileSync('./sss-cert.pem'),
key: fs.readFileSync('./sss-key.pem'),
});
/**
* Hapi Server configuration
* tls: true is needed to tell hapi.js that TLS is running when you pass in a custom listener
* https://github.com/hapijs/hapi/issues/4437 explains the issue in detail, when applying CORS configuration here
* you have to explicitly allow ["*"] at the routes you dont need the ORIGIN enforced
* @type {[type]}
*/
const server = hapi.server({
host: env.HOST,
listener,
port: env.PORT,
router: {
stripTrailingSlash: true,
},
routes: {
cors: {
origin: env.ORIGIN,
headers: ['Accept', 'Authorization', 'Content-Type'],
//additionalHeaders = access-control-allow-headers
exposedHeaders: ['x-simple-superhero-service'],
},
payload: {
allow: ['application/json', 'application/*+json'],
},
security: true,
},
tls: true,
});
if (env.SWAGGER_ENABLED) {
const swgrDesc = `I needed a self-contained data service (no Database) for testing a number of different scenarios with a diverse and robust dataset that also contains some sparseness.
Service runs on Node and hapijs.
The service itself and the data contained within service is useful for testing:
1. CORS configuration
1. Server configuration
1. Bandwidth
1. Form population
1. Data visualization
1. Stubbing out UI components
...
Data is the comic book character dataset from [fivethrityeight](https://datahub.io/five-thirty-eight/comic-characters#readme)`;
const swaggerOptions = {
info: {
title: 'Simple Superhero Service API Documentation',
description: swgrDesc,
contact: {
name: 'MORGANGRAPHICS',
url: `https://github.com/morgangraphics`
}
},
schemes: ['https'],
host: `${env.HOST}:${env.PORT}`,
uiCompleteScript: `
$(document).ready(() => {
// $('input[name=help],input[name=pretty],input[name=random],input[name=seed]').toggle(false);
});
`,
};
hapiPlugins.push(
Inert, // Static Files
Vision, // Template Rendering
{ // Swagger
plugin: HapiSwagger,
options: swaggerOptions,
},
);
};
const banner = (server) => {
const swggr = (env.SWAGGER_ENABLED)? `\n Swagger Interface: ${server.info.uri}/documentation#! \n` : ''
const main = `
██████ ██▓ ███▄ ▄███▓ ██▓███ ██▓ ▓█████
▒██ ▒ ▓██▒▓██▒▀█▀ ██▒▓██░ ██▒▓██▒ ▓█ ▀
░ ▓██▄ ▒██▒▓██ ▓██░▓██░ ██▓▒▒██░ ▒███
▒ ██▒░██░▒██ ▒██ ▒██▄█▓▒ ▒▒██░ ▒▓█ ▄
▒██████▒▒░██░▒██▒ ░██▒▒██▒ ░ ░░██████▒░▒████▒
▒ ▒▓▒ ▒ ░░▓ ░ ▒░ ░ ░▒▓▒░ ░ ░░ ▒░▓ ░░░ ▒░ ░
░ ░▒ ░ ░ ▒ ░░ ░ ░░▒ ░ ░ ░ ▒ ░ ░ ░ ░
░ ░ ░ ▒ ░░ ░ ░░ ░ ░ ░
░ ░ ░ ░ ░ ░ ░
██████ █ ██ ██▓███ ▓█████ ██▀███ ██░ ██ ▓█████ ██▀███ ▒█████
▒██ ▒ ██ ▓██▒▓██░ ██▒▓█ ▀ ▓██ ▒ ██▒▓██░ ██▒▓█ ▀ ▓██ ▒ ██▒▒██▒ ██▒
░ ▓██▄ ▓██ ▒██░▓██░ ██▓▒▒███ ▓██ ░▄█ ▒▒██▀▀██░▒███ ▓██ ░▄█ ▒▒██░ ██▒
▒ ██▒▓▓█ ░██░▒██▄█▓▒ ▒▒▓█ ▄ ▒██▀▀█▄ ░▓█ ░██ ▒▓█ ▄ ▒██▀▀█▄ ▒██ ██░
▒██████▒▒▒▒█████▓ ▒██▒ ░ ░░▒████▒░██▓ ▒██▒░▓█▒░██▓░▒████▒░██▓ ▒██▒░ ████▓▒░
▒ ▒▓▒ ▒ ░░▒▓▒ ▒ ▒ ▒▓▒░ ░ ░░░ ▒░ ░░ ▒▓ ░▒▓░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░░ ▒░▒░▒░
░ ░▒ ░ ░░░▒░ ░ ░ ░▒ ░ ░ ░ ░ ░▒ ░ ▒░ ▒ ░▒░ ░ ░ ░ ░ ░▒ ░ ▒░ ░ ▒ ▒░
░ ░ ░ ░░░ ░ ░ ░░ ░ ░░ ░ ░ ░░ ░ ░ ░░ ░ ░ ░ ░ ▒
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
██████ ▓█████ ██▀███ ██▒ █▓ ██▓ ▄████▄ ▓█████
▒██ ▒ ▓█ ▀ ▓██ ▒ ██▒▓██░ █▒▓██▒▒██▀ ▀█ ▓█ ▀
░ ▓██▄ ▒███ ▓██ ░▄█ ▒ ▓██ █▒░▒██▒▒▓█ ▄ ▒███
▒ ██▒▒▓█ ▄ ▒██▀▀█▄ ▒██ █░░░██░▒▓▓▄ ▄██▒▒▓█ ▄
▒██████▒▒░▒████▒░██▓ ▒██▒ ▒▀█░ ░██░▒ ▓███▀ ░░▒████▒
▒ ▒▓▒ ▒ ░░░ ▒░ ░░ ▒▓ ░▒▓░ ░ ▐░ ░▓ ░ ░▒ ▒ ░░░ ▒░ ░
░ ░▒ ░ ░ ░ ░ ░ ░▒ ░ ▒░ ░ ░░ ▒ ░ ░ ▒ ░ ░ ░
░ ░ ░ ░ ░░ ░ ░░ ▒ ░░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░
░ ░
=============================================================================
Server running at: ${server.info.uri}
Marvel: ${server.info.uri}/marvel
DC: ${server.info.uri}/dc
${swggr}
=============================================================================
`;
return main;
};
/**
* Asynchronously start hapi server
* @param {[type]} async [description]
* @return {[type]} [description]
*/
(async () => {
await server.register(hapiPlugins);
try {
await server.start();
server.route(Routes);
console.log(banner(server));
//console.log('server config = ', server);
//console.log('server config = ', server._core.router.routes.get('get').routes[2]);
} catch (err) {
console.log(err);
}
})();
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
process.on('SIGHUP', (err) => {
console.log(`*^!@4=> Received event: ${err}`)
})