-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
98 lines (80 loc) · 2.8 KB
/
server.ts
File metadata and controls
98 lines (80 loc) · 2.8 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
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
93
94
95
96
97
98
/*
# AI Assistance Disclosure:
# Tool: ChatGPT (model: GPT‑5), Claude 4.5 Sonnet
# Scope:
# - Boiler plate code, types
# Author review:
# - Verify through running
# - Read the code
*/
import express from "express";
import cors from "cors";
import { toNodeHandler } from "better-auth/node";
import {closeDatabase, connectToDatabase } from "./lib/db";
import { auth } from "./lib/auth";
import userRouter from "./routes/route";
import authRouter from "./routes/authRoute";
const app = express();
const PORT: number = process.env.AUTH_PORT ? parseInt(process.env.AUTH_PORT, 10) : 8000;
const allowedOrigins = ['http://localhost:5173', 'http://localhost', 'http://localhost:80'];
interface CorsOriginCallback {
(err: Error | null, allow?: boolean): void;
}
interface CustomCorsOptions {
origin: (origin: string | null | undefined, callback: CorsOriginCallback) => void;
credentials: boolean;
}
const corsOptions: CustomCorsOptions = {
origin: function (origin: string | null | undefined, callback: CorsOriginCallback) {
// allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
credentials: true,
};
app.use(cors(corsOptions));
app.all('/api/auth/{*any}', toNodeHandler(auth));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use("/api/v1/users", userRouter);
app.use("/api/jwt", authRouter);
app.get("/health", (req: express.Request, res: express.Response) => {
res.status(200).json({ status: "ok", message: "Server is running" });
});
app.use((req: express.Request, res: express.Response) => {
res.status(404).json({ error: "Route not found" });
});
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
console.error(err.stack);
res.status(500).json({ error: "Something went wrong!" });
});
async function startServer() {
try {
// Connect to MongoDB for profile service
await connectToDatabase();
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT}`);
console.log(`API available at http://localhost:${PORT}/api/v1`);
console.log(`Auth endpoints at http://localhost:${PORT}/api/auth`);
});
} catch (error) {
console.error("Failed to start server:", error);
process.exit(1);
}
}
process.on("SIGINT", async () => {
console.log("\nShutting down gracefully...");
await closeDatabase();
process.exit(0);
});
process.on("SIGTERM", async () => {
console.log("\nShutting down gracefully...");
await closeDatabase();
process.exit(0);
});
startServer();
export default app;