From 46082780e1da909489ee65d629216fdd50e517bc Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 8 Apr 2020 02:19:20 +0200 Subject: [PATCH] . --- public/s/js/scroller.js | 36 ++++++++++---------- src/inc/routes/index.mjs | 3 +- src/inc/routes/stats.mjs | 9 +---- src/inc/tpl.mjs | 8 ++--- src/websrv.mjs | 71 ++++++++++++++++++++-------------------- views/contact.html | 15 ++------- views/how.html | 15 ++------- views/index.html | 17 ++-------- views/item.html | 26 ++------------- views/main/footer.html | 7 ++++ views/main/header.html | 20 +++++++++++ 11 files changed, 97 insertions(+), 130 deletions(-) create mode 100644 views/main/footer.html create mode 100644 views/main/header.html diff --git a/public/s/js/scroller.js b/public/s/js/scroller.js index 4548803..8067c65 100644 --- a/public/s/js/scroller.js +++ b/public/s/js/scroller.js @@ -1,21 +1,23 @@ let load = false; (() => { const posts = document.querySelector("#posts"); - document.addEventListener("wheel", e => { - if((((document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop + window.innerHeight) + 310 - >= ((document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight)) && !load) { - load = true; - fetch(`./api/p/${posts.dataset.last}`) - .then(res => res.json()) - .then((msg, html = "") => { - for(let i = 0; i < msg.items.length; i++) - if(msg.items[i].id) - html += `
  • \n`; - posts.insertAdjacentHTML("beforeend", html); - posts.dataset.last = msg.last; - load = false; - }) - .catch(err => console.log(err)); - } - }); + if(posts) { + document.addEventListener("wheel", e => { + if((((document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop + window.innerHeight) + 310 + >= ((document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight)) && !load) { + load = true; + fetch(`./api/p/${posts.dataset.last}`) + .then(res => res.json()) + .then((msg, html = "") => { + for(let i = 0; i < msg.items.length; i++) + if(msg.items[i].id) + html += `
  • \n`; + posts.insertAdjacentHTML("beforeend", html); + posts.dataset.last = msg.last; + load = false; + }) + .catch(err => console.log(err)); + } + }); + } })(); diff --git a/src/inc/routes/index.mjs b/src/inc/routes/index.mjs index 132c986..cafdded 100644 --- a/src/inc/routes/index.mjs +++ b/src/inc/routes/index.mjs @@ -43,7 +43,8 @@ router.get(/^\/([0-9]+)$/, async (req, res) => { timestamp: new Date(query.stamp * 1000).toISOString() }, next: query.next ? query.next : null, - prev: query.prev ? query.prev : null + prev: query.prev ? query.prev : null, + title: `${query.id} - f0ck.me` }; res.reply({ body: tpl.render("views/item", data) }); }); diff --git a/src/inc/routes/stats.mjs b/src/inc/routes/stats.mjs index 3ede38d..71b5142 100644 --- a/src/inc/routes/stats.mjs +++ b/src/inc/routes/stats.mjs @@ -2,10 +2,6 @@ import router from "../router.mjs"; import url from "url"; import util from "util"; import sql from "../sql.mjs"; -import lib from "../lib.mjs"; -import tpl from "../tpl.mjs"; - -tpl.readdir("views"); router.get("/stats", async (req, res) => { const query = await sql.query("select src from items"); @@ -15,10 +11,7 @@ router.get("/stats", async (req, res) => { hosts[host] ? hosts[host]++ : hosts[host] = 1; }); - const sorted = Object.keys(hosts).sort((a, b) => hosts[b] - hosts[a]).map(k => ({ [k]: hosts[k] })).reduce((a, b) => ({ ...a, ...b })); - res.reply({ - body: "
    " + util.inspect(sorted) + "
    " + body: "
    " + util.inspect(Object.keys(hosts).sort((a, b) => hosts[b] - hosts[a]).map(k => ({ [k]: hosts[k] })).reduce((a, b) => ({ ...a, ...b }))) + "
    " }); - //res.reply({ body: tpl.render("views/index", data) }); }); diff --git a/src/inc/tpl.mjs b/src/inc/tpl.mjs index 14edfd3..b35314e 100644 --- a/src/inc/tpl.mjs +++ b/src/inc/tpl.mjs @@ -4,13 +4,13 @@ import path from "path"; export default new class { #templates = {}; #syntax = [ - [ "each", (t, args = t.slice(4).trim().split(" ")) => `util.forEach(${args[0]},(${(args[1] === "as" && args[2]) ? args[2] : "value"},key)=>{` ], + [ "each", (t, _, args = t.slice(4).trim().split(" ")) => `util.forEach(${args[0]},(${(args[1] === "as" && args[2]) ? args[2] : "value"},key)=>{` ], [ "/each", () => "});" ], [ "if", t => `if(${t.slice(2).trim()}){` ], [ "elseif", t => `}else if(${t.slice(6).trim()}){` ], [ "else", () => "}else{" ], [ "/if", () => "}" ], - [ "include", t => `html+=util.tpl["${t.slice(7).trim()}"];`], + [ "include", (t, data) => `html+='${this.render(t.slice(7).trim(), data)}';` ], //`html+=util.include("${t.slice(7).trim()}", data);`], // parse them aswell [ "=", t => `html+=${t.slice(1).trim()};` ] ]; readdir(dir, root = dir, rel = dir.replace(`${root}/`, "")) { @@ -33,8 +33,8 @@ export default new class { .replace(/[\n\r]/g, "") .split(/{{\s*([^}]+)\s*}}/) .filter(Boolean) - .map(t => !(f = this.#syntax.filter(s => t.startsWith(s[0]))[0]) ? `html+='${t}';` : f[1](t)) + .map(t => !(f = this.#syntax.filter(s => t.startsWith(s[0]))[0]) ? `html+='${t}';` : f[1](t, data)) .join("") + "}return html.trim().replace(/>[\\n\\r\\s]*?<')" - ).bind(null, { forEach: this.forEach, tpl: this.#templates })(data); + ).bind(null, { forEach: this.forEach })(data); } }; diff --git a/src/websrv.mjs b/src/websrv.mjs index 5f1fbc0..58f5add 100644 --- a/src/websrv.mjs +++ b/src/websrv.mjs @@ -1,43 +1,44 @@ import http from "http"; import url from "url"; +import { promises as fs } from "fs"; import querystring from "querystring"; import cfg from "../config.json"; import router from "./inc/router.mjs"; -// routes -import "./inc/routes/index.mjs"; -import "./inc/routes/api.mjs"; -import "./inc/routes/static.mjs"; -import "./inc/routes/stats.mjs"; +(async () => { + await Promise.all((await fs.readdir("./src/inc/routes")) + .filter(r => r.endsWith(".mjs")) + .map(r => import(`./inc/routes/${r}`))); -http.createServer(async (req, res, r) => { - const t_start = process.hrtime(); - - req.url = url.parse(req.url.replace(/(?!^.)(\/+)?$/, '')); - req.url.split = req.url.pathname.split("/").slice(1); - req.url.qs = querystring.parse(req.url.query); - - req.post = new Promise((resolve, _, data = "") => req - .on("data", d => void (data += d)) - .on("end", () => void resolve(Object.fromEntries(Object.entries(querystring.parse(data)).map(([k, v]) => [k, decodeURIComponent(v)]))))); - - res.reply = ({ - code = 200, - type = "text/html", - body - }) => res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }).end(body); + http.createServer(async (req, res, r) => { + const t_start = process.hrtime(); - res.redirect = target => res.writeHead(301, { - "Cache-Control": "no-cache, public", - "Location": target - }).end(); - - !(r = router.routes.getRoute(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : await r(req, res); - console.log([ - `[${(new Date()).toLocaleTimeString()}]`, - `${(process.hrtime(t_start)[1] / 1e6).toFixed(2)}ms`, - `${req.method} ${res.statusCode}`, - req.url.pathname].map(e=>e.toString().padEnd(15)).join("")); -}).listen(cfg.websrv.port, () => setTimeout(() => { - console.log(`f0ck is listening on port ${cfg.websrv.port}.`); -}, 500)); + req.url = url.parse(req.url.replace(/(?!^.)(\/+)?$/, '')); + req.url.split = req.url.pathname.split("/").slice(1); + req.url.qs = querystring.parse(req.url.query); + + req.post = new Promise((resolve, _, data = "") => req + .on("data", d => void (data += d)) + .on("end", () => void resolve(Object.fromEntries(Object.entries(querystring.parse(data)).map(([k, v]) => [k, decodeURIComponent(v)]))))); + + res.reply = ({ + code = 200, + type = "text/html", + body + }) => res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }).end(body); + + res.redirect = target => res.writeHead(301, { + "Cache-Control": "no-cache, public", + "Location": target + }).end(); + + !(r = router.routes.getRoute(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : await r(req, res); + console.log([ + `[${(new Date()).toLocaleTimeString()}]`, + `${(process.hrtime(t_start)[1] / 1e6).toFixed(2)}ms`, + `${req.method} ${res.statusCode}`, + req.url.pathname].map(e=>e.toString().padEnd(15)).join("")); + }).listen(cfg.websrv.port, () => setTimeout(() => { + console.log(`f0ck is listening on port ${cfg.websrv.port}.`); + }, 500)); +})(); diff --git a/views/contact.html b/views/contact.html index b656bc8..adcafcf 100644 --- a/views/contact.html +++ b/views/contact.html @@ -1,19 +1,8 @@ - - - - f0ck! I need contact! - - - - - - {{include snippets/navbar}} +{{include main/header}}

    Got a problem? We have the answer: admin@f0ck.space

    return to main
    - - - +{{include main/footer}} diff --git a/views/how.html b/views/how.html index 8b80e34..8b56c75 100644 --- a/views/how.html +++ b/views/how.html @@ -1,13 +1,4 @@ - - - f0ck me! but how? - - - - - - - {{include snippets/navbar}} +{{include main/header}}

    irc.n0xy.net +6697 (ssl only) #f0ck

    @@ -41,6 +32,4 @@ reupload somewhere until this is fixed

    - - - +{{include main/footer}} diff --git a/views/index.html b/views/index.html index 45a089b..33aba0c 100644 --- a/views/index.html +++ b/views/index.html @@ -1,14 +1,4 @@ - - - - f0ck! - - - - - - - {{include snippets/navbar}} +{{include main/header}}
    - - - - +{{include main/footer}} diff --git a/views/item.html b/views/item.html index 48d706d..8083f1a 100644 --- a/views/item.html +++ b/views/item.html @@ -1,21 +1,4 @@ - - - - {{=item.id}} - f0ck.me - - - - - - - - - - - - - - {{include snippets/navbar}} +{{include main/header}}
    Random @@ -65,9 +48,4 @@
    - - - - - - +{{include main/footer}} \ No newline at end of file diff --git a/views/main/footer.html b/views/main/footer.html new file mode 100644 index 0000000..697c2e8 --- /dev/null +++ b/views/main/footer.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/views/main/header.html b/views/main/header.html new file mode 100644 index 0000000..b895a66 --- /dev/null +++ b/views/main/header.html @@ -0,0 +1,20 @@ + + + + {{if data.title}}{{=data.title}}{{else}}f0ck!{{/if}} + + + + + + + {{if data.item}} + + + + + + {{/if}} + + + {{include snippets/navbar}}