use http instead of redis
Some checks failed
Docker build image / build (push) Failing after 56s

This commit is contained in:
Fijxu 2024-12-14 17:24:29 -03:00
parent 3a0481112f
commit 7c3f7294cc
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
2 changed files with 39 additions and 46 deletions

View file

@ -1,35 +1,12 @@
const { env } = require('process');
const { createTask } = require('./lib/task')
const { fetchVisitorData } = require('./lib/workflow')
const { Redis } = require('ioredis')
const http = require('http');
const redis = new Redis({ host: process.env.HOST || config.host });
const sub = new Redis({ host: process.env.HOST || config.host });
const port = env.PORT || "3000"
const address = env.ADDRESS || "0.0.0.0"
redis.on('error', (error) => {
console.error('Could not connect to Redis:', error);
process.exit(1);
});
sub.on('error', (error) => {
console.error('Could not connect to Redis:', error);
process.exit(1);
});
sub.subscribe("generate-token", (err, count) => {
if (err) {
console.error("Failed to subscribe: %s", err.message);
} else {
console.log(
`Subscribed successfully! This client is currently subscribed to ${count} channels.`
);
}
});
sub.on("message", (channel, user) => {
console.log(`${channel}`);
console.log(`Generating po_token and visitor_data for ${user}`);
userGen(user);
});
let tokens = {};
const generate = async () => {
console.debug("[DEBUG] generate called")
@ -43,29 +20,46 @@ const generate = async () => {
}
}
const run = async () => {
const refreshTokens = async () => {
console.log("[DEBUG] generateTokens called")
try {
const { visitorData, poToken } = await generate();
redis.set("invidious:po_token", poToken)
redis.set("invidious:visitor_data", visitorData)
tokens.visitorData = visitorData;
tokens.poToken = poToken;
} catch (e) {
console.error(`run: Error ${e}`)
console.error(`refreshTokens: Error ${e}`)
}
console.log("New tokens sent to redis")
}
const userGen = async (user) => {
try {
const { visitorData, poToken } = await generate();
redis.set(`invidious:${user}:po_token`, poToken)
redis.set(`invidious:${user}:visitor_data`, visitorData)
} catch(e) {
console.error(`userGen: Error ${e}`)
const server = http.Server(async (req, res) => {
switch (req.url) {
case "/generate":
if (tokens.visitorData == undefined || tokens.poToken == undefined) {
res.writeHead(500)
return res.end()
}
console.log(`Tokens generated successfully for user: ${user}`)
const json = {
potoken: tokens.poToken,
visitorData: tokens.visitorData,
};
res.writeHead(200, { 'Content-Type': 'text/json' })
res.write(JSON.stringify(json))
return res.end()
case "/update":
await refreshTokens();
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write("Tokens updated")
return res.end()
default:
return res.end()
}
})
run();
setInterval(run, (process.env.RENEW_INTERVAL || 60) * 1000)
refreshTokens();
setInterval(refreshTokens, (env.RENEW_INTERVAL || 60) * 1000)
server.listen(port, address, () => {
console.log("Listening at port", port);
})
module.exports = { generate }

View file

@ -16,7 +16,6 @@
"main": "index.js",
"license": "MIT",
"dependencies": {
"ioredis": "^5.4.1",
"jsdom": "^24.1.1"
},
"engines": {