This repository has been archived by the owner on Apr 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (76 loc) · 2.1 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
require('dotenv').config();
const path = require('path');
const hapi = require('hapi');
const pick = require('lodash/pick');
const subdomains = require(process.env.SUBDOMAIN_LIB_PATH);
const NODE_ENV = process.env.NODE_ENV || 'development';
const HOST_DOMAIN = process.env.HOST_DOMAIN;
function handleError(err) {
console.error(err);
process.exit(1);
}
async function initialize() {
const server = hapi.server({
host: 'localhost',
port: process.env.PORT,
routes: {
files: {
relativeTo: path.resolve(__dirname, 'public')
}
}
});
await server.register({
plugin: require('hapi-pino'),
options: {
prettyPrint: NODE_ENV !== 'production'
}
});
await server.register(require('inert'));
server.route({
method: 'GET',
path: '/',
vhost: HOST_DOMAIN,
handler(request, h) {
return h.file('index.html');
}
});
server.route({
method: 'GET',
path: '/assets/{asset*}',
vhost: HOST_DOMAIN,
handler: {
directory: {
path: path.resolve(__dirname, 'public/assets')
}
}
});
server.route({
method: 'POST',
path: '/tunnels',
vhost: HOST_DOMAIN,
async handler(request, h) {
const seed = request.info.remoteAddress + ':' + request.info.remotePort;
const name = await subdomains.generate(seed);
const subdomain = await subdomains.add(name);
return pick(subdomain, ['address']);
}
});
server.route({
method: '*',
path: '/{p*}',
vhost: HOST_DOMAIN,
async handler(request, h) {
return h.file('404.html').code(404);
}
});
server.route({
method: '*',
path: '/{p*}',
async handler(request, h) {
return `test response for ${request.info.hostname}${request.path}`;
}
});
await server.start().catch(handleError);
}
process.on('unhandledRejection', handleError);
initialize();