diff --git a/backend/app.ts b/backend/app.ts index 857bae3..6d6c9cb 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -10,6 +10,7 @@ import { ensureFiles } from './data'; import { UserConfig } from './UserConfig'; import { ServerConfiguration } from 'ass'; import { MySql } from './sql/mysql'; +import { buildFrontendRouter } from './routers/_frontend'; /** * Top-level metadata exports @@ -129,11 +130,13 @@ async function main() { app.get('/.ass.host', (req, res) => res.type('text').send(req.ass.host)); app.get('/.ass.version', (req, res) => res.type('text').send(req.ass.version)); - // Routers + // Basic page routers + app.use('/login', buildFrontendRouter('login')); + app.use('/admin', buildFrontendRouter('admin')); + app.use('/user', buildFrontendRouter('user')); + + // Advanced routers app.use('/setup', (await import('./routers/setup.js')).router); - app.use('/login', (await import('./routers/login.js')).router); - app.use('/admin', (await import('./routers/admin.js')).router); - app.use('/user', (await import('./routers/user.js')).router); app.use('/api', (await import('./routers/api.js')).router); app.use('/', (await import('./routers/index.js')).router); diff --git a/backend/routers/_frontend.ts b/backend/routers/_frontend.ts new file mode 100644 index 0000000..9a830db --- /dev/null +++ b/backend/routers/_frontend.ts @@ -0,0 +1,30 @@ +import { path } from '@tycrek/joint'; +import { Router } from 'express'; +import { UserConfig } from '../UserConfig'; +import { App } from '../app'; + +/** + * Builds a basic router for loading a page with frontend JS + */ +export const buildFrontendRouter = (page: string, onConfigReady = true) => { + + // Config readiness checker + const ready = () => (onConfigReady) + ? UserConfig.ready + : !UserConfig.ready; + + // Set up a router + const router = Router({ caseSensitive: true }); + + // Render the page + router.get('/', (_req, res) => ready() + ? res.render(page, { version: App.pkgVersion }) + : res.redirect('/')); + + // Load frontend JS + router.get('/ui.js', (_req, res) => ready() + ? res.type('text/javascript').sendFile(path.join(`dist-frontend/${page}.mjs`)) + : res.sendStatus(403)); + + return router; +}; diff --git a/backend/routers/admin.ts b/backend/routers/admin.ts deleted file mode 100644 index b3c55f0..0000000 --- a/backend/routers/admin.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { path } from '@tycrek/joint'; -import { Router, json as BodyParserJson } from 'express'; -import { log } from '../log'; -import { UserConfig } from '../UserConfig'; -import { App } from '../app'; - -const router = Router({ caseSensitive: true }); - -// Static routes -router.get('/', (req, res) => !UserConfig.ready ? res.redirect('/') : res.render('admin', { version: App.pkgVersion })); -router.get('/ui.js', (req, res) => !UserConfig.ready ? res.send('') : res.type('text/javascript').sendFile(path.join('dist-frontend/admin.mjs'))); - -router.post('/', BodyParserJson(), async (req, res) => { -}); - -export { router }; diff --git a/backend/routers/login.ts b/backend/routers/login.ts deleted file mode 100644 index 3d07501..0000000 --- a/backend/routers/login.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { path } from '@tycrek/joint'; -import { Router, json as BodyParserJson } from 'express'; -import { log } from '../log'; -import { UserConfig } from '../UserConfig'; -import { App } from '../app'; - -const router = Router({ caseSensitive: true }); - -// Static routes -router.get('/', (req, res) => !UserConfig.ready ? res.redirect('/') : res.render('login', { version: App.pkgVersion })); -router.get('/ui.js', (req, res) => !UserConfig.ready ? res.send('') : res.type('text/javascript').sendFile(path.join('dist-frontend/login.mjs'))); - -router.post('/', BodyParserJson(), async (req, res) => { -}); - -export { router }; diff --git a/backend/routers/user.ts b/backend/routers/user.ts deleted file mode 100644 index 60a5ab1..0000000 --- a/backend/routers/user.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { path } from '@tycrek/joint'; -import { Router, json as BodyParserJson } from 'express'; -import { log } from '../log'; -import { UserConfig } from '../UserConfig'; -import { App } from '../app'; - -const router = Router({ caseSensitive: true }); - -// Static routes -router.get('/', (req, res) => !UserConfig.ready ? res.redirect('/') : res.render('user', { version: App.pkgVersion })); -router.get('/ui.js', (req, res) => !UserConfig.ready ? res.send('') : res.type('text/javascript').sendFile(path.join('dist-frontend/user.mjs'))); - -router.post('/', BodyParserJson(), async (req, res) => { -}); - -export { router };