diff --git a/src/Application.ts b/src/Application.ts index 8148afb..e6f4ae9 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -6,6 +6,7 @@ import * as path from "path"; import ApplicationComponent from "./ApplicationComponent.js"; import CacheProvider from "./CacheProvider.js"; +import {route, setPublicUrl} from "./common/Routing.js"; import FrontendToolsComponent from "./components/FrontendToolsComponent.js"; import LogRequestsComponent from "./components/LogRequestsComponent.js"; import RedisComponent from "./components/RedisComponent.js"; @@ -37,6 +38,8 @@ export default abstract class Application implements Extendable('app.public_url')); } protected abstract getMigrations(): MigrationType[]; @@ -163,7 +166,7 @@ export default abstract class Application implements Extendable { req.flash('validation', bag.getMessages()); - res.redirect(req.getPreviousUrl() || Controller.route('home')); + res.redirect(req.getPreviousUrl() || route('home')); }, }); return; diff --git a/src/Controller.ts b/src/Controller.ts index 4b55cdc..659ebc2 100644 --- a/src/Controller.ts +++ b/src/Controller.ts @@ -1,55 +1,13 @@ -import config from "config"; import express, {IRouter, RequestHandler, Router} from "express"; import {PathParams} from "express-serve-static-core"; -import * as querystring from "querystring"; -import {ParsedUrlQueryInput} from "querystring"; import Application from "./Application.js"; +import {registerRoute} from "./common/Routing.js"; import FileUploadMiddleware from "./FileUploadMiddleware.js"; import {logger} from "./Logger.js"; import Middleware, {MiddlewareType} from "./Middleware.js"; export default abstract class Controller { - /** - * TODO: this should not be static, it should actually be bound to an app instance. - */ - private static readonly routes: { [p: string]: string | undefined } = {}; - - public static route( - route: string, - params: RouteParams = [], - query: ParsedUrlQueryInput = {}, - absolute: boolean = false, - ): string { - let path = this.routes[route]; - if (path === undefined) throw new Error(`Unknown route for name ${route}.`); - - const regExp = this.getRouteParamRegExp('[a-zA-Z0-9_-]+', 'g'); - if (typeof params === 'string' || typeof params === 'number') { - path = path.replace(regExp, '' + params); - } else if (Array.isArray(params)) { - let i = 0; - for (const match of path.matchAll(regExp)) { - if (match.length > 0) { - path = path.replace(match[0], typeof params[i] !== 'undefined' ? params[i] : ''); - } - i++; - } - path = path.replace(/\/+/g, '/'); - } else { - for (const key of Object.keys(params)) { - path = path.replace(this.getRouteParamRegExp(key), params[key].toString()); - } - } - - const queryStr = querystring.stringify(query); - return `${absolute ? config.get('public_url') : ''}${path}` + (queryStr.length > 0 ? '?' + queryStr : ''); - } - - private static getRouteParamRegExp(key: string, flags?: string): RegExp { - return new RegExp(`:${key}(\\(.+?\\))?\\??`, flags); - } - private readonly router: Router = express.Router(); private readonly fileUploadFormRouter: Router = express.Router(); private app?: Application; @@ -183,13 +141,15 @@ export default abstract class Controller { routePath = (prefix !== '/' ? prefix : '') + path; } - if (!Controller.routes[routeName]) { - if (typeof routePath === 'string') { - logger.info(`Route ${routeName} has path ${routePath}`); - Controller.routes[routeName] = routePath; - } else { - logger.warn(`Cannot assign path to route ${routeName}.`); - } + if (typeof routePath !== 'string') { + logger.warn(`Cannot assign path to route ${routeName}.`); + return; + } + + if (registerRoute(routeName, routePath)) { + logger.info(`Route ${routeName} has path ${routePath}`); + } else { + logger.warn(`Couldn't register ${routeName} for path ${routePath}`); } } @@ -202,5 +162,3 @@ export default abstract class Controller { this.app = app; } } - -export type RouteParams = { [p: string]: string | number } | string[] | string | number; diff --git a/src/assets/views/layouts/svelte_layout.html b/src/assets/views/layouts/svelte_layout.html index 9b2bd52..e154628 100644 --- a/src/assets/views/layouts/svelte_layout.html +++ b/src/assets/views/layouts/svelte_layout.html @@ -6,6 +6,9 @@