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';
|
2021-07-03 08:19:20 -05:00
|
|
|
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';
|
2021-08-15 10:39:03 -05:00
|
|
|
import applicationRoutes from '@/routes/application';
|
2021-08-29 22:30:54 -05:00
|
|
|
import sessionRoutes from '@/routes/session';
|
2021-09-05 21:40:06 -05:00
|
|
|
import statusRoutes from '@/routes/status';
|
2021-08-29 22:30:54 -05:00
|
|
|
import swaggerRoutes from '@/routes/swagger';
|
|
|
|
import userRoutes from '@/routes/user';
|
2021-06-27 07:44:05 -05:00
|
|
|
|
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) => {
|
2021-09-01 04:35:23 -05:00
|
|
|
const anonymousRouter: AnonymousRouter = new Router();
|
2021-06-27 07:44:05 -05:00
|
|
|
|
2021-09-05 21:40:06 -05:00
|
|
|
statusRoutes(anonymousRouter);
|
2021-08-15 10:39:03 -05:00
|
|
|
sessionRoutes(anonymousRouter, provider);
|
2021-09-24 04:00:26 -05:00
|
|
|
userRoutes(anonymousRouter, provider);
|
2021-08-15 10:39:03 -05:00
|
|
|
swaggerRoutes(anonymousRouter);
|
|
|
|
|
2021-09-01 04:35:23 -05:00
|
|
|
const router: AuthedRouter = new Router();
|
2021-08-15 10:39:03 -05:00
|
|
|
router.use(koaAuth());
|
2021-08-14 08:39:37 -05:00
|
|
|
applicationRoutes(router);
|
2021-06-27 07:44:05 -05:00
|
|
|
|
2021-08-15 10:39:03 -05:00
|
|
|
return [anonymousRouter, router];
|
2021-07-03 08:19:20 -05:00
|
|
|
};
|
|
|
|
|
2021-07-29 13:21:47 -05:00
|
|
|
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());
|
|
|
|
}
|
2021-07-29 13:21:47 -05:00
|
|
|
|
|
|
|
app.use(mount('/api', apisApp));
|
2021-06-27 07:44:05 -05:00
|
|
|
}
|