-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (62 loc) · 1.92 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
import express from "express";
import es6Renderer from "express-es6-template-engine";
import micros from "./micros/index.js";
import buildMicro from "./libs/buildMicro.js";
import fetch from "node-fetch";
const PORT = process.env.PORT ?? 3000;
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.engine("html", es6Renderer);
app.set("views", "views");
app.set("view engine", "html");
const singlePageMiddleware = (req, res, next) => {
// If the page is requested by the browser, send the index,
// if requested by request, send only the relevant part.
const fromBrowser = req.get("accept").includes("text/html");
if (fromBrowser) {
res.render("index", {
locals: { micros }
});
} else next();
};
app.get("/", (req, res) => {
res.render("index", {
locals: { micros }
});
});
app.get("/docs/:doc", singlePageMiddleware, (req, res) => {
res.render(req.params.doc);
});
app.get("/micros", singlePageMiddleware, (req, res) => {
res.render("allMicros", {
locals: {
micros
}
});
});
app.get("/micros/:micro", singlePageMiddleware, async (req, res) => {
const micro = micros.find((micro) => micro.file === req.params.micro);
const userReq = await fetch(`https://api.github.com/users/${micro.authorGitHub}`);
const author = await userReq.json();
res.render("micro", {
locals: { micro, author }
});
});
app.post("/micros/run/:microFile", async (req, res) => {
const token = req.get("x-api-key") === "null" ? process.env.BROWSERLESS_API_KEY : req.get("x-api-key");
const microFile = req.params.microFile;
let data = null;
try {
const micro = await buildMicro(microFile, token);
data = await micro(req.body);
} catch(err) {
let output = err.code === "ENOENT" ? "Micro not found" : err.toString();
data = {
status: false,
output
}
}
res.json(data);
});
app.listen(PORT, () => console.log("App listening on", PORT));