summaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'app.js')
-rw-r--r--app.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/app.js b/app.js
new file mode 100644
index 0000000..bc02d96
--- /dev/null
+++ b/app.js
@@ -0,0 +1,54 @@
+"use strict";
+
+const http = require("http");
+const fs = require("fs");
+
+const Router = require("./lib/router");
+const Static = require("./lib/static");
+const Socket = require("./lib/socket");
+const pipe = require("./lib/pipe");
+const otp = require("./lib/otp");
+
+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 wss = new Socket(server);
+const app = new Router();
+
+const sj = (res, data) => {
+ res.setHeader("Access-Control-Allow-Origin", "*");
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST");
+ res.setHeader("Content-Type", "application/json");
+ res.end(JSON.stringify(data)); };
+
+app.get("/", (req, res) => {
+ res.writeHead(302, {"Location":"https://alexishovorka.com/"});
+ res.end();
+});
+
+wss.on("open", client => {
+ console.log(`${Date.now()} SOCK open`);
+ client.send("hello", {foo:"bar"});
+});
+
+wss.on("world", (client, data) => {
+ console.log(`${Date.now()} SOCK world`, data);
+ wss.sendAll("yay", {meh:"woot"});
+});
+
+wss.on("close", client => {
+ console.log(`${Date.now()} SOCK close`);
+});
+
+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(`${Date.now()} Running on http://${HOST}:${PORT}`);