-
Notifications
You must be signed in to change notification settings - Fork 0
/
express-entry.ts
92 lines (75 loc) · 2.66 KB
/
express-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
81
82
83
84
85
86
87
88
89
90
91
92
import "dotenv/config";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import express from "express";
import session from "express-session";
import {
createHandler,
createMiddleware,
} from "@universal-middleware/express";
import { dbMiddleware } from "./server/db-middleware";
import {
luciaAuthContextMiddleware,
luciaAuthCookieMiddleware,
luciaAuthLoginHandler,
luciaAuthLogoutHandler,
luciaAuthSignupHandler,
luciaCsrfMiddleware,
luciaDbMiddleware,
} from "./server/lucia-auth-handlers";
import { tigerBeetleMiddleware } from "./server/tigerbeetle-middleware";
import { tsRestHandler } from "./server/ts-rest-handler";
import { vikeHandler } from "./server/vike-handler";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const root = __dirname;
const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
const hmrPort = process.env.HMR_PORT ? parseInt(process.env.HMR_PORT, 10) : 24678;
console.log("privkey", process.env.PRIVATE_KEY);
export default (await startServer()) as unknown;
async function startServer() {
const app = express();
if (process.env.NODE_ENV === "production") {
app.use(express.static(`${root}/dist/client`));
} else {
// Instantiate Vite's development server and integrate its middleware to our server.
// ⚠️ We should instantiate it *only* in development. (It isn't needed in production
// and would unnecessarily bloat our server in production.)
const vite = await import("vite");
const viteDevMiddleware = (
await vite.createServer({
root,
server: { middlewareMode: true, hmr: { port: hmrPort } },
})
).middlewares;
app.use(viteDevMiddleware);
}
app.use(createMiddleware(dbMiddleware)());
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
cookie: { secure: true },
}),
);
app.use(createMiddleware(luciaDbMiddleware)());
app.use(createMiddleware(luciaCsrfMiddleware)());
app.use(createMiddleware(luciaAuthContextMiddleware)());
app.use(createMiddleware(luciaAuthCookieMiddleware)());
app.use(createMiddleware(tigerBeetleMiddleware)());
app.post("/api/signup", createHandler(luciaAuthSignupHandler)());
app.post("/api/login", createHandler(luciaAuthLoginHandler)());
app.post("/api/logout", createHandler(luciaAuthLogoutHandler)());
app.all("/api/*", createHandler(tsRestHandler)());
/**
* Vike route
*
* @link {@see https://vike.dev}
**/
app.all("*", createHandler(vikeHandler)());
app.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`);
});
return app;
}