-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
154 lines (119 loc) · 4.13 KB
/
index.js
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
import express from "express";
import { createServer } from "http";
import path from "path";
import cors from "cors";
import "dotenv/config";
import mongodb, { redis } from "./config/db.js";
import admin from "./routes/admin.js";
import session from "express-session";
import RedisStore from "connect-redis";
import { fetchBlogPosts, fetchProjectData } from "./utils/fetchData.js";
import { DEV } from "./utils/constant.js";
import { mailQueue } from "./worker.js";
import multer from "multer";
import rateLimit from "express-rate-limit";
import expressLayouts from "express-ejs-layouts";
import project from "./routes/project.js";
import csrf from "csrf";
import cookieParser from "cookie-parser";
import verifyUser from "./middlewares/verifyUser.js";
import service from "./routes/services.js";
import constructFullURL from "./middlewares/middlewares.js";
import { Server } from "socket.io";
import websocket from "./config/websocket.js";
import { createAdapter } from "@socket.io/redis-streams-adapter";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = process.env.PORT || 3000;
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 1000,
message: 'Too many requests from this IP, please try again after 15 minutes.'
});
const app = express();
const server = createServer(app);
app.set("trust proxy", 3)
app.use(cors());
// app.use(limiter);
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(
session({
store: new RedisStore({ client: redis.client, prefix: "quantumweb:" }),
secret: process.env.COOKIE_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: DEV ? false : true, httpOnly: true, sameSite: 'strict' },
})
);
// const upload = multer({ dest: "views/img/uploads/" });
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
app.use(expressLayouts);
app.use(constructFullURL)
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(verifyUser.verifyIp);
app.get("/", async (req, res) => {
const projects = await fetchProjectData();
const blogs = await fetchBlogPosts();
res.render("index", {
hostname: req.get('host'),
url: req.fulUrl,
pageTitle: "Software Engineer | Web Developer | Adeniji Olajide Portfolio",
blogs: blogs.posts,
projects,
});
});
app.get('/shop', async (req, res) => {
res.render("comingsoon", {
layout: 'layouts/empty',
pageTitle: "Software Engineer | Web Developer | Adeniji Olajide Portfolio",
});
})
app.get("/test", (req, res) => {
console.log(req.fulUrl)
res.send("Hello, World!");
})
app.post("/chirpmail", multer().none(), async (req, res) => {
const { name, email, message, password } = req.body;
const host = req.get("host");
// return res.status(400).send("Mail services are currently disabled due to bot infiltration.");
if (!name || !email || !message) {
return res.status(400).send("All fields are required.");
}
const secret = req.session.csrfSecret;
const token = req.body._csrf;
const ip = req.ip;
const userAgent = req.headers['user-agent'];
mailQueue.add({ name, email, message, host, ip, password, userAgent });
res.status(200).send("Chirpmail sent successfully.");
});
app.get('/getblog/:key([0-9]+)', async (req, res) => {
const { key } = req.params;
const blogPost = await fetchBlogPosts(key, true) || new Array();
res.json(blogPost);
})
app.set('layout', 'layouts/layout');
app.use("/admin", admin);
app.use("/project", project);
app.use("/service", service);
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, "404.html"));
});
app.use((req, res) => {
res.status(500).sendFile(path.join(__dirname, "500.html"));
});
server.listen(PORT, () => {
if (process.env.DEV === "true") {
console.log("Running on Development");
}
redis.run().catch(console.dir);
mongodb.run().catch(console.dir);
const io = new Server(server, {
adapter: createAdapter(redis.client)
});
websocket.getConnection(io);
console.log(`Server is running on http://localhost:${PORT}`);
});