Backend lol

This commit is contained in:
Flummi 2019-05-07 20:23:43 +00:00
parent c3291fd930
commit d64b9ce20d
5 changed files with 36 additions and 17 deletions

View File

@ -9,7 +9,7 @@ router.get(/^\/api$/, (req, res) => {
}); });
router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => { router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
const db = await sql; const db = await sql.getConnection();
const args = []; const args = [];
let q = queries.random.main; let q = queries.random.main;
@ -30,33 +30,48 @@ router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, r
.writeHead(500) .writeHead(500)
.end(JSON.stringify(err), 'utf-8'); .end(JSON.stringify(err), 'utf-8');
} }
db.end();
}); });
router.get(/^\/api\/p$/, async (req, res) => { router.get(/^\/api\/p$/, async (req, res) => {
const db = await sql; const db = await sql.getConnection();
const id = parseInt(req.url.qs.id) || 99999999; let id = parseInt(req.url.qs.id) || 99999999;
const eps = Math.min(parseInt(req.url.qs.eps) || 100, 200); const eps = Math.min(parseInt(req.url.qs.eps) || 100, 200);
const [ order, trend ] = req.url.qs.order === "asc" ? [ "asc", ">" ] : [ "desc", "<" ]; let [ order, trend ] = req.url.qs.order === "asc" ? [ "asc", ">" ] : [ "desc", "<" ];
try { try {
const rows = await db.query(`select id, mime from f0ck.items where id ${trend} ? order by id ${order} limit ?`, [ id, eps ]); const tmp = (await db.query("select min(id) as min, max(id) as max from items limit 1"))[0];
if((id - 1 + eps) > tmp.max) {
id = tmp.max;
[ order, trend ] = [ "desc", "<=" ];
}
if((id + 1 - eps) < tmp.min) {
id = tmp.min;
[ order, trend ] = [ "asc", ">=" ];
}
const rows = await db.query(queries.p(trend, order), [ id, eps ]);
const items = { const items = {
items: rows, items: rows,
first: rows[0].id, first: rows[0].id,
last: rows[rows.length - 1].id last: rows[rows.length - 1].id,
newest: tmp.max,
oldest: tmp.min
}; };
res res
.writeHead(200, { 'Content-Type': 'application/json' }) .writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(items), 'utf-8'); .end(JSON.stringify(items), 'utf-8');
} catch(err) { } catch(err) {
console.error(err);
res res
.writeHead(500) .writeHead(500)
.end(JSON.stringify(err), 'utf-8'); .end(JSON.stringify(err), 'utf-8');
} }
db.end();
}); });
router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => { router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
const db = await sql; const db = await sql.getConnection();
try { try {
const rows = await db.query(queries.item, Array(3).fill(req.url.split[2])); const rows = await db.query(queries.item, Array(3).fill(req.url.split[2]));
const data = rows[0].length > 0 ? { const data = rows[0].length > 0 ? {
@ -76,10 +91,11 @@ router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
.writeHead(500) .writeHead(500)
.end(JSON.stringify(err), 'utf-8'); .end(JSON.stringify(err), 'utf-8');
} }
db.end();
}); });
router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umstellen router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umstellen
const db = await sql; const db = await sql.getConnection();
const user = req.url.split[2]; const user = req.url.split[2];
const eps = Math.min(req.url.split[3] || 50, 50); const eps = Math.min(req.url.split[3] || 50, 50);
try { try {
@ -92,4 +108,5 @@ router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umste
.writeHead(500) .writeHead(500)
.end(JSON.stringify(err), 'utf-8'); .end(JSON.stringify(err), 'utf-8');
} }
db.end();
}); });

View File

@ -6,11 +6,12 @@ export const mimes = {
export const queries = { export const queries = {
random: { random: {
main: "select id, mime, size, username, userchannel, usernetwork, stamp, dest, src from f0ck.items ", main: "select id, mime, size, username, userchannel, usernetwork, stamp, dest, src from items ",
where: where => `${where?`where ${where}`:""} order by rand() limit 1` where: where => `${where?`where ${where}`:""} order by rand() limit 1`
}, },
item: "select id, mime, size, src, stamp, userchannel, username, usernetwork from f0ck.items where id = ? limit 1;" item: "select id, mime, size, src, stamp, userchannel, username, usernetwork from items where id = ? limit 1;"
+ "select id from f0ck.items where id = (select min(id) from f0ck.items where id > ?);" + "select id from items where id = (select min(id) from items where id > ?);"
+ "select id from f0ck.items where id = (select max(id) from f0ck.items where id < ?)", + "select id from items where id = (select max(id) from items where id < ?)",
user: "select id, mime, size, src, stamp, userchannel, username, usernetwork from f0ck.items where username = ? limit ?" p: (trend, order) => `select id, mime from items where id ${trend} ? order by id ${order} limit ?`,
user: "select id, mime, size, src, stamp, userchannel, username, usernetwork from items where username = ? limit ?"
}; };

View File

@ -1,3 +1,3 @@
export const queries = { export const queries = {
items: "select id, mime from f0ck.items order by id desc limit 72" items: "select id, mime from items order by id desc limit 72"
}; };

View File

@ -7,8 +7,8 @@ import { queries } from "./inc/index";
const template = fs.readFileSync("./views/index.hbs", "utf-8"); const template = fs.readFileSync("./views/index.hbs", "utf-8");
router.get(/^\/$/, async (req, res) => { router.get(/^\/(page\/[0-9]+)?$/, async (req, res) => {
const db = await sql; const db = await sql.getConnection();
try { try {
const rows = await db.query(queries.items); const rows = await db.query(queries.items);
@ -21,4 +21,5 @@ router.get(/^\/$/, async (req, res) => {
.writeHead(500) .writeHead(500)
.end(JSON.stringify(err), 'utf-8'); .end(JSON.stringify(err), 'utf-8');
} }
db.end();
}); });

View File

@ -1,4 +1,4 @@
import mariadb from "mariadb"; import mariadb from "mariadb";
import cfg from "../../config.json"; import cfg from "../../config.json";
export default mariadb.createPool(cfg.sql).getConnection(); export default mariadb.createPool(cfg.sql);//.getConnection();