From 609499bc61f6c4dd89e0f66d3c768b77f0a8dab8 Mon Sep 17 00:00:00 2001 From: geek Date: Fri, 30 Dec 2016 15:46:33 -0600 Subject: [PATCH] Use hapi for frontend --- frontend/package.json | 6 +-- frontend/server/index.js | 90 ++++++++++++++++++++++---------------- frontend/server/locales.js | 11 ----- 3 files changed, 56 insertions(+), 51 deletions(-) delete mode 100644 frontend/server/locales.js diff --git a/frontend/package.json b/frontend/package.json index e5cf6078..a88f6f73 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -20,10 +20,10 @@ }, "dependencies": { "constant-case": "^2.0.0", - "express": "^4.14.0", "force-array": "^3.1.0", + "hapi": "^16.1.0", + "inert": "^4.1.0", "inherits": "^2.0.3", - "locale": "^0.1.0", "lodash.find": "^4.6.0", "lodash.get": "^4.4.2", "lodash.isempty": "^4.4.0", @@ -47,8 +47,8 @@ "redux-promise-middleware": "^4.2.0", "redux-thunk": "^2.1.0", "reselect": "^2.5.4", - "st": "^1.2.0", "styled-components": "^1.2.1", + "understood": "^1.0.1", "url-loader": "^0.5.7" }, "devDependencies": { diff --git a/frontend/server/index.js b/frontend/server/index.js index 3cace565..11bd12fd 100644 --- a/frontend/server/index.js +++ b/frontend/server/index.js @@ -1,44 +1,60 @@ -const template = require('lodash.template'); -const locale = require('locale'); -const path = require('path'); -const express = require('express'); -const st = require('st'); -const fs = require('fs'); +'use strict'; -const app = express(); +const fs = require('fs'); +const hapi = require('hapi'); +const inert = require('inert'); +const path = require('path'); +const template = require('lodash.template'); +const understood = require('understood'); const index = path.join(__dirname, './index.html'); const html = template(fs.readFileSync(index, 'utf-8')); -var mount = st({ - path: path.join(__dirname, '../static'), - url: 'static/', - index: false, - dot: false, - passthrough: false, - gzip: true, - cors: false +const server = new hapi.Server({ + connections: { + routes: { + files: { + relativeTo: path.join(__dirname, '../static') + } + } + } +}); +server.connection({ port: process.env.PORT || 8000 }); + +server.register([ + inert, + { register: understood, options: { default: 'en-us', localesDir: path.join(__dirname, '../static/locales') } }], + (err) => { + if (err) { + console.error(err); + process.exit(1); + } + + server.route({ + method: 'GET', + path: '/static/{param*}', + handler: { + directory: { + path: '.', + redirectToSlash: true, + index: false + } + } + }); + + server.route({ method: '*', path: '/{param*}', handler: defaultHandler }); + + server.start((err) => { + if (err) { + console.error(err); + process.exit(1); + } + + console.log(`Server running at: http://localhost:${server.info.port}`); + }); }); -app.use(mount); -app.use(locale(require('./locales'))); - -app.get('/*', (req, res, next) => { - const locale = (req.locale || '').toLowerCase(); - const lang = locale.split(/\-/)[0]; - - res.header('Content-Type', 'text/html'); - - res.send(html({ - locale, - lang - })); -}); - -app.listen(8000, (err, address) => { - if (err) { - throw err; - } - - console.log('Server running at: http://localhost:8000'); -}); +function defaultHandler (request, reply) { + const locales = (request.locale || '').toLowerCase().split(/\-/); + reply(html({ locale: locales[1], lang: locales[0] })); +} diff --git a/frontend/server/locales.js b/frontend/server/locales.js deleted file mode 100644 index d75eee7e..00000000 --- a/frontend/server/locales.js +++ /dev/null @@ -1,11 +0,0 @@ -const uniq = require('lodash.uniq'); -const fs = require('fs'); -const path = require('path'); - -const files = fs.readdirSync(path.join(__dirname, '../static/locales')); - -module.exports = uniq(files.map((file) => { - return file.replace(/\.js$/, ''); -}).filter((file) => { - return file.match(/.*?-.*?/); -}));