X-Git-Url: https://git.r.bdr.sh/rbdr/generador-de-insultos/blobdiff_plain/4fc8123b78a8b275288fb875254ee1370c0bf59b..c0d27c055055f4aada0a96b779e0bd661347160c:/bin/server.js diff --git a/bin/server.js b/bin/server.js index 2dbcc1d..13ae381 100755 --- a/bin/server.js +++ b/bin/server.js @@ -2,49 +2,48 @@ '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:'/', - handler: function (request, reply) { - insults.generate().then(function (insult) { - reply(template.replace(replaceRe, insult)); - }); + 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(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); @@ -52,14 +51,19 @@ fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, fu } 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); }); });