-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
205 lines (169 loc) · 5.37 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
// Start this program from the command line with `pm2`
//
// ~/Nodes/punkish$ NODE_ENV=production pm2 start index.js --name zenodeo
// ~/Nodes/punkish$ pm2 restart zenodeo
const Hapi = require('@hapi/hapi');
const Inert = require('@hapi/inert');
const Vision = require('@hapi/vision');
const HapiSwagger = require('hapi-swagger');
const Disk = require('catbox-disk');
const Blipp = require('blipp');
const config = require('config');
const cacheName = config.get('v2.cache.name');
const cachePath = config.get('v2.cache.path');
const zendeoUri = config.get('v2.uri.zenodeo');
const Pack = require('./package');
const info = config.get('info');
const swaggeredScheme = config.get('swaggered-scheme');
const port = config.get('port');
const plog = require(config.get('plog'));
// Generate Swagger-compatible documentation for the app
// The commented options below are listed for completeness
// sakes. We just don't need them here.
const swaggerOptions = {
/******** URLs and plugin **************/
schemes: swaggeredScheme,
//host: 'http://localhost:3030',
//auth: false,
//cors: false
/******** JSON **************/
//jsonPath: '/swagger.json',
//basePath: '/v2/',
//pathPrefixSize: 2,
//pathReplacements: [],
info: {
title: info.title,
license: info.license,
version: Pack.version,
description: Pack.description,
termsOfService: info.termsOfService,
contact: Pack.author
},
/******** UI **************/
documentationPage: false,
documentationPath: "/docs",
sortEndpoints: 'ordered',
validatorUrl: null
};
// Create the server. Everything begins here.
const server = new Hapi.server({
// Get the value of port from the config settings
port: port,
// Since this application *always* runs behind a proxy,
// the value of host will *always* be 'localhost'
host: 'localhost',
routes: { cors: true },
cache: [{
name: cacheName,
engine: new Disk({
cachePath: cachePath,
cleanEvery: 0,
partition: 'cache'
})
}],
router: {
stripTrailingSlash: true
}
});
const start = async () => {
await server.register([
// Inert serves static files
{ plugin: Inert, options: {} },
// Vision provides templating support (see server.view() below
{ plugin: Vision, options: {} },
// Blipp just prints out to the console all the routes when
// the server starts. This is *only* in development mode
{ plugin: Blipp, options: {} },
// Generate Swagger-compaitible swagger-files and docs auto-
// magically
{ plugin: HapiSwagger, options: swaggerOptions }
]);
// API v1
await server.register(
{ plugin: require('./api/v1/index.js'), options: {} },
{ routes: { prefix: '/v1' } }
);
// API v2
await server.register(
{ plugin: require('./api/v2/index.js'), options: {} },
{ routes: { prefix: '/v2' } }
);
// Static pages
server.route([
require('./resources/inert'),
require('./resources/docs'),
require('./resources/tos'),
require('./resources/install'),
require('./resources/examples'),
require('./resources/about'),
require('./resources/releases'),
require('./resources/workflow'),
// A catch-all route for any request that hasn't been satisfied
// by any of the routes so far. Send back a 404 and suggest
// a starting point.
{
method: '*',
path: '/{any*}',
handler: function (request, h) {
return {
error: "404 Error! Page Not Found! You might want to start at the root",
root: zendeoUri
}
}
}
]);
// Use handlebars for templating
server.views({
engines: {
html: require('handlebars')
},
relativeTo: __dirname,
path: './views',
layoutPath: './views/layouts',
partialsPath: './views/partials',
layout: 'main',
isCached: false
});
await server.start();
if (process.env.NODE_ENV) {
console.log(
'Server running in %s mode on %s',
process.env.NODE_ENV.toUpperCase(),
server.info.uri
);
}
else {
console.log(
'Server running in DEVELOPMENT mode on %s',
server.info.uri
);
}
server.events.on('log', (event, tags) => {
if (tags.error) {
plog.logger({
host: server.info.uri,
//host: host,
start: '',
end: '',
status: 500,
resource: '',
query: '',
message: `Server error: ${event.error ? event.error.message : 'unknown'}`
});
}
});
server.events.on('response', function (request) {
plog.logger({
host: request.info.host,
//host: host,
start: request.info.received,
end: request.info.completed,
status: request.response.statusCode,
resource: request.route.path.split('/').pop(),
query: JSON.stringify(request.query),
message: request.url.href
});
});
};
start();