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

66 lines
2.4 KiB
TypeScript
Raw Normal View History

import { UserRole } from '@logto/schemas';
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';
import koaLogSession from '@/middleware/koa-log-session';
import adminUserRoutes from '@/routes/admin-user';
2021-08-15 23:39:03 +08:00
import applicationRoutes from '@/routes/application';
import authnRoutes from '@/routes/authn';
import connectorRoutes from '@/routes/connector';
import customPhraseRoutes from '@/routes/custom-phrase';
import dashboardRoutes from '@/routes/dashboard';
import logRoutes from '@/routes/log';
import phraseRoutes from '@/routes/phrase';
import resourceRoutes from '@/routes/resource';
import roleRoutes from '@/routes/role';
2022-08-23 17:34:00 +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 wellKnownRoutes from '@/routes/well-known';
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) => {
const sessionRouter: AnonymousRouter = new Router();
sessionRouter.use(koaLogSession(provider));
sessionRoutes(sessionRouter, provider);
2021-06-27 20:44:05 +08:00
const managementRouter: AuthedRouter = new Router();
managementRouter.use(koaAuth(UserRole.Admin));
applicationRoutes(managementRouter);
settingRoutes(managementRouter);
connectorRoutes(managementRouter);
resourceRoutes(managementRouter);
signInExperiencesRoutes(managementRouter);
adminUserRoutes(managementRouter);
logRoutes(managementRouter);
roleRoutes(managementRouter);
dashboardRoutes(managementRouter);
customPhraseRoutes(managementRouter);
const anonymousRouter: AnonymousRouter = new Router();
phraseRoutes(anonymousRouter, provider);
wellKnownRoutes(anonymousRouter, provider);
statusRoutes(anonymousRouter);
authnRoutes(anonymousRouter);
// The swagger.json should contain all API routers.
swaggerRoutes(anonymousRouter, [sessionRouter, managementRouter, anonymousRouter]);
return [sessionRouter, managementRouter, anonymousRouter];
};
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
}