|
1 | | -import { Hono } from "@hono/hono"; |
2 | | -import { HTTPException } from "@hono/hono/http-exception"; |
3 | | -import { logger } from "@hono/hono/logger"; |
4 | | - |
5 | 1 | import config from "./config.ts"; |
6 | | -import { contextMiddleware, getRequestLog } from "./context.ts"; |
7 | | -import { hasKey, verify } from "./crypto.ts"; |
8 | | -import handler from "./handler.ts"; |
9 | | -import { setMetaHeader } from "./util.ts"; |
10 | | - |
11 | | -const app = new Hono(); |
12 | | - |
13 | | -app.use(contextMiddleware); |
14 | | - |
15 | | -// use context's requestlog for server logs |
16 | | -app.use(logger((...args) => getRequestLog().log(...args))); |
17 | | - |
18 | | -// add deploy header to responses |
19 | | -app.use(async (c, next) => { |
20 | | - await next(); |
21 | | - setMetaHeader(c, "deploy", config.deployId); |
22 | | -}); |
23 | | - |
24 | | -if (config.mainRedirect) { |
25 | | - app.get("/", (c) => { |
26 | | - return c.redirect(config.mainRedirect!); |
27 | | - }); |
28 | | -} |
29 | | - |
30 | | -app.post("/:id/:token", async (c) => { |
31 | | - const { id, token } = c.req.param(); |
32 | | - |
33 | | - // verify signature |
34 | | - if (hasKey) { |
35 | | - const signature = c.req.query("sig"); |
36 | | - if (!signature || !(await verify(`${id}/${token}`, signature))) { |
37 | | - throw new HTTPException(403); |
38 | | - } |
39 | | - } |
40 | | - |
41 | | - const data = await c.req.json(); |
42 | | - let [res, meta] = await handler(data, c.req.header(), c.req.query(), id, token); |
43 | | - |
44 | | - // clone response to make headers mutable |
45 | | - res = new Response(res.body, res); |
46 | | - |
47 | | - // set metadata headers |
48 | | - for (const [key, value] of Object.entries(meta)) { |
49 | | - setMetaHeader(c, key, value); |
50 | | - } |
51 | | - |
52 | | - // remove other headers that don't make sense here |
53 | | - for (const header of ["set-cookie", "alt-svc"]) { |
54 | | - res.headers.delete(header); |
55 | | - } |
56 | | - |
57 | | - return res; |
58 | | -}); |
| 2 | +import app from "./server/index.ts"; |
59 | 3 |
|
60 | 4 | if (import.meta.main) { |
61 | 5 | if (config.signKey) { |
|
0 commit comments