0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00
logto/packages/core/src/routes/init.ts
2021-09-01 17:35:23 +08:00

36 lines
1 KiB
TypeScript

import Koa from 'koa';
import mount from 'koa-mount';
import Router from 'koa-router';
import { Provider } from 'oidc-provider';
import koaAuth from '@/middleware/koa-auth';
import applicationRoutes from '@/routes/application';
import sessionRoutes from '@/routes/session';
import swaggerRoutes from '@/routes/swagger';
import userRoutes from '@/routes/user';
import { AnonymousRouter, AuthedRouter } from './types';
const createRouters = (provider: Provider) => {
const anonymousRouter: AnonymousRouter = new Router();
sessionRoutes(anonymousRouter, provider);
userRoutes(anonymousRouter);
swaggerRoutes(anonymousRouter);
const router: AuthedRouter = new Router();
router.use(koaAuth());
applicationRoutes(router);
return [anonymousRouter, router];
};
export default function initRouter(app: Koa, provider: Provider) {
const apisApp = new Koa();
for (const router of createRouters(provider)) {
apisApp.use(router.routes()).use(router.allowedMethods());
}
app.use(mount('/api', apisApp));
}