Redo Fastify routing to work with Choo's routing

This commit is contained in:
Robbie Antenesse 2019-09-08 21:56:16 -06:00
parent 8d9e0e8f9f
commit a9747ec510
3 changed files with 21 additions and 61 deletions

View File

@ -33,10 +33,10 @@ fastify.addHook('onRequest', (request, reply, done) => {
// Routes
fastify.register(require('./routes/resources'));
fastify.register(require('./routes/home'));
fastify.register(require('./routes/account'));
fastify.register(require('./routes/search'));
fastify.register(require('./routes/public'));
// fastify.register(require('./routes/home'));
// fastify.register(require('./routes/account'));
// fastify.register(require('./routes/search'));
// Start the server
fastify.listen(fastify.siteConfig.port, function (err, address) {

17
server/routes/public.js Normal file
View File

@ -0,0 +1,17 @@
async function routes(fastify, options) {
// This route is not totally necessary because fastify-static serves public/ wholesale, but it's good to be verbose!
fastify.get('/', async (request, reply) => {
return reply.sendFile('index.html');
});
// This is overridden by any explicitly named routes, so the API will be fine.
fastify.get('/:chooRoute', async (request, reply) => {
if (/\.\w+$/.test(request.params.chooRoute)) { // If the :chooRoute is a filename, serve the file instead
return reply.sendFile(request.params.chooRoute);
}
// Otherwise, send index.html and allow Choo to route it.
return reply.sendFile('index.html');
});
}
module.exports = routes

View File

@ -1,57 +0,0 @@
async function routes(fastify, options) {
fastify.get('/styles/:css', async (request, reply) => {
reply.sendFile('css/' + request.params.css);
});
fastify.get('/images/:image', async (request, reply) => {
reply.sendFile('images/' + request.params.image);
});
fastify.get('/manifest.webmanifest', async (request, reply) => {
const manifest = {
name: typeof fastify.siteConfig !== 'undefined' ? fastify.siteConfig.siteName : 'name not configured',
short_name: typeof fastify.siteConfig !== 'undefined' ? fastify.siteConfig.siteName : 'name not configured',
icons: [
{
src: '/images/icon-128.png',
sizes: '128x128',
type: 'image/png',
},
{
src: '/images/icon-144.png',
sizes: '144x144',
type: 'image/png',
},
{
src: '/images/icon-152.png',
sizes: '152x152',
type: 'image/png',
},
{
src: '/images/icon-192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: '/images/icon-256.png',
sizes: '256x256',
type: 'image/png',
},
{
src: '/images/icon-512.png',
sizes: '512x512',
type: 'image/png',
}
],
start_url: '/',
display: 'standalone',
orientation: 'portrait',
background_color: '#000000',
theme_color: '#1C4AFF',
};
return JSON.stringify(manifest);
});
}
module.exports = routes