From 62b2bf953fea0b3f208d5c780f2e64290ccc0e8b Mon Sep 17 00:00:00 2001 From: tycrek Date: Wed, 7 Dec 2022 14:55:23 -0700 Subject: [PATCH 1/2] feat: added initial API routers (no routes yet) --- src/routers/api.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/routers/api.ts diff --git a/src/routers/api.ts b/src/routers/api.ts new file mode 100644 index 0000000..d1c5765 --- /dev/null +++ b/src/routers/api.ts @@ -0,0 +1,22 @@ +/** + * Developer API + * - Users + * - Resources + */ + +import { Router, Request, Response, NextFunction } from 'express'; +import { users } from '../auth'; +import { data } from '../data'; + +const RouterUser = Router(); +const RouterResource = Router(); + +/** + * Token authentication middleware + */ +const authMiddleware = (req: Request, res: Response, next: NextFunction) => { + const token = req.headers.authorization; + (token && users[token]) + ? next() + : res.sendStatus(401); +}; From f302f56ac6319dbbbbaef812917d344e0a33935e Mon Sep 17 00:00:00 2001 From: tycrek Date: Wed, 7 Dec 2022 15:53:06 -0700 Subject: [PATCH 2/2] feat: gave API an onStart() --- src/ass.ts | 4 ++++ src/routers/api.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/ass.ts b/src/ass.ts index 5b41b6b..445e893 100644 --- a/src/ass.ts +++ b/src/ass.ts @@ -10,6 +10,7 @@ import tailwindcss from 'tailwindcss'; import helmet from 'helmet'; import { path, log, getTrueHttp, getTrueDomain } from './utils'; +import { onStart as ApiOnStart } from './routers/api'; //#endregion //#region Setup - Run first time setup if using Docker (pseudo-process, setup will be run with docker exec) @@ -105,6 +106,9 @@ ASS_FRONTEND.enabled && app.use(ASS_FRONTEND.endpoint, ASS_FRONTEND.router); // // Upload router (has to come after custom frontends as express-busboy interferes with all POST calls) app.use('/', ROUTERS.upload); +// API +app.use('/api', ApiOnStart()); + // CSS app.use('/css', epcss({ cssPath: path('tailwind.css'), diff --git a/src/routers/api.ts b/src/routers/api.ts index d1c5765..c028130 100644 --- a/src/routers/api.ts +++ b/src/routers/api.ts @@ -8,6 +8,7 @@ import { Router, Request, Response, NextFunction } from 'express'; import { users } from '../auth'; import { data } from '../data'; +const RouterApi = Router(); const RouterUser = Router(); const RouterResource = Router(); @@ -20,3 +21,10 @@ const authMiddleware = (req: Request, res: Response, next: NextFunction) => { ? next() : res.sendStatus(401); }; + +export const onStart = () => { + RouterApi.use('/user', RouterUser); + RouterApi.use('/resource', RouterResource); + + return RouterApi; +};