/
stencila.js
59 lines (54 loc) · 1.71 KB
/
stencila.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
const path = require("path");
const fs = require("fs");
const darServer = require("dar-server");
const express = require("express");
const logging = require("morgan");
const mustache = require("mustache");
const port = parseInt(process.env.STENCILA_PORT || "4000");
const archiveDir = process.env.STENCILA_ARCHIVE_DIR || process.env.HOME;
var archive = process.env.STENCILA_ARCHIVE;
if (!archive) {
for (var name of fs.readdirSync(archiveDir)) {
if (fs.lstatSync(path.join(archiveDir, name)).isDirectory()) {
archive = name;
break;
}
}
}
archive = archive || "manuscript";
const baseUrl = process.env.BASE_URL || "/";
const serverUrl = baseUrl + "stencila";
const server = express();
darServer.serve(server, {
port: port,
serverUrl: serverUrl,
rootDir: archiveDir,
apiUrl: "/archives"
});
// check for local node_modules
let node_modules = path.join(__dirname, "node_modules");
if (!fs.existsSync(node_modules)) {
node_modules = path.dirname(path.resolve(__dirname));
}
let stencilaDist = path.join(node_modules, "stencila", "dist");
console.log("Stencila app root: %s", stencilaDist);
console.log("DAR archive path: %s", archiveDir);
console.log("DAR public URL: %s", serverUrl);
console.log("Serving stencila on :%s", port);
server.use(logging("dev"));
console.log(stencilaDist);
server.use("/stencilaDist", express.static(stencilaDist));
server.get("/app.js", (req, res) => {
const appJs = path.join(__dirname, "app.js");
fs.readFile(appJs, (err, content) => {
res.append("Content-Type", "application/javascript");
res.send(
mustache.render(content.toString(), {
archive: archive
})
);
res.end();
});
});
server.use("/", express.static(__dirname));
server.listen(port);