-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
73 lines (66 loc) · 1.69 KB
/
app.ts
File metadata and controls
73 lines (66 loc) · 1.69 KB
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
import { Application } from "https://deno.land/x/[email protected]/mod.ts";
import router from "./routes/index.ts";
import {
MemoryStore,
Session,
} from "https://deno.land/x/[email protected]/mod.ts";
import config from "./config/config.json" assert { type: "json" };
import { setResponseBody } from "./utils/util.ts";
export type AppState = {
session: Session;
};
const app = new Application<AppState>();
app.use(async (ctx, next) => {
try {
await next();
if (ctx.response.status >= 400 || ctx.response.status < 200) {
console.log(ctx.request.url.toString());
}
} catch (err) {
console.error("app catch", err);
setResponseBody(ctx, 500, undefined, err.message);
}
});
app.use(
//@ts-ignore
Session.initMiddleware(new MemoryStore(), {
cookieSetOptions: {
httpOnly: true,
},
}),
);
app.use(async (ctx, next) => {
await next();
if (config.allowedHost.includes("*")) {
ctx.response.headers.set(
"Access-Control-Allow-Origin",
"*",
);
} else {
const origin = ctx.request.headers.get("Origin");
if (origin && config.allowedHost.includes(origin)) {
ctx.response.headers.set(
"Access-Control-Allow-Origin",
origin,
);
}
}
ctx.response.headers.set(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, DELETE",
);
ctx.response.headers.set(
"Access-Control-Allow-Headers",
"Content-Type",
);
ctx.response.headers.set(
"Access-Control-Allow-Credentials",
"true",
);
});
app.use(router.routes());
app.use(router.allowedMethods());
app.addEventListener("listen", () => {
console.log("serve is listen on http://localhost:7000");
});
await app.listen({ port: 7000 });