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

52 lines
1.7 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';
import koaLogSession from '@/middleware/koa-log-session';
2021-08-15 10:39:03 -05:00
import applicationRoutes from '@/routes/application';
import connectorRoutes from '@/routes/connector';
import resourceRoutes from '@/routes/resource';
2021-08-29 22:30:54 -05:00
import sessionRoutes from '@/routes/session';
import settingRoutes from '@/routes/setting';
import signInExperiencesRoutes from '@/routes/sign-in-experience';
import statusRoutes from '@/routes/status';
2021-08-29 22:30:54 -05:00
import swaggerRoutes from '@/routes/swagger';
2021-06-27 07:44:05 -05:00
import adminUserRoutes from './admin-user';
import roleRoutes from './role';
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) => {
const sessionRouter: AnonymousRouter = new Router();
sessionRouter.use(koaLogSession(provider));
sessionRoutes(sessionRouter, provider);
2021-06-27 07:44:05 -05:00
const anonymousRouter: AnonymousRouter = new Router();
statusRoutes(anonymousRouter);
2021-08-15 10:39:03 -05:00
swaggerRoutes(anonymousRouter);
const authedRouter: AuthedRouter = new Router();
authedRouter.use(koaAuth());
applicationRoutes(authedRouter);
settingRoutes(authedRouter);
connectorRoutes(authedRouter);
resourceRoutes(authedRouter);
signInExperiencesRoutes(authedRouter);
adminUserRoutes(authedRouter);
roleRoutes(authedRouter);
return [sessionRouter, anonymousRouter, authedRouter];
};
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
}