forked from srm-kzilla/kzilla.xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
174 lines (154 loc) · 4.47 KB
/
server.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import express from "express";
import helmet from "helmet";
import rateLimit from "express-rate-limit";
import cookieParser from "cookie-parser";
import cors from "cors";
import { config } from "dotenv";
import { mw } from "request-ip";
import { catchAllRoutes } from "./api/controllers/link-controller";
import { Constants, APIEndpoints } from "./api/constants";
import linksRoute, { fetchLink } from "./api/routes/link-routes";
import analyticsRoutes from "./api/routes/analytics-routes";
import webhookRoutes from "./api/routes/webhook-routes";
import appRoutes from "./api/routes/app-routes";
import { AnalyticsService } from "./api/services/analytics-service";
import { DatabaseService } from "./api/services/database-service";
import { recaptchaMiddleware } from "./api/middlewares/recaptcha-middleware";
import { generateTooManyRequests } from "./api/utils/ejs-templates";
const { expressCspHeader, INLINE, NONE, SELF } = require("express-csp-header");
import path from "path";
/**
* Driver class to run the application
*/
class Server {
/**
* An instance of Express application
*/
public app: express.Application;
constructor() {
config();
this.app = express();
this.initializeDatabase();
this.mountMiddlewares();
this.mountPublicRoutes();
this.mountSecuredRoutes();
this.initializeServer();
AnalyticsService.getInstance();
}
/**
* Mounts middleswares into the Express instance
*/
private mountMiddlewares() {
this.app.use(cors());
this.app.use(express.static("public"));
this.app.use(helmet());
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: true }));
this.app.use(cookieParser());
this.app.use(mw());
this.app.use(
expressCspHeader({
directives: {
"default-src": [SELF, INLINE],
"script-src": [
SELF,
INLINE,
"*.googletagmanager.com",
"*.unpkg.com",
"*.bootstrapcdn.com",
"*.cloudflare.com",
"*.jquery.com",
"*.google.com",
"*",
],
"style-src": [
SELF,
INLINE,
"*",
"*.cloudflare.com",
"*.bootstrapcdn.com",
],
"img-src": ["data:", "*", SELF, INLINE],
"worker-src": [NONE],
"frame-src": ["*.google.com", "*"],
"font-src": ["*.cloudflare.com", "*"],
"connect-src": ["*"],
"block-all-mixed-content": false,
},
})
);
}
/**
* Mounts unsecured, public routes into the Express instance
*/
private async mountPublicRoutes() {
const page429 = await generateTooManyRequests();
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000,
max: 60,
message: page429,
});
const appLimiter = rateLimit({
windowMs: 1 * 60 * 1000,
max: 40,
message: "Too many requests from this IP, please try again later.",
});
this.app.use(
`${APIEndpoints.BASE}${APIEndpoints.Webhook.BASE_ENDPOINT}`,
apiLimiter,
webhookRoutes
);
this.app.use(
`${APIEndpoints.BASE}${APIEndpoints.App.BASE_ENDPOINT}`,
appLimiter,
appRoutes
);
if (Constants.NODE_ENV !== "development") {
this.app.post("*", recaptchaMiddleware);
this.app.put("*", recaptchaMiddleware);
}
this.app.get(APIEndpoints.Links.GET_LINK, apiLimiter, fetchLink);
this.app.use(
`${APIEndpoints.BASE}${APIEndpoints.Links.BASE_ENDPOINT}`,
apiLimiter,
linksRoute
);
this.app.use(
`${APIEndpoints.BASE}${APIEndpoints.Analytics.BASE_ENDPOINT}`,
apiLimiter,
analyticsRoutes
);
this.app.use(
"/",
express.static(path.join(__dirname, "..", "client", "public"))
);
this.app.use(
"/analytics/:analyticsId",
express.static(path.join(__dirname, "..", "client", "public"))
);
this.app.use(
"/me",
express.static(path.join(__dirname, "..", "client", "public"))
);
this.app.all("*", catchAllRoutes);
}
/**
* Mounts secured routes into the Express instance
*/
private mountSecuredRoutes() {}
/**
* Establish a connection with the database
*/
private initializeDatabase() {
DatabaseService.getInstance();
}
/**
* Starts a server of the Express instance
*/
private initializeServer() {
this.app.listen(Constants.PORT, () => {
console.log(`Server started on ${Constants.PORT}.`);
});
}
}
new Server();