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';
|
2021-07-03 21:19:20 +08:00
|
|
|
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';
|
2022-01-24 14:40:15 +08:00
|
|
|
import connectorRoutes from '@/routes/connector';
|
2022-01-18 09:20:49 +08:00
|
|
|
import resourceRoutes from '@/routes/resource';
|
2021-08-30 11:30:54 +08:00
|
|
|
import sessionRoutes from '@/routes/session';
|
2022-01-13 20:05:14 +08:00
|
|
|
import settingRoutes from '@/routes/setting';
|
2022-01-26 11:07:45 +08:00
|
|
|
import signInExperiencesRoutes from '@/routes/sign-in-experience';
|
2021-09-06 10:40:06 +08:00
|
|
|
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
|
|
|
|
2021-09-06 10:40:06 +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);
|
2022-01-13 20:05:14 +08:00
|
|
|
settingRoutes(router);
|
2022-01-24 14:40:15 +08:00
|
|
|
connectorRoutes(router);
|
2022-01-18 09:20:49 +08:00
|
|
|
resourceRoutes(router);
|
2022-01-26 11:07:45 +08:00
|
|
|
signInExperiencesRoutes(router);
|
2021-06-27 20:44:05 +08:00
|
|
|
|
2021-08-15 23:39:03 +08:00
|
|
|
return [anonymousRouter, router];
|
2021-07-03 21:19:20 +08:00
|
|
|
};
|
|
|
|
|
2021-07-30 02:21:47 +08:00
|
|
|
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());
|
|
|
|
}
|
2021-07-30 02:21:47 +08:00
|
|
|
|
|
|
|
app.use(mount('/api', apisApp));
|
2021-06-27 20:44:05 +08:00
|
|
|
}
|