async/await

This commit is contained in:
Flummi 2019-04-26 09:23:16 +00:00
parent 6f6f3c8481
commit 06f665b220
4 changed files with 82 additions and 66 deletions

View File

@ -5,19 +5,18 @@ export let admins = [];
export const loadAdmins = async () => {
const db = await sql;
admins = [];
db.query(`select * from user`)
.then(rows => rows.forEach(row => {
admins.push({
try {
const rows = await db.query("select id, prefix, account, level, network from user");
rows.forEach(row => admins.push({
id: row.id,
prefix: row.prefix,
account: row.account,
network: row.network,
level: row.level
});
}))
.catch(err => {
}));
} catch(err) {
console.log("keine Admins vorhanden", err);
});
}
};
(async () => {

View File

@ -10,10 +10,8 @@ router.get(/^\/api$/, (req, res) => {
router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
const db = await sql;
const args = [];
let q = queries.random.main;
let args = [];
res.writeHead(200, { 'Content-Type': 'text/html' });
if(req.url.split[2] === "user") {
q += queries.random.where("username like ?");
@ -22,39 +20,45 @@ router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, r
else
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)
.then(rows => {
res.end(JSON.stringify(rows.length > 0 ? rows[0] : []), 'utf-8');
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
try {
const rows = await db.query(q, args);
res
.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) => {
const db = await sql;
const id = parseInt(req.url.qs.id) || 99999999;
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])
.then(rows => {
let items = {
"items": [],
"last": id
const [ 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 items = {
items: rows,
first: rows[0].id,
last: rows[rows.length - 1].id
};
rows.forEach(e => {
items.items.push({
"id": e.id,
"mime": e.mime
});
items.last = e.id;
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8');
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
res
.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(items), 'utf-8');
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), 'utf-8');
}
});
router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
const db = await sql;
db.query(queries.item, Array(3).fill(req.url.split[2]))
.then(rows => {
try {
const rows = await db.query(queries.item, Array(3).fill(req.url.split[2]));
const data = rows[0].length > 0 ? {
...rows[0][0], ...{
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
};
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(data), 'utf-8');
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
res
.writeHead(200, { 'Content-Type': 'application/json' })
.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 user = req.url.split[2];
const eps = Math.min(req.url.split[3] || 50, 50);
db.query(queries.user, [ user, eps ])
.then(rows => {
res.end(JSON.stringify(rows.length > 0 ? rows : []), 'utf-8');
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
try {
const rows = await db.query(queries.user, [ user, eps ]);
res
.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');
}
});

View File

@ -9,14 +9,16 @@ const template = fs.readFileSync("./views/index.hbs", "utf-8");
router.get(/^\/$/, async (req, res) => {
const db = await sql;
res.writeHead(200, { 'Content-Type': 'text/html' });
db.query(queries.items)
.then(items => {
try {
const rows = await db.query(queries.items);
const tpl = handlebars.compile(template);
res.end(tpl({ items: items, debug: JSON.stringify(req.url, null, 2) }));
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
/*const tpl = handlebars.compile(template);
res.end(tpl());*/
res
.writeHead(200, { 'Content-Type': 'text/html' })
.end(tpl({ items: rows, debug: JSON.stringify(req.url, null, 2) }));
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), 'utf-8');
}
});

View File

@ -16,5 +16,5 @@ http.createServer((req, res, r) => {
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}.`));