forked from patheticGeek/gdrive-index
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (36 loc) · 1.46 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
const compression = require("compression");
const express = require("express");
const logger = require("./utils/logger");
const parseCookies = require("./utils/parseCookies");
const api = require("./routes/api");
require("dotenv").config();
const dev = process.env.NODE_ENV !== "production";
const PORT = parseInt(process.env.PORT, 10) || 3000;
const { AUTH } = dev ? require("./config.json") : process.env;
const basicauth = Buffer.from(AUTH).toString("base64");
logger(`Dev: ${dev}\nAuth: ${!!AUTH}`);
const server = express();
function checkAuth(req) {
if (!AUTH) return true;
if (!req.headers.cookie) return false;
const cookies = parseCookies(req.headers.cookie);
return cookies.basicauth === basicauth;
}
server.use(compression());
server.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
next();
});
server.get("/ping", (req, res) => res.send("pong"));
server.use("/logs", (req, res) => res.sendFile("logs.txt", { root: __dirname }));
server.use("/api", api);
server.use("/static", express.static("web/build/static"));
server.get("/checkAuth", (req, res) => {
res.send({ auth: req.query.basicauth === basicauth });
});
server.get("/", (req, res) => {
res.sendFile("web/build/index.html", { root: __dirname });
});
server.all("*", (req, res) => res.sendFile("web/build/index.html", { root: __dirname }));
server.listen(PORT, () => logger(`> Listining on http://localhost:${PORT}`));