From a9747ec5101ba7f3d6de1521f60d1b4255d26b18 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 8 Sep 2019 21:56:16 -0600 Subject: [PATCH] Redo Fastify routing to work with Choo's routing --- server/index.js | 8 +++--- server/routes/public.js | 17 ++++++++++++ server/routes/resources.js | 57 -------------------------------------- 3 files changed, 21 insertions(+), 61 deletions(-) create mode 100644 server/routes/public.js delete mode 100644 server/routes/resources.js diff --git a/server/index.js b/server/index.js index 8e33124..8ce9c5a 100644 --- a/server/index.js +++ b/server/index.js @@ -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) { diff --git a/server/routes/public.js b/server/routes/public.js new file mode 100644 index 0000000..3507203 --- /dev/null +++ b/server/routes/public.js @@ -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 \ No newline at end of file diff --git a/server/routes/resources.js b/server/routes/resources.js deleted file mode 100644 index 513a85b..0000000 --- a/server/routes/resources.js +++ /dev/null @@ -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 \ No newline at end of file