feat: add backend
This commit is contained in:
10
apps/server/middlewares/index.ts
Normal file
10
apps/server/middlewares/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Router } from './router.ts';
|
||||
import { ResponseTime } from './responseTime.ts';
|
||||
import type Koa from 'koa';
|
||||
|
||||
export function registerMiddlewares(app: Koa) {
|
||||
const router = new Router();
|
||||
const responseTime = new ResponseTime();
|
||||
responseTime.apply(app);
|
||||
router.apply(app);
|
||||
}
|
||||
13
apps/server/middlewares/responseTime.ts
Normal file
13
apps/server/middlewares/responseTime.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { Middleware } from './types.ts';
|
||||
import type Koa from 'koa';
|
||||
|
||||
export class ResponseTime implements Middleware {
|
||||
apply(app: Koa): void {
|
||||
app.use(async (ctx, next) => {
|
||||
const start = Date.now();
|
||||
await next();
|
||||
const ms = Date.now() - start;
|
||||
ctx.set('X-Response-Time', `${ms}ms`);
|
||||
});
|
||||
}
|
||||
}
|
||||
19
apps/server/middlewares/router.ts
Normal file
19
apps/server/middlewares/router.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import KoaRouter from '@koa/router';
|
||||
import type Koa from 'koa';
|
||||
import type { Middleware } from './types.ts';
|
||||
import * as application from '../controllers/application.ts';
|
||||
|
||||
export class Router implements Middleware {
|
||||
private router: KoaRouter;
|
||||
constructor() {
|
||||
this.router = new KoaRouter({
|
||||
prefix: '/api',
|
||||
});
|
||||
this.router.get('/application/list', application.list);
|
||||
}
|
||||
|
||||
apply(app: Koa) {
|
||||
app.use(this.router.routes());
|
||||
app.use(this.router.allowedMethods());
|
||||
}
|
||||
}
|
||||
5
apps/server/middlewares/types.ts
Normal file
5
apps/server/middlewares/types.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type Koa from 'koa';
|
||||
|
||||
export abstract class Middleware {
|
||||
abstract apply(app: Koa, options?: unknown): void;
|
||||
}
|
||||
Reference in New Issue
Block a user