-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
58 lines (50 loc) · 1.36 KB
/
bootstrap.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
const express = require("express");
const basicAuth = require("express-basic-auth");
const path = require("path");
const os = require("os");
const ip = require("ip");
const open = require("open");
const serveIndex = require("./serve-index");
const config = require(path.join(os.homedir(), "./.swsrc.json"));
const app = express();
const address = ip.address();
const port = config.port;
const public = path.join(os.homedir(), config.public);
app.use(
basicAuth({
users: config.users,
challenge: true,
})
);
app.use("/", (req, res, next) => {
if (req.auth.user === "admin") {
next();
return;
}
const permissionDir = req.path.substring(1, req.path.indexOf("/", 1));
if (permissionDir !== "/" && permissionDir !== req.auth.user) {
res.sendStatus(403);
} else {
next();
}
});
app.use(
"/",
express.static(public),
serveIndex(public, {
icons: true,
hidden: false,
view: "details",
filter(filename, index, files, dir, req) {
if (req.auth.user === "admin") return true;
const permissionDir = req.path.substring(1, req.path.indexOf("/", 1));
if (permissionDir === "/" && filename === req.auth.user) return true;
if (permissionDir === req.auth.user) return true;
return false;
},
})
);
app.listen(port, () => {
console.log(`app listening at http://${address}`);
open(`http://${address}`);
});