From 7c3f7294cc54432007cd92b53abc572a4c56dd17 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Sat, 14 Dec 2024 17:24:29 -0300 Subject: [PATCH] use http instead of redis --- index.js | 84 ++++++++++++++++++++++++---------------------------- package.json | 1 - 2 files changed, 39 insertions(+), 46 deletions(-) diff --git a/index.js b/index.js index 07e7bca..7df2ef3 100644 --- a/index.js +++ b/index.js @@ -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) - } catch(e) { - console.error(`run: Error ${e}`) + tokens.visitorData = visitorData; + tokens.poToken = poToken; + } catch (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() + } + 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() } - console.log(`Tokens generated successfully for user: ${user}`) -} +}) -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 } diff --git a/package.json b/package.json index d76803c..e8f9e7c 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "main": "index.js", "license": "MIT", "dependencies": { - "ioredis": "^5.4.1", "jsdom": "^24.1.1" }, "engines": {