summaryrefslogtreecommitdiff
path: root/app/app.js
blob: d9cd39c22623e09ad31174ef05dd81ae8483062e (plain)
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
"use strict";

const http = require("http");
const fs = require("fs");

const argon2 = require("argon2");

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");
if (!fs.existsSync("./users")) fs.mkdirSync("./users");
if (!fs.existsSync("./private")) fs.mkdirSync("./private");

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 = process.env.HOST||"0.0.0.0";

const server = http.createServer();
const stat = new Static("./public");
//const wss = new Socket(server);
const app = new Router();

const cors = fn => (req, res, ...rest) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Methods", "GET, POST");
  fn(req, res, ...rest); };

const sj = (res, data) => {
  res.setHeader("Content-Type", "application/json");
  res.end(JSON.stringify(data)); };

//app.get("/", (req, res) => {
//  res.writeHead(302, {"Location":"https://alexishovorka.com/"});
//  res.end();
//});

//await argon2.hash(password, {type: argon2.argon2id});
//await argon2.verify(hash, password);

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}`);