()
| 7 | import logger from "./log"; |
| 8 | |
| 9 | export function initKoa() { |
| 10 | const koaApp = new Koa(); |
| 11 | const config = globalConfiguration.config; |
| 12 | koaApp.use(uploadSpeedLimitMiddleware); |
| 13 | koaApp.use(uploadFileCheckMiddleware); |
| 14 | koaApp.use( |
| 15 | koaBody({ |
| 16 | multipart: true, |
| 17 | formidable: { |
| 18 | maxFileSize: 1024 * 1024 * 100, // 100MB |
| 19 | maxFiles: 1 |
| 20 | }, |
| 21 | jsonLimit: "10mb", |
| 22 | onError(err, ctx) { |
| 23 | logger.error("koaBody Lib Error:", err); |
| 24 | } |
| 25 | }) |
| 26 | ); |
| 27 | // Load Koa top-level middleware |
| 28 | koaApp.use(async (ctx, next) => { |
| 29 | await next(); |
| 30 | // Because all HTTP requests can only be used by creating a task passport on the panel side, cross-domain requests are allowed, and security can also be guaranteed |
| 31 | ctx.response.set("Access-Control-Allow-Origin", "*"); |
| 32 | ctx.response.set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS"); |
| 33 | ctx.response.set( |
| 34 | "Access-Control-Allow-Headers", |
| 35 | "Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " + |
| 36 | "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection" |
| 37 | ); |
| 38 | ctx.response.set("X-Power-by", "MCSManager"); |
| 39 | }); |
| 40 | |
| 41 | if (config.prefix != "") { |
| 42 | const prefix = config.prefix; |
| 43 | koaApp.use(async (ctx, next) => { |
| 44 | if (ctx.url.startsWith(prefix)) { |
| 45 | const orig = ctx.url; |
| 46 | ctx.url = ctx.url.slice(prefix.length); |
| 47 | if (!ctx.url.startsWith("/")) { |
| 48 | ctx.url = "/" + ctx.url; |
| 49 | } |
| 50 | await next(); |
| 51 | ctx.url = orig; |
| 52 | } else { |
| 53 | ctx.redirect(removeTrail(prefix, "/") + ctx.url); |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | koaApp.use(koaRouter.routes()).use(koaRouter.allowedMethods()); |
| 58 | return koaApp; |
| 59 | } |
nothing calls this directly
no test coverage detected