diff options
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,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}`); |