-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
175 lines (141 loc) · 4.31 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const os = require("os");
const fs = require("fs");
const express = require("express");
const { Deta } = require("deta");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 8080;
const deta = Deta();
const db = deta.Base(process.env.DATABASE_NAME);
// Static files
app.use("/public", express.static(__dirname + "/public"));
// Body Parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Routes
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/html/index.html");
});
app.get("/admin", (req, res) => {
res.sendFile(__dirname + "/public/html/admin.html");
});
app.get("/robots.txt", (req, res) => {
res.sendFile(__dirname + "/public/robots.txt");
});
app.get("/api/catchUpLink", async (req, res) => {
const config = await db.get(process.env.DATABASE_OBJ_KEY);
res.send(config.value);
});
app.get("/summary", (req, res) => {
res.sendFile(__dirname + "/public/html/summary/combined-summary.html");
});
app.get("/summaries", (req, res) => {
res.redirect("/summary");
});
app.get("/summary/:catchupNumber", (req, res) => {
let catchupNumber = req.params.catchupNumber.toLowerCase();
if (catchupNumber === "latest" || catchupNumber === "random") {
// get summary numbers in descending order
let sortedCatchupNumbers = fs
.readdirSync(__dirname + `/public/html/summary`)
.map((file) => parseInt(file.replace(/\.html/, "")))
.filter((number) => !Number.isNaN(number))
.sort((a, b) => b - a);
let index = -1;
if (catchupNumber === "latest") index = 0;
else if (catchupNumber === "random")
index = Math.floor(Math.random() * sortedCatchupNumbers.length);
res.redirect(`/summary/${sortedCatchupNumbers[index]}`);
return;
}
let parsedCatchupNumber = parseInt(catchupNumber).toString();
let normalizedCatchupNumber = parsedCatchupNumber.padStart(3, "0");
const path =
__dirname + `/public/html/summary/${normalizedCatchupNumber}.html`;
if (fs.existsSync(path)) {
// if entered path is not canonical, redirect to the canonical path
if (catchupNumber !== parsedCatchupNumber)
res.redirect(`/summary/${parsedCatchupNumber}`);
else res.sendFile(path);
} else res.status(404).sendFile(__dirname + "/public/html/404.html");
});
app.get("/showcase", (req, res) => {
res.sendFile(__dirname + "/public/html/project-showcase-form.html");
});
app.get("/cfp", (req, res) => {
res.redirect("https://talks.ourtech.community");
});
app.get("/ideas", (req, res) => {
res.redirect(
"https://docs.google.com/document/d/1K67G2_0w3-MEOcrakJrvStwHDz4zhlSliycHDa5oTGk/edit?usp=sharing"
);
});
function auth(req, res) {
const authHeader = req.headers.authorization;
if (!authHeader) {
res.status(401);
res.setHeader("WWW-Authenticate", "Basic");
res.end();
return false;
}
let [username, password] = new Buffer.from(
authHeader.split(" ")[1],
"base64"
)
.toString()
.split(":");
if (
username === process.env.ADMIN_USERNAME &&
password === process.env.ADMIN_PASSWORD
)
return true;
else {
res.status(401);
res.setHeader("WWW-Authenticate", "Basic");
res.end();
return false;
}
}
app.post("/api/catchUpLink", async (req, res) => {
if (!auth(req, res)) return;
const link = req.body.catchUpLink;
try {
const url = new URL(link);
} catch (e) {
res.status(400).send({
error: `Error: Could not set CatchUp link ${link}: ${e.message}`
});
return;
}
let config = {};
config.catchUpLink = link;
config.lastUpdated = new Date().toISOString();
config = JSON.stringify(config);
await db.put(config, process.env.DATABASE_OBJ_KEY);
res.send(`Meet link changed to ${link}.`);
});
app.get("/attend", async (req, res) => {
let date = new Date(
new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" })
);
let day = date.getDay(); // 6 is Saturday, 0 is Sunday
let redirectUrl = "/?isCatchUpOn=false";
if (day === 6 || day === 0) {
// 6 is Saturday, 0 is Sunday
let config = await db.get(process.env.DATABASE_OBJ_KEY);
config = JSON.parse(config.value);
redirectUrl = config.catchUpLink;
}
res.redirect(redirectUrl);
});
app.get("*", (req, res) => {
res.status(404).sendFile(__dirname + "/public/html/404.html");
});
module.exports = app;
app.listen(port, (err) => {
if (err) console.log(err);
else
console.log(
`Server started on port ${port}...\nAccess the web app at http://localhost:${port}`
);
});