aboutsummaryrefslogtreecommitdiff
path: root/bin/server.js
blob: b07aab4285c87607a6c07777c47db194ad865f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env node

'use strict';

var fs = require('fs');
var path = require('path');

var Hapi = require('hapi');

var server = new Hapi.Server();
var InsultGenerator = require('../lib/InsultGenerator');

var insults = new InsultGenerator({
  file : path.resolve(__dirname, '../ext/words.tab')
});
var replaceRe = /{{insult}}/
var template;


server.connection({ 
    host: 'localhost', 
    port: 9200 
});

server.route({
    method: 'GET',
    path:'/', 
    handler: function (request, reply) {
      insults.generate().then(function (insult) {
        reply(template.replace(replaceRe, insult));
      });
    }
});

// Preload stuff, then run
console.log("Loading template...");
fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) {

  if (err) {
    console.error(err);
    process.exit(1)
  }

  template = contents;
  console.log("Setting up insults...")
  insults.generate().then(function (insult) {
    server.start(function() {
      console.log('Server running at:', server.info.uri);
    });
  }).catch(function (err) {
    console.error(err);
    console.log("Error running insults... bad augur");
    process.exit(1);
  });
});