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