]> git.r.bdr.sh - rbdr/generador-de-insultos/blob - bin/server.js
b07aab4285c87607a6c07777c47db194ad865f01
[rbdr/generador-de-insultos] / bin / server.js
1 #!/usr/bin/env node
2
3 'use strict';
4
5 var fs = require('fs');
6 var path = require('path');
7
8 var Hapi = require('hapi');
9
10 var server = new Hapi.Server();
11 var InsultGenerator = require('../lib/InsultGenerator');
12
13 var insults = new InsultGenerator({
14 file : path.resolve(__dirname, '../ext/words.tab')
15 });
16 var replaceRe = /{{insult}}/
17 var template;
18
19
20 server.connection({
21 host: 'localhost',
22 port: 9200
23 });
24
25 server.route({
26 method: 'GET',
27 path:'/',
28 handler: function (request, reply) {
29 insults.generate().then(function (insult) {
30 reply(template.replace(replaceRe, insult));
31 });
32 }
33 });
34
35 // Preload stuff, then run
36 console.log("Loading template...");
37 fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) {
38
39 if (err) {
40 console.error(err);
41 process.exit(1)
42 }
43
44 template = contents;
45 console.log("Setting up insults...")
46 insults.generate().then(function (insult) {
47 server.start(function() {
48 console.log('Server running at:', server.info.uri);
49 });
50 }).catch(function (err) {
51 console.error(err);
52 console.log("Error running insults... bad augur");
53 process.exit(1);
54 });
55 });