From e58b839339c467cea43c51419fb2869939117088 Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Sat, 11 Feb 2023 00:33:01 +0100 Subject: [PATCH] split routers --- packages/backend/src/server/api/index.ts | 31 ++++++++++++++---------- packages/backend/src/server/index.ts | 9 +++++-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/server/api/index.ts b/packages/backend/src/server/api/index.ts index 1096d995b..e2a0257e6 100644 --- a/packages/backend/src/server/api/index.ts +++ b/packages/backend/src/server/api/index.ts @@ -19,19 +19,11 @@ import signupPending from "./private/signup-pending.js"; import discord from "./service/discord.js"; import github from "./service/github.js"; import twitter from "./service/twitter.js"; +import koaBody from "koa-body"; // Init app const app = new Koa(); -// Init multer instance -const upload = multer({ - storage: multer.diskStorage({}), - limits: { - fileSize: config.maxFileSize || 262144000, - files: 1, - }, -}); - app.use( cors({ origin: "*", @@ -44,7 +36,20 @@ app.use(async (ctx, next) => { await next(); }); -app.use( +// Init router +const router = new Router(); +const mastoRouter = new Router(); + +// Init multer instance +const upload = multer({ + storage: multer.diskStorage({}), + limits: { + fileSize: config.maxFileSize || 262144000, + files: 1, + }, +}); + +router.use( bodyParser({ // リクエストが multipart/form-data でない限りはJSONだと見なす detectJSON: (ctx) => @@ -55,10 +60,9 @@ app.use( }), ); -// Init router -const router = new Router(); +mastoRouter.use(koaBody()); -apiMastodonCompatible(router); +apiMastodonCompatible(mastoRouter); /** * Register endpoint handlers @@ -150,5 +154,6 @@ router.all("(.*)", async (ctx) => { // Register router app.use(router.routes()); +app.use(mastoRouter.routes()); export default app; diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index 6609627fe..a3cdad438 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -29,6 +29,7 @@ import fileServer from "./file/index.js"; import proxyServer from "./proxy/index.js"; import webServer from "./web/index.js"; import { initializeStreamingServer } from "./api/streaming.js"; +import koaBody from "koa-body"; export const serverLogger = new Logger("server", "gray", false); @@ -69,6 +70,9 @@ app.use(mount("/proxy", proxyServer)); // Init router const router = new Router(); +const mastoRouter = new Router(); + +mastoRouter.use(koaBody()); // Routing router.use(activityPub.routes()); @@ -134,13 +138,13 @@ router.get("/verify-email/:code", async (ctx) => { } }); -router.get("/oauth/authorize", async (ctx) => { +mastoRouter.get("/oauth/authorize", async (ctx) => { const client_id = ctx.request.query.client_id; console.log(ctx.request.req); ctx.redirect(Buffer.from(client_id?.toString() || '', 'base64').toString()); }); -router.post("/oauth/token", async (ctx) => { +mastoRouter.post("/oauth/token", async (ctx) => { const body: any = ctx.request.body; const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; const generator = (megalodon as any).default; @@ -167,6 +171,7 @@ router.post("/oauth/token", async (ctx) => { // Register router app.use(router.routes()); +app.use(mastoRouter.routes()); app.use(mount(webServer));