MCPcopy Create free account
hub / github.com/MCSManager/MCSManager / middleware

Function middleware

panel/src/app/middleware/protocol.ts:8–99  ·  view source on GitHub ↗
(
  ctx: Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, any>,
  next: Function
)

Source from the content-addressed store, hash-verified

6
7// Define standard response data format middleware
8export async function middleware(
9 ctx: Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, any>,
10 next: Function
11): Promise<void> {
12 // Increase the number of interface requests
13 if (ctx.url.startsWith("/api/")) {
14 VisualDataSubsystem.addRequestCount();
15 }
16
17 // Compatible with version 9.X API parameters
18 if (ctx.query?.remote_uuid) ctx.query.daemonId = ctx.query.remote_uuid;
19 if (ctx.query?.daemon_id) ctx.query.daemonId = ctx.query.daemon_id;
20
21 // Pass the next middleware, any errors and return data will be processed according to the response protocol
22 try {
23 await next();
24 } catch (error: any) {
25 ctx.body = error;
26 }
27
28 if (systemConfig?.crossDomain) {
29 ctx.response.set("Access-Control-Allow-Origin", "*");
30 ctx.response.set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");
31 ctx.response.set(
32 "Access-Control-Allow-Headers",
33 "Content-Type, Content-Length, Authorization, Accept, X-Requested-With"
34 );
35 }
36 ctx.response.set("X-Powered-By", "MCSManager");
37 ctx.response.set("X-Version", getVersion());
38
39 // Serialize and display when sending Error class
40 if (ctx.body instanceof Error) {
41 const error = ctx.body as Error;
42 ctx.status = 500;
43 ctx.body = JSON.stringify({
44 status: ctx.status,
45 data: error.message,
46 time: new Date().getTime()
47 });
48 return;
49 }
50
51 // release all data streams
52 if (ctx.body instanceof Stream) {
53 return;
54 }
55
56 // 404 error code
57 if (ctx.status == 404) {
58 ctx.status = 404;
59 ctx.body = JSON.stringify({
60 status: ctx.status,
61 data: "[404] Not Found",
62 time: new Date().getTime()
63 });
64 return;
65 }

Callers

nothing calls this directly

Calls 3

getVersionFunction · 0.90
addRequestCountMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected