Extraction des secrets
This commit is contained in:
@@ -1,20 +1,68 @@
|
||||
const fs = require("fs/promises");
|
||||
const path = require("path");
|
||||
const { loadEnv } = require("./env");
|
||||
|
||||
let cached = null;
|
||||
|
||||
function parseNumber(value) {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
function applyEnvOverrides(config = {}) {
|
||||
const merged = { ...config };
|
||||
|
||||
merged.rebrickable = { ...(config.rebrickable || {}) };
|
||||
if (process.env.REBRICKABLE_API_KEY) {
|
||||
merged.rebrickable.apiKey = process.env.REBRICKABLE_API_KEY;
|
||||
}
|
||||
|
||||
const weather = config.weather || {};
|
||||
const providers = weather.providers || {};
|
||||
|
||||
merged.weather = { ...weather, providers: { ...providers } };
|
||||
merged.weather.providers.influxdb = { ...(providers.influxdb || {}) };
|
||||
merged.weather.providers.openMeteo = { ...(providers.openMeteo || {}) };
|
||||
|
||||
if (process.env.WEATHER_INFLUXDB_URL) {
|
||||
merged.weather.providers.influxdb.url = process.env.WEATHER_INFLUXDB_URL;
|
||||
}
|
||||
if (process.env.WEATHER_INFLUXDB_TOKEN) {
|
||||
merged.weather.providers.influxdb.token = process.env.WEATHER_INFLUXDB_TOKEN;
|
||||
}
|
||||
|
||||
const envLatitude = parseNumber(process.env.WEATHER_OPEN_METEO_LATITUDE);
|
||||
if (envLatitude !== null) {
|
||||
merged.weather.providers.openMeteo.latitude = envLatitude;
|
||||
}
|
||||
|
||||
const envLongitude = parseNumber(process.env.WEATHER_OPEN_METEO_LONGITUDE);
|
||||
if (envLongitude !== null) {
|
||||
merged.weather.providers.openMeteo.longitude = envLongitude;
|
||||
}
|
||||
|
||||
merged.goaccess = { ...(config.goaccess || {}) };
|
||||
if (process.env.GOACCESS_URL) {
|
||||
merged.goaccess.url = process.env.GOACCESS_URL;
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
async function loadToolsConfig(configPath = "tools/config.json") {
|
||||
const resolved = path.resolve(configPath);
|
||||
if (cached && cached.path === resolved) {
|
||||
return cached.data;
|
||||
}
|
||||
|
||||
loadEnv();
|
||||
const raw = await fs.readFile(resolved, "utf8");
|
||||
const data = JSON.parse(raw);
|
||||
const data = applyEnvOverrides(JSON.parse(raw));
|
||||
cached = { path: resolved, data };
|
||||
return data;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
applyEnvOverrides,
|
||||
loadToolsConfig,
|
||||
};
|
||||
|
||||
33
tools/lib/env.js
Normal file
33
tools/lib/env.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
let envLoaded = false;
|
||||
|
||||
function loadEnv(envPath = path.resolve(__dirname, "..", "..", ".env")) {
|
||||
if (envLoaded) return;
|
||||
envLoaded = true;
|
||||
|
||||
if (!fs.existsSync(envPath)) return;
|
||||
|
||||
const content = fs.readFileSync(envPath, "utf8");
|
||||
const lines = content.split(/\r?\n/);
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
|
||||
const separator = trimmed.indexOf("=");
|
||||
if (separator === -1) continue;
|
||||
|
||||
const key = trimmed.slice(0, separator).trim();
|
||||
const value = trimmed.slice(separator + 1);
|
||||
|
||||
if (!key || process.env[key] !== undefined) continue;
|
||||
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
loadEnv,
|
||||
};
|
||||
@@ -1,5 +1,7 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { applyEnvOverrides } = require("../config");
|
||||
const { loadEnv } = require("../env");
|
||||
|
||||
const DEFAULT_WEATHER_CONFIG = {
|
||||
timezone: "Europe/Paris",
|
||||
@@ -20,6 +22,8 @@ const DEFAULT_WEATHER_CONFIG = {
|
||||
};
|
||||
|
||||
function loadWeatherConfig(configPath = path.resolve(__dirname, "..", "..", "config.json")) {
|
||||
loadEnv();
|
||||
|
||||
let raw = {};
|
||||
|
||||
if (fs.existsSync(configPath)) {
|
||||
@@ -30,7 +34,8 @@ function loadWeatherConfig(configPath = path.resolve(__dirname, "..", "..", "con
|
||||
}
|
||||
}
|
||||
|
||||
const weather = raw.weather || {};
|
||||
const withEnv = applyEnvOverrides(raw);
|
||||
const weather = withEnv.weather || {};
|
||||
|
||||
const providers = {
|
||||
...DEFAULT_WEATHER_CONFIG.providers,
|
||||
|
||||
Reference in New Issue
Block a user