forked from leosilvadev/avrodoc-plus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
64 lines (55 loc) · 1.79 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
import express from "express";
import morgan from "morgan";
import lessMiddleware from "less-middleware";
import http from "http";
import path from "path";
import glob from "glob";
import fs from "fs";
import { dustTemplates, topLevelHTML } from "./src/static_content.js";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const schema_dir = path.resolve(
process.cwd(),
process.env.SCHEMA_DIR ?? "schemata"
);
const schemata = [];
glob("**/*.avsc", { cwd: schema_dir }, function (err, files) {
if (err) throw err;
files.sort().forEach(function (file) {
schemata.push({ filename: "/schemata/" + file });
});
});
// Precompile dust templates at app startup, and then serve them out of memory
const dust_templates = dustTemplates();
const app = express();
app.set("port", process.env.PORT ?? 8080);
app.use(morgan("combined"));
app.use(express.json());
app.use(lessMiddleware(__dirname + "/public"));
app.use(express.static(path.join(__dirname, "public")));
app.get("/", async function (req, res) {
const html = await topLevelHTML("Server side Avrodoc", [], {
schemata: schemata,
});
res.set("Content-Type", "text/html").send(html);
});
app.get("/dust-templates.js", function (req, res) {
res.set("Content-Type", "text/javascript").send(dust_templates);
});
app.get(/^\/schemata\/(\w[\w.-]*(?:\/\w[\w.-]*)*)$/, function (req, res) {
fs.readFile(
path.join(schema_dir, req.params[0]),
"utf-8",
function (err, data) {
if (err) {
res.status(404).send("Not found");
} else {
res.set("Content-Type", "application/json").send(data);
}
}
);
});
http.createServer(app).listen(app.get("port"), function () {
console.log("Express server listening on port " + app.get("port"));
});