joyent-portal/frontend/server/index.js

80 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-12-30 23:46:33 +02:00
'use strict';
2016-10-20 15:36:24 +03:00
2016-12-30 23:46:33 +02:00
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');
2016-10-20 15:36:24 +03:00
2016-10-20 22:42:39 +03:00
const index = path.join(__dirname, './index.html');
const html = template(fs.readFileSync(index, 'utf-8'));
2016-12-30 23:46:33 +02:00
const server = new hapi.Server({
2016-12-30 23:46:55 +02:00
connections: {
routes: {
files: {
relativeTo: path.join(__dirname, '../static')
}
2016-12-30 23:46:33 +02:00
}
2016-12-30 23:46:55 +02:00
}
});
2017-01-03 15:36:09 +02:00
2016-12-30 23:46:55 +02:00
server.connection({
port: process.env.PORT || 8000
2016-10-20 15:36:24 +03:00
});
2016-12-30 23:46:33 +02:00
2017-01-03 15:36:09 +02:00
const plugins = [
2016-12-30 23:46:33 +02:00
inert,
2016-12-30 23:46:55 +02:00
{
register: understood,
options: {
2017-01-03 15:36:09 +02:00
'default': 'en-us',
localesDir: path.join(__dirname, '../static/locales')
2016-12-30 23:46:33 +02:00
}
2017-01-03 15:36:09 +02:00
}
];
2016-12-30 23:46:33 +02:00
2017-01-03 15:36:09 +02:00
const defaultHandler = (request, reply) => {
const locales = (request.locale || '').toLowerCase().split(/\-/);
reply(html({
locale: locales[1],
lang: locales[0]
}));
};
2016-12-30 23:46:33 +02:00
2017-01-03 15:36:09 +02:00
server.register(plugins, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
2016-12-30 23:46:33 +02:00
2017-01-03 15:36:09 +02:00
server.route({
method: 'GET',
path: '/static/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: false
2016-12-30 23:46:33 +02:00
}
2017-01-03 15:36:09 +02:00
}
});
2016-12-30 23:46:33 +02:00
2017-01-03 15:36:09 +02:00
server.route({
method: '*',
path: '/{param*}',
handler: defaultHandler
2016-12-30 23:46:55 +02:00
});
2016-10-20 15:36:24 +03:00
2017-01-03 15:36:09 +02:00
server.start((err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server running at: http://localhost:${server.info.port}`);
});
});