-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
86 lines (82 loc) · 2.35 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Application, helpers } from "oak/mod.ts";
import { ErrorInterceptor } from "@/middleware/mod.ts";
import router from "@/routers/mod.ts";
import config from "./config/mod.ts";
// import logger from "oak_logger/mod.ts";
import session from "@/helper/session/mod.ts";
import logger from "@/helper/logger/mod.ts";
const app = new Application();
const controller = new AbortController();
const { signal } = controller;
logger.debug("pace logger start working...");
app.keys = config.keys;
app.addEventListener("listen", ({ hostname, port, secure }) => {
Deno.readTextFile("./src/banner.txt").then((response: any) =>
console.info(
response,
config.version,
`Listening on: ${secure ? "https://" : "http://"}${hostname ??
"localhost"}:${port}`,
)
);
});
const errorInsterceptor = new ErrorInterceptor();
app.use(errorInsterceptor.httpErrorInterceptor());
app.use(
session({
// store: redisStore(config.redis),
cookie: {
path: "/",
httpOnly: false,
maxAge: 24 * 60 * 60 * 1000, //one day in ms,
signed: true,
},
}),
);
app.use(async (ctx: Record<string, any>, next) => {
await next();
if (
typeof ctx.request.body === "object" &&
ctx.request.body()?.data !== undefined
) {
ctx.response.type = "json";
ctx.response.body = JSON.stringify(ctx.response.body, undefined, 2);
}
});
//增加query对象
app.use(async (ctx, next) => {
const query = helpers.getQuery(ctx, { mergeParams: true });
logger.debug(`query${JSON.stringify(ctx.request)}`)
Object.defineProperties(ctx.request, {
"query": {
value: query,
writable: false,
},
});
Object.defineProperties(ctx,{
"query":{
value: query,
writable: false,
}
})
await next();
});
//jsonp
app.use(async (ctx: Record<string, any>, next) => {
logger.debug(`当前ctx为${JSON.stringify(ctx.request)}`);
await next();
logger.debug(`当前ctx为${JSON.stringify(ctx.request)}`);
if (ctx.request.query?.callback) {
let body = typeof ctx.response.body === "object"
? JSON.stringify(ctx.response.body, undefined, 2)
: ctx.response.body;
ctx.response.body = ctx.request.query.callback + "(" + body + ")";
ctx.response.type = "application/x-javascript";
}
});
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({
port: config.serve.port,
signal
});