2018-01-08 17:13:05 +02:00
|
|
|
const Inert = require('inert');
|
|
|
|
const Path = require('path');
|
2018-02-20 02:35:31 +02:00
|
|
|
const RenderReact = require('hapi-render-react');
|
|
|
|
const Url = require('url');
|
2018-03-23 02:57:18 +02:00
|
|
|
const Intercept = require('apr-intercept');
|
|
|
|
const Fs = require('mz/fs');
|
|
|
|
|
|
|
|
const { NAMESPACE = 'images' } = process.env;
|
2018-01-08 17:13:05 +02:00
|
|
|
|
|
|
|
exports.register = async server => {
|
2018-03-23 02:57:18 +02:00
|
|
|
const relativeTo = Path.join(__dirname, 'app');
|
|
|
|
const buildRoot = Path.join(__dirname, `../build/${NAMESPACE}/static/`);
|
|
|
|
const publicRoot = Path.join(__dirname, `../public/static/`);
|
|
|
|
|
2018-02-20 02:35:31 +02:00
|
|
|
await server.register([
|
|
|
|
{
|
|
|
|
plugin: Inert
|
|
|
|
},
|
|
|
|
{
|
2018-03-23 02:57:18 +02:00
|
|
|
plugin: RenderReact
|
2018-02-20 02:35:31 +02:00
|
|
|
}
|
|
|
|
]);
|
2018-01-08 17:13:05 +02:00
|
|
|
|
|
|
|
server.route([
|
2018-02-20 02:35:31 +02:00
|
|
|
{
|
|
|
|
method: 'GET',
|
2018-03-23 02:57:18 +02:00
|
|
|
path: `/${NAMESPACE}/service-worker.js`,
|
2018-02-20 02:35:31 +02:00
|
|
|
config: {
|
|
|
|
auth: false,
|
|
|
|
handler: {
|
|
|
|
file: {
|
|
|
|
path: Path.join(__dirname, '../build/service-worker.js')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'GET',
|
2018-03-23 02:57:18 +02:00
|
|
|
path: `/${NAMESPACE}/favicon.ico`,
|
2018-02-20 02:35:31 +02:00
|
|
|
config: {
|
|
|
|
auth: false,
|
|
|
|
handler: {
|
|
|
|
file: {
|
|
|
|
path: Path.join(__dirname, '../build/favicon.ico')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'GET',
|
2018-03-23 02:57:18 +02:00
|
|
|
path: `/${NAMESPACE}/static/{rest*}`,
|
2018-02-20 02:35:31 +02:00
|
|
|
config: {
|
2018-03-23 02:57:18 +02:00
|
|
|
auth: false
|
|
|
|
},
|
|
|
|
handler: async (request, h) => {
|
|
|
|
const { params } = request;
|
|
|
|
const { rest } = params;
|
2018-02-20 02:35:31 +02:00
|
|
|
|
2018-03-23 02:57:18 +02:00
|
|
|
const publicPathname = Path.join(publicRoot, rest);
|
|
|
|
const buildPathname = Path.join(buildRoot, rest);
|
2018-02-20 02:35:31 +02:00
|
|
|
|
2018-03-23 02:57:18 +02:00
|
|
|
const [err] = await Intercept(
|
|
|
|
Fs.access(publicPathname, Fs.constants.R_OK)
|
|
|
|
);
|
2018-02-20 02:35:31 +02:00
|
|
|
|
2018-03-23 02:57:18 +02:00
|
|
|
const file = err ? buildPathname : publicPathname;
|
|
|
|
return h.file(file, { confine: false });
|
2018-01-08 17:13:05 +02:00
|
|
|
}
|
|
|
|
},
|
2018-02-20 02:35:31 +02:00
|
|
|
{
|
|
|
|
method: '*',
|
2018-03-23 02:57:18 +02:00
|
|
|
path: `/${NAMESPACE}/~server-error`,
|
2018-02-20 02:35:31 +02:00
|
|
|
handler: {
|
|
|
|
view: {
|
2018-03-23 02:57:18 +02:00
|
|
|
name: 'server-error',
|
|
|
|
relativeTo
|
2018-02-20 02:35:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-01-08 17:13:05 +02:00
|
|
|
{
|
|
|
|
method: '*',
|
2018-03-23 02:57:18 +02:00
|
|
|
path: `/${NAMESPACE}/{path*}`,
|
2018-02-20 02:35:31 +02:00
|
|
|
handler: {
|
|
|
|
view: {
|
2018-03-23 02:57:18 +02:00
|
|
|
name: 'app',
|
|
|
|
relativeTo
|
2018-01-08 17:13:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.pkg = require('../package.json');
|