async/await
This commit is contained in:
parent
6f6f3c8481
commit
06f665b220
|
@ -5,19 +5,18 @@ export let admins = [];
|
||||||
export const loadAdmins = async () => {
|
export const loadAdmins = async () => {
|
||||||
const db = await sql;
|
const db = await sql;
|
||||||
admins = [];
|
admins = [];
|
||||||
db.query(`select * from user`)
|
try {
|
||||||
.then(rows => rows.forEach(row => {
|
const rows = await db.query("select id, prefix, account, level, network from user");
|
||||||
admins.push({
|
rows.forEach(row => admins.push({
|
||||||
id: row.id,
|
id: row.id,
|
||||||
prefix: row.prefix,
|
prefix: row.prefix,
|
||||||
account: row.account,
|
account: row.account,
|
||||||
network: row.network,
|
network: row.network,
|
||||||
level: row.level
|
level: row.level
|
||||||
});
|
}));
|
||||||
}))
|
} catch(err) {
|
||||||
.catch(err => {
|
|
||||||
console.log("keine Admins vorhanden", err);
|
console.log("keine Admins vorhanden", err);
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
|
@ -10,10 +10,8 @@ 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;
|
||||||
|
const args = [];
|
||||||
let q = queries.random.main;
|
let q = queries.random.main;
|
||||||
let args = [];
|
|
||||||
|
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
||||||
|
|
||||||
if(req.url.split[2] === "user") {
|
if(req.url.split[2] === "user") {
|
||||||
q += queries.random.where("username like ?");
|
q += queries.random.where("username like ?");
|
||||||
|
@ -22,39 +20,45 @@ router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, r
|
||||||
else
|
else
|
||||||
q += queries.random.where(mimes[req.url.split[2]] ? mimes[req.url.split[2]].map(mime => `mime = "${mime}"`).join(" or ") : null);
|
q += queries.random.where(mimes[req.url.split[2]] ? mimes[req.url.split[2]].map(mime => `mime = "${mime}"`).join(" or ") : null);
|
||||||
|
|
||||||
db.query(q, args)
|
try {
|
||||||
.then(rows => {
|
const rows = await db.query(q, args);
|
||||||
res.end(JSON.stringify(rows.length > 0 ? rows[0] : []), 'utf-8');
|
res
|
||||||
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
|
.writeHead(200, { 'Content-Type': 'application/json' })
|
||||||
|
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), 'utf-8');
|
||||||
|
} catch(err) {
|
||||||
|
res
|
||||||
|
.writeHead(500)
|
||||||
|
.end(JSON.stringify(err), 'utf-8');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//router.get(/^\/api\/p(\/[0-9]+|\/)?(\/[0-9]+)?$/, async (req, res) => {
|
|
||||||
router.get(/^\/api\/p$/, async (req, res) => {
|
router.get(/^\/api\/p$/, async (req, res) => {
|
||||||
const db = await sql;
|
const db = await sql;
|
||||||
const id = parseInt(req.url.qs.id) || 99999999;
|
const 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);
|
||||||
db.query("select * from f0ck.items where id < ? order by id desc limit ?", [id, eps])
|
const [ order, trend ] = req.url.qs.order === "asc" ? [ "asc", ">" ] : [ "desc", "<" ];
|
||||||
.then(rows => {
|
|
||||||
let items = {
|
try {
|
||||||
"items": [],
|
const rows = await db.query(`select id, mime from f0ck.items where id ${trend} ? order by id ${order} limit ?`, [ id, eps ]);
|
||||||
"last": id
|
const items = {
|
||||||
|
items: rows,
|
||||||
|
first: rows[0].id,
|
||||||
|
last: rows[rows.length - 1].id
|
||||||
};
|
};
|
||||||
rows.forEach(e => {
|
res
|
||||||
items.items.push({
|
.writeHead(200, { 'Content-Type': 'application/json' })
|
||||||
"id": e.id,
|
.end(JSON.stringify(items), 'utf-8');
|
||||||
"mime": e.mime
|
} catch(err) {
|
||||||
});
|
res
|
||||||
items.last = e.id;
|
.writeHead(500)
|
||||||
});
|
.end(JSON.stringify(err), 'utf-8');
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
}
|
||||||
res.end(JSON.stringify(items), 'utf-8');
|
|
||||||
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
||||||
db.query(queries.item, Array(3).fill(req.url.split[2]))
|
try {
|
||||||
.then(rows => {
|
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 ? {
|
||||||
...rows[0][0], ...{
|
...rows[0][0], ...{
|
||||||
thumb: `${cfg.main.url}/t/${rows[0][0].id}.png`,
|
thumb: `${cfg.main.url}/t/${rows[0][0].id}.png`,
|
||||||
|
@ -64,17 +68,28 @@ router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
|
||||||
} : {
|
} : {
|
||||||
error: true
|
error: true
|
||||||
};
|
};
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
res
|
||||||
res.end(JSON.stringify(data), 'utf-8');
|
.writeHead(200, { 'Content-Type': 'application/json' })
|
||||||
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
|
.end(JSON.stringify(data), 'utf-8');
|
||||||
|
} catch(err) {
|
||||||
|
res
|
||||||
|
.writeHead(500)
|
||||||
|
.end(JSON.stringify(err), 'utf-8');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => {
|
router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umstellen
|
||||||
const db = await sql;
|
const db = await sql;
|
||||||
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);
|
||||||
db.query(queries.user, [ user, eps ])
|
try {
|
||||||
.then(rows => {
|
const rows = await db.query(queries.user, [ user, eps ]);
|
||||||
res.end(JSON.stringify(rows.length > 0 ? rows : []), 'utf-8');
|
res
|
||||||
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
|
.writeHead(200, { 'Content-Type': 'application/json' })
|
||||||
|
.end(JSON.stringify(rows.length > 0 ? rows : []), 'utf-8');
|
||||||
|
} catch(err) {
|
||||||
|
res
|
||||||
|
.writeHead(500)
|
||||||
|
.end(JSON.stringify(err), 'utf-8');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,14 +9,16 @@ const template = fs.readFileSync("./views/index.hbs", "utf-8");
|
||||||
|
|
||||||
router.get(/^\/$/, async (req, res) => {
|
router.get(/^\/$/, async (req, res) => {
|
||||||
const db = await sql;
|
const db = await sql;
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
||||||
|
|
||||||
db.query(queries.items)
|
try {
|
||||||
.then(items => {
|
const rows = await db.query(queries.items);
|
||||||
const tpl = handlebars.compile(template);
|
const tpl = handlebars.compile(template);
|
||||||
res.end(tpl({ items: items, debug: JSON.stringify(req.url, null, 2) }));
|
res
|
||||||
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
|
.writeHead(200, { 'Content-Type': 'text/html' })
|
||||||
|
.end(tpl({ items: rows, debug: JSON.stringify(req.url, null, 2) }));
|
||||||
/*const tpl = handlebars.compile(template);
|
} catch(err) {
|
||||||
res.end(tpl());*/
|
res
|
||||||
|
.writeHead(500)
|
||||||
|
.end(JSON.stringify(err), 'utf-8');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,5 +16,5 @@ http.createServer((req, res, r) => {
|
||||||
|
|
||||||
console.log(`[${(new Date()).toLocaleTimeString()}] ${req.method} ${req.url.pathname}`);
|
console.log(`[${(new Date()).toLocaleTimeString()}] ${req.method} ${req.url.pathname}`);
|
||||||
|
|
||||||
!(r = routes.getRegex(req.url.pathname, req.method)) ? res.end(`404 - ${req.url.pathname}`) : r(req, res);
|
!(r = routes.getRegex(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : r(req, res);
|
||||||
}).listen(cfg.websrv.port, () => console.log(`f0ck is listening on port ${cfg.websrv.port}.`));
|
}).listen(cfg.websrv.port, () => console.log(`f0ck is listening on port ${cfg.websrv.port}.`));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user