api/cookies: trigger cookie load from api entrypoint

This commit is contained in:
jj 2024-10-27 18:10:57 +00:00
parent af50852815
commit 5a5a65b373
No known key found for this signature in database
2 changed files with 7 additions and 8 deletions

View file

@ -18,6 +18,7 @@ import { friendlyServiceName } from "../processing/service-alias.js";
import { verifyStream, getInternalStream } from "../stream/manage.js"; import { verifyStream, getInternalStream } from "../stream/manage.js";
import { createResponse, normalizeRequest, getIP } from "../processing/request.js"; import { createResponse, normalizeRequest, getIP } from "../processing/request.js";
import * as APIKeys from "../security/api-keys.js"; import * as APIKeys from "../security/api-keys.js";
import * as Cookies from "../processing/cookie/manager.js";
const git = { const git = {
branch: await getBranch(), branch: await getBranch(),
@ -348,6 +349,10 @@ export const runAPI = (express, app, __dirname) => {
APIKeys.setup(env.apiKeyURL); APIKeys.setup(env.apiKeyURL);
} }
if (env.cookiePath) {
Cookies.setup(env.cookiePath);
}
app.listen(env.apiPort, env.listenAddress, () => { app.listen(env.apiPort, env.listenAddress, () => {
console.log(`\n` + console.log(`\n` +
Bright(Cyan("cobalt ")) + Bright("API ^ω⁠^") + "\n" + Bright(Cyan("cobalt ")) + Bright("API ^ω⁠^") + "\n" +

View file

@ -1,26 +1,20 @@
import Cookie from './cookie.js'; import Cookie from './cookie.js';
import { readFile, writeFile } from 'fs/promises'; import { readFile, writeFile } from 'fs/promises';
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser'; import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
import { env } from '../../config.js';
const WRITE_INTERVAL = 60000, const WRITE_INTERVAL = 60000,
cookiePath = env.cookiePath,
COUNTER = Symbol('counter'); COUNTER = Symbol('counter');
let cookies = {}, dirty = false, intervalId; let cookies = {}, dirty = false, intervalId;
const setup = async () => { export const setup = async (cookiePath) => {
try { try {
if (!cookiePath) return;
cookies = await readFile(cookiePath, 'utf8'); cookies = await readFile(cookiePath, 'utf8');
cookies = JSON.parse(cookies); cookies = JSON.parse(cookies);
intervalId = setInterval(writeChanges, WRITE_INTERVAL) intervalId = setInterval(writeChanges, WRITE_INTERVAL);
} catch { /* no cookies for you */ } } catch { /* no cookies for you */ }
} }
setup();
function writeChanges() { function writeChanges() {
if (!dirty) return; if (!dirty) return;
dirty = false; dirty = false;