joyent-portal/packages/my-joy-instances/lib/index.js

205 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-01-08 17:13:05 +02:00
const Inert = require('inert');
2018-03-06 03:14:33 +02:00
const resolvePkg = require('resolve-pkg');
2018-01-08 17:13:05 +02:00
const Path = require('path');
const RenderReact = require('hapi-render-react');
const Wreck = require('wreck');
const Url = require('url');
2018-01-08 17:13:05 +02:00
2018-03-06 03:14:33 +02:00
const ImagesRoot = resolvePkg('my-joy-images', {
cwd: __dirname
});
2018-01-08 17:13:05 +02:00
exports.register = async server => {
2018-03-06 03:14:33 +02:00
const InstancesRelativeTo = Path.join(__dirname, 'app');
const ImagesRelativeTo = Path.join(ImagesRoot, 'lib/app');
await server.register([
{
plugin: Inert
},
{
2018-03-06 03:14:33 +02:00
plugin: RenderReact
}
]);
2018-01-08 17:13:05 +02:00
server.route([
{
method: 'GET',
2018-03-06 03:14:33 +02:00
path: '/instances/service-worker.js',
config: {
auth: false,
handler: {
file: {
path: Path.join(__dirname, '../build/service-worker.js')
}
}
}
},
{
method: 'GET',
2018-03-06 03:14:33 +02:00
path: '/images/service-worker.js',
config: {
auth: false,
handler: {
file: {
path: Path.join(ImagesRoot, 'build/service-worker.js')
}
}
}
},
{
method: 'GET',
path: '/instances/favicon.ico',
config: {
auth: false,
handler: {
file: {
path: Path.join(__dirname, '../build/favicon.ico')
}
}
}
},
2018-03-06 03:14:33 +02:00
{
method: 'GET',
path: '/images/favicon.ico',
config: {
auth: false,
handler: {
file: {
path: Path.join(ImagesRoot, 'build/favicon.ico')
}
}
}
},
{
method: 'GET',
path: '/font/{pathname*}',
config: {
auth: false,
handler: async (request, h) => {
const { params } = request;
const { pathname } = params;
const location = Url.format({
protocol: 'https:',
slashes: true,
host: 'fonts.gstatic.com',
pathname
});
const res = await Wreck.request('GET', location);
return h.response(res);
}
}
},
{
method: 'GET',
path: '/fonts/css',
config: {
auth: false,
handler: async (request, h) => {
const { query, headers } = request;
const { family } = query;
const { host } = headers;
const url = Url.parse(`http://${host}`);
const location = Url.format({
protocol: 'https:',
slashes: true,
host: 'fonts.googleapis.com',
pathname: '/css',
query: { family }
});
const res = await Wreck.request('GET', location);
const body = await Wreck.read(res);
2018-02-28 00:40:51 +02:00
const _body = body
.toString()
.replace(
/https:\/\/fonts\.gstatic\.com/g,
`http://${url.host}/font`
);
return h
.response(_body)
.header('content-type', res.headers['content-type'])
.header('expires', res.headers.expires)
.header('date', res.headers.date)
.header('cache-control', res.headers['cache-control']);
}
}
},
2018-01-08 17:13:05 +02:00
{
method: 'GET',
2018-03-06 03:14:33 +02:00
path: '/instances/static/{path*}',
2018-01-08 17:13:05 +02:00
config: {
auth: false,
handler: {
directory: {
2018-03-07 17:36:32 +02:00
path: Path.join(__dirname, '../build/instances/static/'),
2018-01-08 17:13:05 +02:00
redirectToSlash: true,
index: false
}
}
}
},
2018-03-06 03:14:33 +02:00
{
method: 'GET',
path: '/images/static/{path*}',
config: {
auth: false,
handler: {
directory: {
2018-03-07 17:36:32 +02:00
path: Path.join(ImagesRoot, 'build/images/static/'),
2018-03-06 03:14:33 +02:00
redirectToSlash: true,
index: false
}
}
}
},
{
method: '*',
path: '/instances/~server-error',
handler: {
view: {
name: 'server-error',
relativeTo: InstancesRelativeTo
}
}
},
{
method: '*',
path: '/images/~server-error',
handler: {
view: {
name: 'server-error',
relativeTo: ImagesRelativeTo
}
}
},
{
method: '*',
2018-03-06 03:14:33 +02:00
path: '/images/{path*}',
handler: {
view: {
2018-03-06 03:14:33 +02:00
name: 'app',
relativeTo: ImagesRelativeTo
}
}
},
2018-01-08 17:13:05 +02:00
{
method: '*',
path: '/{path*}',
handler: {
view: {
2018-03-06 03:14:33 +02:00
name: 'app',
relativeTo: InstancesRelativeTo
2018-01-08 17:13:05 +02:00
}
}
}
]);
};
exports.pkg = require('../package.json');