-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (48 loc) · 1.26 KB
/
index.js
File metadata and controls
57 lines (48 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require("express");
const ApiSession = require("./LLM");
const dotenv = require("dotenv");
const authToken = dotenv.config().parsed.Auth
const app = express();
app.use(express.json());
/**
* @param {express.Request} req
* @param {express.Response} res
* @returns {Boolean}
*/
function authConnection(req, res) {
if (req.headers.authorization !== authToken) {
res.status(401).json({ message: "Unauthorized" });
res.end();
return false;
}
return true;
}
var queue = [];
async function runQueue() {
if (queue.length == 0) return;
await queue[0]();
queue.shift();
runQueue();
}
async function addToQueue(asyncFunction) {
queue.push(asyncFunction);
if (queue.length == 1) await runQueue();
}
app.post("/getCategory", (req, res) => {
if (!authConnection(req, res)) return;
addToQueue(async function () {
const instance = await ApiSession.getInstance();
const category = await instance.getCategory(req.body.sender, req.body.message);
res.json({ category });
});
});
app.get("/refresh", async (req, res) => {
if (!authConnection(req, res)) return;
addToQueue(async function () {
await ApiSession.refresh();
res.json({ message: "Refreshed" });
});
});
app.listen(3087, () => {
console.log("Server is running on http://localhost:3087");
});