File size: 1,124 Bytes
d605f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import express from "express";
import http from "http";
import * as webEditor from "@k-l-lambda/web-editor";

import {service, setEnvironment} from "./backend";
import * as dirServer from "./backend/dirServer";



const app = express();


// refuse direct access to .html files
app.use("*.html", (_, res) => {
	res.writeHead(404);
	res.end();
});


app.use("/", express.static("./dist"));


setEnvironment(process.env);


Object.entries(service)
	.forEach(([path, value]) => Object.entries(value)
		.forEach(([method, handler]) => app[method](path, handler)));


const httpServer = http.createServer(app);

const port = Number(process.env.PORT);
httpServer.listen(port, process.env.HOST, () => {
	console.log("Lotus server online:", `http://${process.env.HOST}:${port}`);
});


if (process.env.SOURCE_EDITOR_DIR) {
	const editorDir = process.env.SOURCE_EDITOR_DIR.replace(/^~/, process.env.HOME);
	const editorApiPath = "/source-dir";

	webEditor.service.createServer(httpServer, {rootDir: editorDir});

	// source directory list handler
	app.use(`${editorApiPath}/*`, dirServer.createHandler(editorDir, editorApiPath));
}