Configure utilities API creds via Settings UI, not .env

Follows the same app_settings/DB pattern already used for the
Forecasting integration: utilities_url and utilities_api_key are now
editable from Reports Settings, with env vars kept only as a fallback.
Avoids needing to SSH in and hand-edit .env to authenticate against
the utilities app's internal API.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-28 14:38:15 +00:00
parent dfd3a506d1
commit 203639af78
4 changed files with 22 additions and 7 deletions

View file

@ -81,7 +81,9 @@ export async function initDb() {
await pool.query(`
INSERT INTO app_settings (key, value) VALUES
('forecasting_url', ''),
('forecasting_api_key', '')
('forecasting_api_key', ''),
('utilities_url', ''),
('utilities_api_key', '')
ON CONFLICT (key) DO NOTHING
`)
}

View file

@ -1,11 +1,13 @@
// Client for the `utilities` app's internal API (meter readings, tariffs, energy costs).
// Mirrors the existing forecasting integration pattern used in
// routes/directors-forecast.js and routes/weekly-actual.js (X-API-Key header,
// base URL + key from env, throw on non-2xx).
// base URL + key from the app_settings table — set via Settings UI — with
// env vars as a fallback, throw on non-2xx).
import { getSetting } from '../db.js'
async function utilFetch(path) {
const apiKey = process.env.UTILITIES_API_KEY
const baseUrl = process.env.UTILITIES_URL || 'http://10.10.10.127:3080'
const apiKey = await getSetting('utilities_api_key') || process.env.UTILITIES_API_KEY
const baseUrl = await getSetting('utilities_url') || process.env.UTILITIES_URL || 'http://10.10.10.127:3080'
if (!apiKey) throw new Error('UTILITIES_API_KEY not configured')
const res = await fetch(`${baseUrl}${path}`, {
headers: { 'X-API-Key': apiKey },

View file

@ -1,7 +1,7 @@
import { requireAuth } from '../auth.js'
import { pool } from '../db.js'
const ALLOWED_KEYS = new Set(['forecasting_url', 'forecasting_api_key'])
const ALLOWED_KEYS = new Set(['forecasting_url', 'forecasting_api_key', 'utilities_url', 'utilities_api_key'])
export async function settingsRoutes(fastify) {
fastify.addHook('preHandler', requireAuth)