-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
96 lines (85 loc) · 2.36 KB
/
Copy pathindex.ts
File metadata and controls
96 lines (85 loc) · 2.36 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
// Copyright (c) 2025 Daniel Marques
// Licensed under the GNU AGPL v3. See LICENSE.
import express, { Application } from "express";
import dotenv from "dotenv";
import topicRoutes from "./routes/topics";
import usersRoutes from "./routes/user";
import { runDB } from "./utils/dbConnect";
import cors from "cors";
import { limiter } from "./utils/limiter";
import helmet from "helmet";
import { ensureTopicStorage } from "./utils/topicStorageMaintenance";
import { ensureUserStorage } from "./utils/userStorageMaintenance";
dotenv.config();
const app: Application = express();
const port = process.env.PORT || 8000;
app.use(
cors({
origin: [
"http://localhost:3000",
"https://learninfive.dev",
"https://www.learninfive.dev",
"https://learninfive.com",
"https://www.learninfive.com"
],
credentials: true,
})
);
app.use(express.json());
app.use(limiter);
if (process.env.NODE_ENV === "production") {
app.use(
helmet({
dnsPrefetchControl: { allow: false },
xContentTypeOptions: true,
frameguard: { action: "sameorigin" },
hsts: {
maxAge: 60 * 60 * 24 * 365,
includeSubDomains: true,
preload: true,
},
referrerPolicy: { policy: "no-referrer" },
crossOriginEmbedderPolicy: true,
crossOriginOpenerPolicy: { policy: "same-origin" },
crossOriginResourcePolicy: { policy: "same-origin" },
})
);
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: [
"'self'",
"https://learninfive.dev",
"https://www.learninfive.dev",
"http://localhost:3000",
],
scriptSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:"],
connectSrc: ["'self'"],
fontSrc: [
"'self'",
"https://fonts.googleapis.com",
"https://fonts.gstatic.com",
],
objectSrc: ["'none'"],
upgradeInsecureRequests: [],
},
})
);
}
app.use(limiter);
app.use("/topics", topicRoutes);
app.use("/users", usersRoutes);
const startServer = async () => {
await runDB();
await ensureTopicStorage();
await ensureUserStorage();
app.listen(port, () => {
console.log(`Server is listening at http://localhost:${port}`);
});
};
startServer().catch((error) => {
console.error(error);
process.exit(1);
});