-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (56 loc) · 1.69 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
const fs = require('fs');
const Path = require('path');
const bunyan = require('bunyan');
const Hapi = require('@hapi/hapi');
const handlebars = require('handlebars');
const layouts = require('handlebars-layouts');
const conf = require('./src/config.js');
const influxOptions = {
// Or single-host configuration
host: conf.get('influx_host'),
port: conf.get('influx_port'),
protocol: 'http', // Optional, default 'http'
username: conf.get('influx_user'),
password: conf.get('influx_pw'),
database: conf.get('influx_db'),
};
const logger = bunyan.createLogger({ name: 'api', level: 'info' });
const server = new Hapi.Server({
port: conf.get('port'),
routes: {
cors: true,
files: {
relativeTo: Path.join(__dirname, 'public'),
},
},
});
const provision = async () => {
await server.register(require('@hapi/inert'));
await server.register(require('@hapi/vision'));
handlebars.registerHelper(layouts(handlebars));
handlebars.registerPartial('main', fs.readFileSync(__dirname + '/views/layouts/main.html', 'utf8'));
server.views({
engines: {
html: handlebars,
},
relativeTo: __dirname,
path: 'views',
});
await server.register({ plugin: require('./src/hapi-influx'), options: influxOptions });
await server.register({ plugin: require('hapi-pino'), options: { redact: ['req.headers.authorization'] } });
logger.info('Plugins Registered');
require('./src/routes')(server);
// Static assets
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: Path.join(__dirname, 'public'),
},
},
});
await server.start();
logger.info('Server running at:', server.info.uri);
};
provision();