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

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-06-27 07:44:05 -05:00
import Koa from 'koa';
2021-08-29 22:30:54 -05:00
import mount from 'koa-mount';
2021-06-27 07:44:05 -05:00
import Router from 'koa-router';
import { Provider } from 'oidc-provider';
2021-08-29 22:30:54 -05:00
2021-09-01 04:35:23 -05:00
import koaAuth from '@/middleware/koa-auth';
2021-08-15 10:39:03 -05:00
import applicationRoutes from '@/routes/application';
2021-08-29 22:30:54 -05:00
import sessionRoutes from '@/routes/session';
import statusRoutes from '@/routes/status';
2021-08-29 22:30:54 -05:00
import swaggerRoutes from '@/routes/swagger';
import userRoutes from '@/routes/user';
2021-06-27 07:44:05 -05:00
2021-09-01 04:35:23 -05:00
import { AnonymousRouter, AuthedRouter } from './types';
2021-08-15 10:39:03 -05:00
const createRouters = (provider: Provider) => {
2021-09-01 04:35:23 -05:00
const anonymousRouter: AnonymousRouter = new Router();
2021-06-27 07:44:05 -05:00
statusRoutes(anonymousRouter);
2021-08-15 10:39:03 -05:00
sessionRoutes(anonymousRouter, provider);
userRoutes(anonymousRouter, provider);
2021-08-15 10:39:03 -05:00
swaggerRoutes(anonymousRouter);
2021-09-01 04:35:23 -05:00
const router: AuthedRouter = new Router();
2021-08-15 10:39:03 -05:00
router.use(koaAuth());
2021-08-14 08:39:37 -05:00
applicationRoutes(router);
2021-06-27 07:44:05 -05:00
2021-08-15 10:39:03 -05:00
return [anonymousRouter, router];
};
export default function initRouter(app: Koa, provider: Provider) {
2021-08-15 10:39:03 -05:00
const apisApp = new Koa();
for (const router of createRouters(provider)) {
apisApp.use(router.routes()).use(router.allowedMethods());
}
app.use(mount('/api', apisApp));
2021-06-27 07:44:05 -05:00
}