-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (49 loc) · 1.24 KB
/
index.ts
File metadata and controls
64 lines (49 loc) · 1.24 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
import { Hono } from "hono";
import { bodyLimit } from "hono/body-limit";
import { cors } from "hono/cors";
import { logger } from "hono/logger";
import { secureHeaders } from "hono/secure-headers";
import { env } from "hono/adapter";
import { HTTPException } from "hono/http-exception";
import { ERR_BODY_TOO_LARGE, ERR_GENERIC, ERR_SERVER } from "@/lib/error/static";
/**
* The main Hono server instance.
*/
const app = new Hono();
/**
* === MIDDLEWARES ===
*/
app.use(logger());
app.use(secureHeaders());
app.use(cors({
origin(origin, c) {
return env(c).CORS_ORIGIN;
},
allowMethods: ["GET", "HEAD", "POST"]
}));
app.use(bodyLimit({
maxSize: 4096, // 4 KiB
onError(c) {
return c.json(ERR_BODY_TOO_LARGE, 413);
}
}));
/**
* Error handler.
*/
app.onError((error, c) => {
if (!(error instanceof HTTPException)) {
console.error(error);
return c.json(ERR_SERVER, 500);
}
const response = error.getResponse();
if (response.headers.has("Content-Type")) {
return response;
}
return c.json(
error.message ? { ...ERR_GENERIC, message: error.message } : ERR_GENERIC,
error.status || 500
);
});
app.notFound((c) => c.body(null, 404));
app.all("/health", (c) => c.text("OK", 200));
export default app;