1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-11 05:40:11 +02:00

Use hapi for frontend

This commit is contained in:
geek 2016-12-30 15:46:33 -06:00
parent c168067296
commit 609499bc61
3 changed files with 56 additions and 51 deletions

View File

@ -20,10 +20,10 @@
}, },
"dependencies": { "dependencies": {
"constant-case": "^2.0.0", "constant-case": "^2.0.0",
"express": "^4.14.0",
"force-array": "^3.1.0", "force-array": "^3.1.0",
"hapi": "^16.1.0",
"inert": "^4.1.0",
"inherits": "^2.0.3", "inherits": "^2.0.3",
"locale": "^0.1.0",
"lodash.find": "^4.6.0", "lodash.find": "^4.6.0",
"lodash.get": "^4.4.2", "lodash.get": "^4.4.2",
"lodash.isempty": "^4.4.0", "lodash.isempty": "^4.4.0",
@ -47,8 +47,8 @@
"redux-promise-middleware": "^4.2.0", "redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.1.0", "redux-thunk": "^2.1.0",
"reselect": "^2.5.4", "reselect": "^2.5.4",
"st": "^1.2.0",
"styled-components": "^1.2.1", "styled-components": "^1.2.1",
"understood": "^1.0.1",
"url-loader": "^0.5.7" "url-loader": "^0.5.7"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,44 +1,60 @@
const template = require('lodash.template'); 'use strict';
const locale = require('locale');
const path = require('path');
const express = require('express');
const st = require('st');
const fs = require('fs');
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 index = path.join(__dirname, './index.html');
const html = template(fs.readFileSync(index, 'utf-8')); const html = template(fs.readFileSync(index, 'utf-8'));
var mount = st({ const server = new hapi.Server({
path: path.join(__dirname, '../static'), connections: {
url: 'static/', routes: {
index: false, files: {
dot: false, relativeTo: path.join(__dirname, '../static')
passthrough: false, }
gzip: true, }
cors: false }
}); });
server.connection({ port: process.env.PORT || 8000 });
app.use(mount); server.register([
app.use(locale(require('./locales'))); inert,
{ register: understood, options: { default: 'en-us', localesDir: path.join(__dirname, '../static/locales') } }],
app.get('/*', (req, res, next) => { (err) => {
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) { if (err) {
throw err; console.error(err);
process.exit(1);
} }
console.log('Server running at: http://localhost:8000'); 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}`);
});
}); });
function defaultHandler (request, reply) {
const locales = (request.locale || '').toLowerCase().split(/\-/);
reply(html({ locale: locales[1], lang: locales[0] }));
}

View File

@ -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(/.*?-.*?/);
}));