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

66 lines
2.5 KiB
TypeScript
Raw Normal View History

import { UserRole } from '@logto/schemas';
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';
2022-10-21 00:14:17 -05:00
import type { Provider } from 'oidc-provider';
2021-08-29 22:30:54 -05:00
2022-11-21 03:38:24 -05:00
import koaAuth from '#src/middleware/koa-auth.js';
import koaLogSession from '#src/middleware/koa-log-session.js';
import adminUserRoutes from '#src/routes/admin-user.js';
import applicationRoutes from '#src/routes/application.js';
import authnRoutes from '#src/routes/authn.js';
import connectorRoutes from '#src/routes/connector.js';
import customPhraseRoutes from '#src/routes/custom-phrase.js';
import dashboardRoutes from '#src/routes/dashboard.js';
import logRoutes from '#src/routes/log.js';
import phraseRoutes from '#src/routes/phrase.js';
import resourceRoutes from '#src/routes/resource.js';
import roleRoutes from '#src/routes/role.js';
import sessionRoutes from '#src/routes/session/index.js';
import settingRoutes from '#src/routes/setting.js';
import signInExperiencesRoutes from '#src/routes/sign-in-experience.js';
import statusRoutes from '#src/routes/status.js';
import swaggerRoutes from '#src/routes/swagger.js';
import wellKnownRoutes from '#src/routes/well-known.js';
2021-06-27 07:44:05 -05:00
2022-11-21 03:38:24 -05:00
import type { AnonymousRouter, AuthedRouter } from './types.js';
2021-09-01 04:35:23 -05:00
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 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 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
}