From: Ben Beltran Date: Sat, 18 Apr 2020 12:20:53 +0000 (+0200) Subject: Update code for 2020 X-Git-Url: https://git.r.bdr.sh/rbdr/generador-de-insultos/commitdiff_plain/da393520c4a1b33bffdfe26974e817f2c65267d3?ds=inline;hp=e865f21be5685ef2fbc57e36460f8d6819e0fe34 Update code for 2020 --- diff --git a/.gitignore b/.gitignore index 123ae94..bfb1212 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules + +.DS_Store diff --git a/bin/server.js b/bin/server.js index 00c23bb..13ae381 100755 --- a/bin/server.js +++ b/bin/server.js @@ -2,33 +2,27 @@ '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()); } }); @@ -42,15 +36,14 @@ server.route({ 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); @@ -58,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); }); }); diff --git a/ext/index.html b/ext/index.html index 768e45e..2c1ddcf 100644 --- a/ext/index.html +++ b/ext/index.html @@ -10,6 +10,13 @@