diff --git a/src2/app.ts b/src2/app.ts index 1b76c09..e3c0a02 100644 --- a/src2/app.ts +++ b/src2/app.ts @@ -1,16 +1,17 @@ -import express, { json as BodyParserJson } from 'express'; +import express, { Request, Response, NextFunction, RequestHandler, json as BodyParserJson } from 'express'; import fs from 'fs-extra'; import { path, isProd } from '@tycrek/joint'; import { log } from './log'; +import { ServerConfiguration } from 'ass'; /** - * Core Express server config. - * This is separate from the user configuration starting in 0.15.0 + * Custom middleware to attach the ass object (and construct the `host` property) */ -interface ServerConfiguration { - host: string, - port: number, - proxied: boolean +function assMetaMiddleware(port: number, proxied: boolean): RequestHandler { + return (req: Request, _res: Response, next: NextFunction) => { + req.ass = { host: `${req.protocol}://${req.hostname}${proxied ? '' : `:${port}`}` }; + next(); + } } /** @@ -60,6 +61,9 @@ async function main() { // Middleware app.use(log.express()); app.use(BodyParserJson()); + app.use(assMetaMiddleware(serverConfig.port, serverConfig.proxied)); + + app.get('/.ass.host', (req, res) => res.send(req.ass.host)); // Routing app.use('/setup', (await import('./routers/setup')).router); diff --git a/src2/global.d.ts b/src2/global.d.ts new file mode 100644 index 0000000..e1f0bdb --- /dev/null +++ b/src2/global.d.ts @@ -0,0 +1,19 @@ +import { Request, Response } from 'express'; + +declare global { + namespace Express { + interface Request { + + /** + * ass-specific request items + */ + ass: { + + /** + * Combination of {protocol}://{hostname} + */ + host: string + } + } + } +}