refactor: ♻️ read patrons from local file for fallback instead of empty array
This commit is contained in:
parent
e2ff531172
commit
092c439993
1 changed files with 10 additions and 1 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue