'use strict';
-var fs = require('fs');
-var path = require('path');
+const Fs = require('fs');
+const Path = require('path');
-var Hapi = require('hapi');
+const Hapi = require('@hapi/hapi');
-var server = new Hapi.Server();
-var InsultGenerator = require('../lib/InsultGenerator');
+const InsultGenerator = require('../lib/InsultGenerator');
-var insults = new InsultGenerator({
- file : path.resolve(__dirname, '../ext/words.tab')
-});
-var replaceRe = /{{insult}}/
-var template;
+const replaceRe = /{{insult}}/
+let template;
-server.connection({
- host: 'localhost',
- port: 9200
+const server = Hapi.Server({
+ host: 'localhost',
+ port: 9200
});
server.route({
method: 'GET',
path:'/',
+ async handler(request, reply) {
+ return template.replace(replaceRe, await InsultGenerator.generate());
+ }
+});
+
+server.route({
+ config: {
+ cors: {
+ origin: ['*'],
+ additionalHeaders: ['cache-control', 'x-requested-with']
+ }
+ },
+ method: 'GET',
+ path:'/raw',
handler: function (request, reply) {
- insults.generate().then(function (insult) {
- reply(template.replace(replaceRe, insult));
- });
+
+ return InsultGenerator.generate();
}
});
// Preload stuff, then run
-console.log("Loading template...");
-fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) {
+console.debug('Loading template...');
+Fs.readFile(Path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) {
if (err) {
console.error(err);
}
template = contents;
- console.log("Setting up insults...")
- insults.generate().then(function (insult) {
+ console.log('Setting up insults...')
+ InsultGenerator.generate()
+ .then(function (insult) {
+
server.start(function() {
- console.log('Server running at:', server.info.uri);
+
+ console.debug('Server running at:', server.info.uri);
});
- }).catch(function (err) {
+ })
+ .catch(function (err) {
+
console.error(err);
- console.log("Error running insults... bad augur");
+ console.error('Error running insults... bad augur');
process.exit(1);
});
});