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
|
"use strict";
const WORD_LENGTH = 6;
const WORDS = require("./words.json");
const START = 1643612400000;
const fs = require("fs");
const http = require("http");
const {sj, cors, err400} = require("./lib/utils");
const Router = require("./lib/router");
const Static = require("./lib/static");
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 app = new Router();
const today = () => WORDS[Math.floor((Date.now() - START) / 864e5)];
const findAll = (c, s) => s.split("")
.reduce((a,e,i) => (e===c&&a.push(i),a), []);
app.get("/guess/(?<word>[a-z]*)", cors((req, res, match) => {
if (match.word.length !== WORD_LENGTH) return err400(res);
const result = WORDS.includes(match.word) &&
match.word.split("").map(c => findAll(c, today()))
.map((c,i,a) => c.length === 0? " " : c.includes(i)? "=" : c)
.map((c,i,a) => Array.isArray(c)? c.filter(e => a[e] !== "=") : c)
.reduce((a,c) => Array.isArray(c)? a+(c.find(e => !a.includes(e))||" ") : a+c, "")
.replace(/[0-9]/g, "-");
sj(res, {word: match.word, result});
}));
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}`);
|