1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-29 05:10:05 +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": {
"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": {

View File

@ -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] }));
}

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