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