-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·159 lines (134 loc) · 4.85 KB
/
app.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
155
156
157
158
159
require("dotenv").config();
const express = require("express");
const https = require("https");
const http = require("http");
const app = express();
const myKeyPath = process.env.myKey;
const myCertPath = process.env.myCert;
const options = { key: myKeyPath, cert: myCertPath };
const myPort1 = process.env.myPort1;
const myPort2 = process.env.myPort2;
let redirectMiddleware;
try {
redirectMiddleware = require("./redirectMiddleware");
} catch (error) {
console.error("Error loading redirectMiddleware:");
}
const { createPool } = require("mysql2/promise");
const pool = createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
});
https.createServer(options, app).listen(myPort1, () => {});
http.createServer(app).listen(myPort2, () => {});
app.use(express.static("public"));
app.set("view engine", "ejs");
if (redirectMiddleware) {
app.use(redirectMiddleware);
} else console.log("no redirect");
app.get(["/", "/index"], async (req, res) => {
const connection = await pool.getConnection();
const [main] = await connection.query(
"SELECT *, DATE_FORMAT(date, '%d %m %Y') AS formatted_date FROM main ORDER BY date DESC"
);
connection.release();
res.render("index", { main });
});
app.get("/loved", async (req, res) => {
const connection = await pool.getConnection();
const [loved] = await connection.query(
"SELECT *, DATE_FORMAT(date, '%d %m %Y') AS formatted_date FROM love ORDER BY id DESC"
);
connection.release();
res.render("loved", { loved });
});
app.get("/archive", async (req, res) => {
const connection = await pool.getConnection();
const [archive] = await connection.query(
"SELECT *, DATE_FORMAT(date, '%d %m %Y') AS formatted_date FROM archive ORDER BY id DESC"
);
connection.release();
res.render("archive", { archive });
});
app.post("/love/:body", async (req, res) => {
const emailID = req.params.body;
const connection = await pool.getConnection();
await connection.beginTransaction();
const [email] = await connection.query(
"SELECT id, sender, subject, body, date FROM main WHERE body = ?",
[emailID]
);
if (email.length === 0 || !email[0].id) {
// Email not found or id is undefined
connection.release();
res.redirect("/");
return;
}
const { id, sender, subject, body, date } = email[0];
await connection.query(
"INSERT INTO love (sender, subject, body, date) VALUES (?, ?, ?, ?)",
[sender, subject, body, date]
);
await connection.query("DELETE FROM main WHERE body = ?", [emailID]);
await connection.commit();
connection.release();
const lastEmailID = id + 1;
res.redirect(`/#${lastEmailID}`);
});
app.post("/archive/:body", async (req, res) => {
const emailID = req.params.body;
const connection = await pool.getConnection();
await connection.beginTransaction();
const [email] = await connection.query(
"SELECT id FROM main WHERE body = ?",
[emailID]
);
if (email.length === 0 || !email[0].id) {
// Email not found or id is undefined
connection.release();
res.redirect("/");
return;
}
const emailIDInMain = email[0].id;
await connection.query(
"INSERT INTO archive (sender, subject, body, date) SELECT sender, subject, body, date FROM main WHERE body = ?",
[emailID]
);
await connection.query("DELETE FROM main WHERE body = ?", [emailID]);
await connection.query("DELETE FROM love WHERE body = ?", [emailID]);
await connection.commit();
connection.release();
const lastEmailID = emailIDInMain + 1;
res.redirect(`/#${lastEmailID}`);
});
app.post("/archivee/:archiveBody", async (req, res) => {
const emailID = req.params.archiveBody;
const connection = await pool.getConnection();
await connection.beginTransaction();
await connection.query(
"INSERT INTO archive (sender, subject, body, date) SELECT sender, subject, body, date FROM love WHERE body = ?",
[emailID]
);
await connection.query("DELETE FROM love WHERE body = ?", [emailID]);
await connection.commit();
connection.release();
res.redirect("/loved");
});
app.post("/lovee/:loveBody", async (req, res) => {
const emailID = req.params.loveBody;
const connection = await pool.getConnection();
await connection.beginTransaction();
await connection.query(
`INSERT INTO love (sender, subject, body, date) SELECT sender, subject, body, date FROM archive WHERE body = ?`,
[emailID]
);
await connection.query("DELETE FROM archive WHERE body = ?", [emailID]);
await connection.commit();
connection.release();
res.redirect("/archive");
});
app.listen(() => {
console.log(`Server running at http://localhost:3000`);
});