joyent-portal/frontend/server/index.js

97 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-12-30 23:46:33 +02:00
'use strict';
2016-10-20 15:36:24 +03:00
2017-04-26 20:08:41 +03:00
const Fs = require('fs');
const Hapi = require('hapi');
const Inert = require('inert');
const Path = require('path');
const PortalApi = require('portal-api');
const Template = require('lodash.template');
const Understood = require('understood');
2016-10-20 15:36:24 +03:00
2017-04-26 20:08:41 +03:00
const index = Path.join(__dirname, './index.html');
const html = Template(Fs.readFileSync(index, 'utf-8'));
2016-10-20 22:42:39 +03:00
2017-04-26 20:08:41 +03:00
const server = new Hapi.Server({
2016-12-30 23:46:55 +02:00
connections: {
routes: {
files: {
2017-04-26 20:08:41 +03:00
relativeTo: Path.join(__dirname, '../static')
2016-12-30 23:46:55 +02:00
}
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 = [
2017-04-26 20:08:41 +03:00
Inert,
2016-12-30 23:46:55 +02:00
{
2017-04-26 20:08:41 +03:00
register: Understood,
2016-12-30 23:46:55 +02:00
options: {
2017-01-05 22:47:33 +02:00
default: 'en-us',
2017-04-26 20:08:41 +03:00
localesDir: Path.join(__dirname, '../static/locales')
2016-12-30 23:46:33 +02:00
}
2017-04-26 20:08:41 +03:00
},
{
register: PortalApi,
routes: { prefix: '/api' }
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({
2017-01-05 22:47:33 +02:00
locale: request.locale,
2017-01-03 15:36:09 +02:00
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-18 17:33:32 +02:00
server.route({
method: 'GET',
path: '/static/images/{param*}',
handler: {
directory: {
path: './images/',
redirectToSlash: true,
index: false
}
}
});
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}`);
});
});