From 6bb3e72df331ef45a69fe7f76ed4b7e7babe021a Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Wed, 17 Jul 2019 12:35:46 -0600 Subject: Initial commit --- app.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 app.js (limited to 'app.js') diff --git a/app.js b/app.js new file mode 100644 index 0000000..56b3b82 --- /dev/null +++ b/app.js @@ -0,0 +1,46 @@ +"use strict" + +const http = require("http"); +const fs = require("fs"); +const ws = require("ws"); + +const Router = require("./lib/router"); +const Static = require("./lib/static"); + +if (!fs.existsSync("./logs")) fs.mkdirSync("./logs"); + +Math.clamp = Math.clamp || ((x,l,h) => Math.max(l,Math.min(x,h))); +const PORT = Math.clamp(+process.env.PORT||8080, 1, 65535); +const HOST = "0.0.0.0"; + +const server = http.createServer(); +const stat = new Static("./public"); +const app = new Router(); + +function sj(res, data) { // For convenience + res.setHeader("Content-Type", "application/json"); + res.end(JSON.stringify(data)); +} + +app.get("/hist.json", (req, res) => sj(res, [])); + +//app.get("/(?[0-9A-Z]+)", (req, res, path) => { +// res.setHeader("Content-Type", "text/plain"); +// res.end(path[id]); +//}); + +//app.gpost("/(?[0-9A-Z]+)/file", (req, res, path, data) => { +// res.end(data); +//}, 1<<28); // 256MB Max + +//app.put("/file/(.+)", (req, res, path) => { +// req.pipe(fs.createWriteStream("./static/"+path[1])); +//}); + +server.on("request", (req, res) => { + console.log(`${Date.now()} ${req.method} ${req.url}`); + app.route(req, res) || stat.route(req, res); +}); + +server.listen(PORT/*, HOST*/); +console.log(`Running on http://${HOST}:${PORT}`); -- cgit v1.2.3-54-g00ecf