forked from elliotBraem/efizzybot
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.ts
More file actions
55 lines (45 loc) · 1.4 KB
/
app.ts
File metadata and controls
55 lines (45 loc) · 1.4 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
import { Hono } from "hono";
import { cors } from "hono/cors";
import { secureHeaders } from "hono/secure-headers";
import { db } from "./db";
import { apiRoutes } from "./routes/api";
import { AppInstance, Env } from "./types/app";
import { getAllowedOrigins } from "./utils/config";
import { errorHandler } from "./utils/error";
import { ServiceProvider } from "./utils/service-provider";
import { logger } from "utils/logger";
import { createAuthMiddleware } from "./middlewares/auth.middleware";
const ALLOWED_ORIGINS = getAllowedOrigins();
export async function createApp(): Promise<AppInstance> {
ServiceProvider.initialize();
const sp = ServiceProvider.getInstance();
const app = new Hono<Env>();
app.onError((err, c) => {
return errorHandler(err, c, logger);
});
app.use(
"*",
cors({
origin: (origin) => {
// Check if origin is in the allowed list
if (ALLOWED_ORIGINS.includes(origin)) {
return origin;
}
// Otherwise, allow same-origin requests (frontend)
return origin;
},
allowMethods: ["GET", "POST"],
}),
);
app.use("*", secureHeaders());
app.use("*", async (c, next) => {
c.set("db", db);
c.set("sp", sp);
await next();
});
// Apply auth middleware to all /api routes
app.use("/api/*", createAuthMiddleware());
// Mount API routes
app.route("/api", apiRoutes);
return { app };
}