check if fredy config exists and is accessible

This commit is contained in:
orangecoding
2025-10-03 17:23:46 +02:00
parent ebc57702dc
commit 9f1e27d011
2 changed files with 25 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { config, getProviders, refreshConfig } from './lib/utils.js';
import { checkIfConfigIsAccessible, config, getProviders, refreshConfig } from './lib/utils.js';
import * as similarityCache from './lib/services/similarity-check/similarityCache.js';
import * as jobStorage from './lib/services/storage/jobStorage.js';
import FredyRuntime from './lib/FredyRuntime.js';
@@ -16,6 +16,13 @@ import { initActiveCheckerCron } from './lib/services/crons/listing-alive-cron.j
// Load configuration before any other startup steps
await refreshConfig();
const isConfigAccessible = await checkIfConfigIsAccessible();
if (!isConfigAccessible) {
logger.error('Configuration exists, but is not accessible. Please check the file permission');
process.exit(1);
}
// Ensure sqlite directory exists before loading anything else (based on config.sqlitepath)
const rawDir = config.sqlitepath || '/db';
const relDir = rawDir.startsWith('/') ? rawDir.slice(1) : rawDir;

View File

@@ -180,6 +180,23 @@ function buildHash(...inputs) {
*/
let config = {};
/**
* If the config exists, but cannot be accessed, we quit Fredy as something is fishy here.
* @returns {Promise<boolean>}
*/
export async function checkIfConfigIsAccessible() {
const path = new URL('../conf/config.json', import.meta.url);
try {
if (!fs.existsSync(path)) {
return true;
}
fs.accessSync(path, fs.constants.R_OK);
return true;
} catch {
return false;
}
}
/**
* Read config JSON from disk (conf/config.json) and parse it.
* @returns {Promise<any>} Parsed configuration object.