]> git.r.bdr.sh - rbdr/generador-de-insultos/blame_incremental - bin/server.js
Port to svelte
[rbdr/generador-de-insultos] / bin / server.js
... / ...
CommitLineData
1#!/usr/bin/env node
2
3'use strict';
4
5const Fs = require('fs');
6const Path = require('path');
7
8const Hapi = require('@hapi/hapi');
9
10const InsultGenerator = require('../lib/InsultGenerator');
11
12const replaceRe = /{{insult}}/
13let template;
14
15
16const server = Hapi.Server({
17 host: 'localhost',
18 port: 9200
19});
20
21server.route({
22 method: 'GET',
23 path:'/',
24 async handler(request, reply) {
25 return template.replace(replaceRe, await InsultGenerator.generate());
26 }
27});
28
29server.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
45console.debug('Loading template...');
46Fs.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});