This commit is contained in:
Flummi 2020-04-07 10:07:47 +02:00
parent 3afd63b86a
commit c97b08a7c4
4 changed files with 53 additions and 21 deletions

View File

@ -0,0 +1,6 @@
export const queries = {
item: "select items.*, "
+ "(select id from items where id = (select min(id) from items where id > ?)) as next, "
+ "(select id from items where id = (select max(id) from items where id < ?)) as prev "
+ "from items where items.id = ? limit 1"
};

View File

@ -1,7 +1,7 @@
import router from "../router.mjs"; import router from "../router.mjs";
import cfg from "../../../config.json"; import cfg from "../../../config.json";
import path from "path";
import url from "url"; import url from "url";
import { queries } from "./inc/index.mjs";
import sql from "../sql.mjs"; import sql from "../sql.mjs";
import lib from "../lib.mjs"; import lib from "../lib.mjs";
import tpl from "../tpl.mjs"; import tpl from "../tpl.mjs";
@ -19,34 +19,31 @@ router.get("/", async (req, res) => {
}); });
router.get(/^\/([0-9]+)$/, async (req, res) => { router.get(/^\/([0-9]+)$/, async (req, res) => {
const q = "select * from items where id = ? limit 1; " // get item const query = (await sql.query(queries.item, Array(3).fill(req.url.split[0])))?.shift();
+ "select id from items where id = (select min(id) from items where id > ?); " // get previous item
+ "select id from items where id = (select max(id) from items where id < ?)"; // get next item if(query.length === 0)
const query = await sql.query(q, Array(3).fill(req.url.split[0]));
if(!query[0][0])
return res.redirect("/404"); return res.redirect("/404");
const e = query[0][0];
const data = { const data = {
user: { user: {
name: e.username, name: query.username,
channel: e.userchannel, channel: query.userchannel,
network: e.usernetwork network: query.usernetwork
}, },
item: { item: {
id: e.id, id: query.id,
src: { src: {
long: e.src, long: query.src,
short: url.parse(e.src).hostname, short: url.parse(query.src).hostname,
}, },
thumbnail: `${cfg.websrv.paths.thumbnails}/${e.id}.png`, thumbnail: `${cfg.websrv.paths.thumbnails}/${query.id}.png`,
dest: `${cfg.websrv.paths.images}/${e.dest}`, dest: `${cfg.websrv.paths.images}/${query.dest}`,
mime: e.mime, mime: query.mime,
size: lib.formatSize(e.size), size: lib.formatSize(query.size),
timestamp: new Date(e.stamp * 1000).toISOString() timestamp: new Date(query.stamp * 1000).toISOString()
}, },
next: query[1].length ? query[1][0].id : null, next: query.next ? query.next : null,
prev: query[2].length ? query[2][0].id : null prev: query.prev ? query.prev : null
}; };
res.reply({ body: tpl.render("views/item", data) }); res.reply({ body: tpl.render("views/item", data) });
}); });

24
src/inc/routes/stats.mjs Normal file
View File

@ -0,0 +1,24 @@
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");
let hosts = {};
query.forEach(e => {
const host = url.parse(e.src).hostname;
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: "<pre>" + util.inspect(sorted) + "</pre>"
});
//res.reply({ body: tpl.render("views/index", data) });
});

View File

@ -8,6 +8,7 @@ import router from "./inc/router.mjs";
import "./inc/routes/index.mjs"; import "./inc/routes/index.mjs";
import "./inc/routes/api.mjs"; import "./inc/routes/api.mjs";
import "./inc/routes/static.mjs"; import "./inc/routes/static.mjs";
import "./inc/routes/stats.mjs";
http.createServer(async (req, res, r) => { http.createServer(async (req, res, r) => {
const t_start = process.hrtime(); const t_start = process.hrtime();
@ -32,7 +33,11 @@ http.createServer(async (req, res, r) => {
}).end(); }).end();
!(r = router.routes.getRoute(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : await r(req, res); !(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] / 1e5).toFixed(2)}ms\t${res.statusCode} ${req.method}\t${req.url.pathname}`); 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(() => { }).listen(cfg.websrv.port, () => setTimeout(() => {
console.log(`f0ck is listening on port ${cfg.websrv.port}.`); console.log(`f0ck is listening on port ${cfg.websrv.port}.`);
}, 500)); }, 500));