]>
Commit | Line | Data |
---|---|---|
1a652aac BB |
1 | #!/usr/bin/env node |
2 | ||
3 | 'use strict'; | |
4 | ||
da393520 BB |
5 | const Fs = require('fs'); |
6 | const Path = require('path'); | |
1a652aac | 7 | |
da393520 | 8 | const Hapi = require('@hapi/hapi'); |
1a652aac | 9 | |
da393520 | 10 | const InsultGenerator = require('../lib/InsultGenerator'); |
1a652aac | 11 | |
da393520 BB |
12 | const replaceRe = /{{insult}}/ |
13 | let template; | |
1a652aac BB |
14 | |
15 | ||
da393520 BB |
16 | const server = Hapi.Server({ |
17 | host: 'localhost', | |
18 | port: 9200 | |
1a652aac BB |
19 | }); |
20 | ||
21 | server.route({ | |
22 | method: 'GET', | |
23 | path:'/', | |
da393520 BB |
24 | async handler(request, reply) { |
25 | return template.replace(replaceRe, await InsultGenerator.generate()); | |
1a652aac BB |
26 | } |
27 | }); | |
28 | ||
4fc8123b | 29 | server.route({ |
9002702b BB |
30 | config: { |
31 | cors: { | |
32 | origin: ['*'], | |
33 | additionalHeaders: ['cache-control', 'x-requested-with'] | |
34 | } | |
35 | }, | |
4fc8123b BB |
36 | method: 'GET', |
37 | path:'/raw', | |
38 | handler: function (request, reply) { | |
da393520 BB |
39 | |
40 | return InsultGenerator.generate(); | |
4fc8123b BB |
41 | } |
42 | }); | |
43 | ||
1a652aac | 44 | // Preload stuff, then run |
da393520 BB |
45 | console.debug('Loading template...'); |
46 | Fs.readFile(Path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) { | |
1a652aac BB |
47 | |
48 | if (err) { | |
49 | console.error(err); | |
50 | process.exit(1) | |
51 | } | |
52 | ||
53 | template = contents; | |
da393520 BB |
54 | console.log('Setting up insults...') |
55 | InsultGenerator.generate() | |
56 | .then(function (insult) { | |
57 | ||
1a652aac | 58 | server.start(function() { |
da393520 BB |
59 | |
60 | console.debug('Server running at:', server.info.uri); | |
1a652aac | 61 | }); |
da393520 BB |
62 | }) |
63 | .catch(function (err) { | |
64 | ||
1a652aac | 65 | console.error(err); |
da393520 | 66 | console.error('Error running insults... bad augur'); |
1a652aac BB |
67 | process.exit(1); |
68 | }); | |
69 | }); |