Backend lol
This commit is contained in:
parent
c3291fd930
commit
d64b9ce20d
@ -9,7 +9,7 @@ router.get(/^\/api$/, (req, res) => {
|
||||
});
|
||||
|
||||
router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
|
||||
const db = await sql;
|
||||
const db = await sql.getConnection();
|
||||
const args = [];
|
||||
let q = queries.random.main;
|
||||
|
||||
@ -30,33 +30,48 @@ router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, r
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), 'utf-8');
|
||||
}
|
||||
db.end();
|
||||
});
|
||||
|
||||
router.get(/^\/api\/p$/, async (req, res) => {
|
||||
const db = await sql;
|
||||
const id = parseInt(req.url.qs.id) || 99999999;
|
||||
const db = await sql.getConnection();
|
||||
let id = parseInt(req.url.qs.id) || 99999999;
|
||||
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 {
|
||||
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 = {
|
||||
items: rows,
|
||||
first: rows[0].id,
|
||||
last: rows[rows.length - 1].id
|
||||
last: rows[rows.length - 1].id,
|
||||
newest: tmp.max,
|
||||
oldest: tmp.min
|
||||
};
|
||||
res
|
||||
.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
.end(JSON.stringify(items), 'utf-8');
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
res
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), 'utf-8');
|
||||
}
|
||||
db.end();
|
||||
});
|
||||
|
||||
router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
|
||||
const db = await sql;
|
||||
const db = await sql.getConnection();
|
||||
try {
|
||||
const rows = await db.query(queries.item, Array(3).fill(req.url.split[2]));
|
||||
const data = rows[0].length > 0 ? {
|
||||
@ -76,10 +91,11 @@ router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), 'utf-8');
|
||||
}
|
||||
db.end();
|
||||
});
|
||||
|
||||
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 eps = Math.min(req.url.split[3] || 50, 50);
|
||||
try {
|
||||
@ -92,4 +108,5 @@ router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umste
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), 'utf-8');
|
||||
}
|
||||
db.end();
|
||||
});
|
||||
|
@ -6,11 +6,12 @@ export const mimes = {
|
||||
|
||||
export const queries = {
|
||||
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`
|
||||
},
|
||||
item: "select id, mime, size, src, stamp, userchannel, username, usernetwork from f0ck.items where id = ? limit 1;"
|
||||
+ "select id from f0ck.items where id = (select min(id) from f0ck.items where id > ?);"
|
||||
+ "select id from f0ck.items where id = (select max(id) from f0ck.items where id < ?)",
|
||||
user: "select id, mime, size, src, stamp, userchannel, username, usernetwork from f0ck.items where username = ? limit ?"
|
||||
item: "select id, mime, size, src, stamp, userchannel, username, usernetwork from items where id = ? limit 1;"
|
||||
+ "select id from items where id = (select min(id) from items where id > ?);"
|
||||
+ "select id from items where id = (select max(id) from items where id < ?)",
|
||||
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 ?"
|
||||
};
|
||||
|
@ -1,3 +1,3 @@
|
||||
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"
|
||||
};
|
||||
|
@ -7,8 +7,8 @@ import { queries } from "./inc/index";
|
||||
|
||||
const template = fs.readFileSync("./views/index.hbs", "utf-8");
|
||||
|
||||
router.get(/^\/$/, async (req, res) => {
|
||||
const db = await sql;
|
||||
router.get(/^\/(page\/[0-9]+)?$/, async (req, res) => {
|
||||
const db = await sql.getConnection();
|
||||
|
||||
try {
|
||||
const rows = await db.query(queries.items);
|
||||
@ -21,4 +21,5 @@ router.get(/^\/$/, async (req, res) => {
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), 'utf-8');
|
||||
}
|
||||
db.end();
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import mariadb from "mariadb";
|
||||
import cfg from "../../config.json";
|
||||
|
||||
export default mariadb.createPool(cfg.sql).getConnection();
|
||||
export default mariadb.createPool(cfg.sql);//.getConnection();
|
||||
|
Loading…
Reference in New Issue
Block a user