refactor: ♻️ read patrons from local file for fallback instead of empty array

This commit is contained in:
ThatOneCalculator 2023-07-05 18:44:29 -07:00
parent e2ff531172
commit 092c439993
No known key found for this signature in database
GPG key ID: 8703CACD01000000

View file

@ -1,5 +1,11 @@
import define from "../define.js"; import define from "../define.js";
import { redisClient } from "@/db/redis.js"; import { redisClient } from "@/db/redis.js";
import * as fs from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
export const meta = { export const meta = {
tags: ["meta"], tags: ["meta"],
@ -35,7 +41,10 @@ export default define(meta, paramDef, async (ps) => {
) )
.then((response) => response.json()) .then((response) => response.json())
.catch(() => { .catch(() => {
patrons = cachedPatrons ? JSON.parse(cachedPatrons) : []; const staticPatrons = JSON.parse(
fs.readFileSync(`${_dirname}/../../../../../../patrons.json`, "utf-8"),
);
patrons = cachedPatrons ? JSON.parse(cachedPatrons) : staticPatrons;
}); });
await redisClient.set("patrons", JSON.stringify(patrons), "EX", 3600); await redisClient.set("patrons", JSON.stringify(patrons), "EX", 3600);
} }