-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (57 loc) · 2.02 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
const express = require("express");
const path = require("path");
const { getCompetitionMessage, getCompetitionFbMessage, getMarkdownMessage } = require("./my_app"); // Logic methods
const { RestrictionError } = require("./utils/errors");
const app = express();
const port = 3000;
// Set view engine
app.set("view engine", "ejs");
// Set views directory
app.set("views", path.join(__dirname, "views"));
// Middleware to serve static files (CSS, JS, etc.)
app.use("/static", express.static(path.join(__dirname, "static")));
// Middleware to parse JSON and URL-encoded form data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Route to serve the index.html
app.get("/", (req, res) => {
res.render("index");
});
app.get("/admin", (req, res) => {
res.render("login", { error: "Invalid credentials!" });
});
app.post("/admin", (req, res) => {
const { username, password } = req.body;
// Validate against environment variables
if (
username === process.env.ADMIN_ID &&
password === process.env.ADMIN_PASSWORD
) {
res.render("admin"); // Render the admin page on success
} else {
res.render("loginerror"); // Send an empty response for incorrect credentials
}
});
// Route to handle the POST request
app.post("/result", async (req, res) => {
try {
const competitionUrl = req.body.competition_url;
const isAdmin = req.body.is_admin === 'true';
const compWhatsAppMessage = await getCompetitionMessage(competitionUrl, isAdmin);
const compFbMessage = await getCompetitionFbMessage(competitionUrl, isAdmin);
const compMarkdownMessage = await getMarkdownMessage(competitionUrl, isAdmin);
res.render("result", {
message: compWhatsAppMessage,
fbMessage: compFbMessage,
markdownMessage: compMarkdownMessage
});
} catch (error) {
console.error("Unexpected error:", error);
res.status(500).send("An unexpected error occurred.");
}
}
);
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});