-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
101 lines (88 loc) · 3.12 KB
/
server.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require("express");
const axios = require("axios");
const getTaskData = require("./helpers/getTaskData");
require("dotenv").config();
const app = express();
const port = 3000;
const getStakingKey = require("./helpers/getStakingKey");
const cron = require("node-cron");
app.use(express.json());
// Custom keywords or randomly selected
const defaultKeyword = process.env.KEY_WORD;
let pendingRequests = [];
let stakingKeyList = {};
// Function to update stakingKeyList
async function updateStakingKeyList() {
try {
stakingKeyList = await getStakingKey(process.env.TASK_ID);
// console.log("Updated stakingKeyList:", stakingKeyList);
} catch (error) {
console.error("Error updating stakingKeyList:", error);
}
}
// Run the updateStakingKeyList function immediately and then every 10 minutes
updateStakingKeyList();
cron.schedule("*/10 * * * *", updateStakingKeyList);
app.get("/keywords", (req, res) => {
try {
console.log("Request received:", req.query);
const stakingKey = req.query;
console.log("Staking Key:", stakingKey.key);
if (!stakingKey || !stakingKeyList[stakingKey.key]) {
console.log("Invalid stakingKey provided:", stakingKey);
return res.status(400).send(`Valid stakingKey is required or Not found, please run task ${process.env.TASK_ID} first.`);
}
let keyword;
if (pendingRequests.length > 0) {
keyword = pendingRequests.shift(); // Get keyword from pendingRequests and remove it
console.log("Keyword from pendingRequests:", keyword);
} else {
keyword = defaultKeyword;
if (!keyword) {
console.log(
"No Keywords from pending list, loading local keywords.json"
);
const wordsList = require("./keywords.json");
const randomIndex = Math.floor(Math.random() * wordsList.length);
keyword = wordsList[randomIndex]; // Load local JSON data
console.log("Keyword from json:", keyword);
}
}
res.send(keyword);
} catch (error) {
console.log("Error processing request:", error);
res.status(500).send("Internal Server Error");
}
});
// Endpoint to add pending keywords
app.post("/add-pending-keywords", (req, res) => {
try {
const { keywords } = req.body;
if (!keywords || !Array.isArray(keywords)) {
return res
.status(400)
.send("Invalid input, array of keywords is required");
}
pendingRequests = pendingRequests.concat(keywords);
console.log("Pending requests updated:", pendingRequests);
res.send("Keywords added to pending requests");
} catch (error) {
console.log("Error processing request:", error);
res.status(500).send("Internal Server Error");
}
});
// Get taskstate from running task
app.get("/taskstate", async (req, res) => {
try {
const taskID = process.env.TASK_ID;
const round = 0;
const taskData = await getTaskData(taskID, round);
res.json({ taskData });
} catch (error) {
console.error("Error fetching task data:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});