]> git.r.bdr.sh - rbdr/generador-de-insultos/blame - bin/server.js
Add a favicon
[rbdr/generador-de-insultos] / bin / server.js
CommitLineData
1a652aac
BB
1#!/usr/bin/env node
2
3'use strict';
4
da393520
BB
5const Fs = require('fs');
6const Path = require('path');
1a652aac 7
da393520 8const Hapi = require('@hapi/hapi');
1a652aac 9
da393520 10const InsultGenerator = require('../lib/InsultGenerator');
1a652aac 11
da393520
BB
12const replaceRe = /{{insult}}/
13let template;
1a652aac
BB
14
15
da393520
BB
16const server = Hapi.Server({
17 host: 'localhost',
18 port: 9200
1a652aac
BB
19});
20
21server.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 29server.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
45console.debug('Loading template...');
46Fs.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});