.
This commit is contained in:
parent
3afd63b86a
commit
c97b08a7c4
6
src/inc/routes/inc/index.mjs
Normal file
6
src/inc/routes/inc/index.mjs
Normal 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"
|
||||
};
|
@ -1,7 +1,7 @@
|
||||
import router from "../router.mjs";
|
||||
import cfg from "../../../config.json";
|
||||
import path from "path";
|
||||
import url from "url";
|
||||
import { queries } from "./inc/index.mjs";
|
||||
import sql from "../sql.mjs";
|
||||
import lib from "../lib.mjs";
|
||||
import tpl from "../tpl.mjs";
|
||||
@ -19,34 +19,31 @@ router.get("/", async (req, res) => {
|
||||
});
|
||||
|
||||
router.get(/^\/([0-9]+)$/, async (req, res) => {
|
||||
const q = "select * from items where id = ? limit 1; " // get item
|
||||
+ "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
|
||||
const query = await sql.query(q, Array(3).fill(req.url.split[0]));
|
||||
if(!query[0][0])
|
||||
const query = (await sql.query(queries.item, Array(3).fill(req.url.split[0])))?.shift();
|
||||
|
||||
if(query.length === 0)
|
||||
return res.redirect("/404");
|
||||
|
||||
const e = query[0][0];
|
||||
const data = {
|
||||
user: {
|
||||
name: e.username,
|
||||
channel: e.userchannel,
|
||||
network: e.usernetwork
|
||||
name: query.username,
|
||||
channel: query.userchannel,
|
||||
network: query.usernetwork
|
||||
},
|
||||
item: {
|
||||
id: e.id,
|
||||
id: query.id,
|
||||
src: {
|
||||
long: e.src,
|
||||
short: url.parse(e.src).hostname,
|
||||
long: query.src,
|
||||
short: url.parse(query.src).hostname,
|
||||
},
|
||||
thumbnail: `${cfg.websrv.paths.thumbnails}/${e.id}.png`,
|
||||
dest: `${cfg.websrv.paths.images}/${e.dest}`,
|
||||
mime: e.mime,
|
||||
size: lib.formatSize(e.size),
|
||||
timestamp: new Date(e.stamp * 1000).toISOString()
|
||||
thumbnail: `${cfg.websrv.paths.thumbnails}/${query.id}.png`,
|
||||
dest: `${cfg.websrv.paths.images}/${query.dest}`,
|
||||
mime: query.mime,
|
||||
size: lib.formatSize(query.size),
|
||||
timestamp: new Date(query.stamp * 1000).toISOString()
|
||||
},
|
||||
next: query[1].length ? query[1][0].id : null,
|
||||
prev: query[2].length ? query[2][0].id : null
|
||||
next: query.next ? query.next : null,
|
||||
prev: query.prev ? query.prev : null
|
||||
};
|
||||
res.reply({ body: tpl.render("views/item", data) });
|
||||
});
|
||||
|
24
src/inc/routes/stats.mjs
Normal file
24
src/inc/routes/stats.mjs
Normal 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) });
|
||||
});
|
@ -8,6 +8,7 @@ import router from "./inc/router.mjs";
|
||||
import "./inc/routes/index.mjs";
|
||||
import "./inc/routes/api.mjs";
|
||||
import "./inc/routes/static.mjs";
|
||||
import "./inc/routes/stats.mjs";
|
||||
|
||||
http.createServer(async (req, res, r) => {
|
||||
const t_start = process.hrtime();
|
||||
@ -32,7 +33,11 @@ http.createServer(async (req, res, r) => {
|
||||
}).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] / 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(() => {
|
||||
console.log(`f0ck is listening on port ${cfg.websrv.port}.`);
|
||||
}, 500));
|
||||
|
Loading…
Reference in New Issue
Block a user