-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhono-entry.ts
80 lines (65 loc) · 1.73 KB
/
hono-entry.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
import { serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import { Hono } from "hono";
import { compress } from "hono/compress";
import { poweredBy } from "hono/powered-by";
import { telefunc } from "telefunc";
import { renderPage } from "vike/server";
import authRoute from "~/lib/auth";
import type { Session, User } from "lucia";
const isProduction = process.env.NODE_ENV === "production";
const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
// const app = new Hono();
const app = new Hono<{
Variables: {
user: User | null;
session: Session | null;
};
}>();
app.use(poweredBy());
app.use(compress());
app.route("/", authRoute);
if (isProduction) {
app.use(
"/*",
serveStatic({
root: `dist/client/`,
})
);
}
app.post("/_telefunc", async (c) => {
const httpResponse = await telefunc({
url: c.req.url.toString(),
method: c.req.method,
body: await c.req.text(),
context: c,
});
const { body, statusCode, contentType } = httpResponse;
c.status(statusCode);
c.header("Content-Type", contentType);
return c.body(body);
});
app.all("*", async (c, next) => {
const pageContextInit = {
urlOriginal: c.req.url,
auth: c.get("user"),
};
const pageContext = await renderPage(pageContextInit);
const { httpResponse } = pageContext;
if (!httpResponse) {
return next();
} else {
const { body, statusCode, headers } = httpResponse;
headers.forEach(([name, value]) => c.header(name, value));
c.status(statusCode);
return c.body(body);
}
});
if (isProduction) {
console.log(`Server listening on http://localhost:${port}`);
serve({
fetch: app.fetch,
port: port,
});
}
export default app;