0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/core/src/routes/init.ts

47 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-06-27 20:44:05 +08:00
import Koa from 'koa';
2021-08-30 11:30:54 +08:00
import mount from 'koa-mount';
2021-06-27 20:44:05 +08:00
import Router from 'koa-router';
import { Provider } from 'oidc-provider';
2021-08-30 11:30:54 +08:00
2021-09-01 17:35:23 +08:00
import koaAuth from '@/middleware/koa-auth';
2021-08-15 23:39:03 +08:00
import applicationRoutes from '@/routes/application';
import connectorRoutes from '@/routes/connector';
import resourceRoutes from '@/routes/resource';
2021-08-30 11:30:54 +08: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-30 11:30:54 +08:00
import swaggerRoutes from '@/routes/swagger';
import userRoutes from '@/routes/user';
2021-06-27 20:44:05 +08:00
2021-09-01 17:35:23 +08:00
import { AnonymousRouter, AuthedRouter } from './types';
2021-08-15 23:39:03 +08:00
const createRouters = (provider: Provider) => {
2021-09-01 17:35:23 +08:00
const anonymousRouter: AnonymousRouter = new Router();
2021-06-27 20:44:05 +08:00
statusRoutes(anonymousRouter);
2021-08-15 23:39:03 +08:00
sessionRoutes(anonymousRouter, provider);
2022-01-26 13:21:45 +08:00
userRoutes(anonymousRouter);
2021-08-15 23:39:03 +08:00
swaggerRoutes(anonymousRouter);
2021-09-01 17:35:23 +08:00
const router: AuthedRouter = new Router();
2021-08-15 23:39:03 +08:00
router.use(koaAuth());
2021-08-14 21:39:37 +08:00
applicationRoutes(router);
settingRoutes(router);
connectorRoutes(router);
resourceRoutes(router);
signInExperiencesRoutes(router);
2021-06-27 20:44:05 +08:00
2021-08-15 23:39:03 +08:00
return [anonymousRouter, router];
};
export default function initRouter(app: Koa, provider: Provider) {
2021-08-15 23:39:03 +08: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 20:44:05 +08:00
}