Files
sorsfonalak/services/worker/src/config.rs
T
fecoandClaude Opus 5.5 450174c273 Free chart summaries; password login off by default
- New analysis kind 'summary' (migration 0005): saving a chart queues a
  free 3-4 sentence summary written by SUMMARY_MODEL (default Sonnet
  5.5, about 1 cent; Haiku was tested too but chose weaker highlights).
  POST /v1/charts/{id}/summary returns it and re-queues a failed one.
- The chart page shows it in a "Röviden" card and polls until ready.
- PASSWORD_LOGIN (default false): register, login and password reset
  answer 403; only Google (later Apple, Facebook) logins remain.
- Prompt v2: Hungarian names for elements and modalities (an English
  "Fire" slipped into a summary); worker compare-models --summary.
- The worker also reads a lower-case openrouter_api_key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-16 17:12:17 +00:00

63 lines
2.6 KiB
Rust

//! Configuration from environment variables.
use std::time::Duration;
use anyhow::Context;
use lettre::message::Mailbox;
pub struct Config {
pub database_url: String,
/// `smtp://host:port` (plain, e.g. Mailpit), `smtp://user:pass@host:587?tls=required`
/// (STARTTLS) or `smtps://...` (TLS). Unset: e-mails stay in the outbox.
pub smtp_url: Option<String>,
/// Sender, e.g. `Sorsfonalak <noreply@sorsfonalak.hu>`.
pub mail_from: Mailbox,
/// Fallback polling interval; new e-mails are picked up at once via LISTEN/NOTIFY.
pub poll_interval: Duration,
/// After this many failed attempts an e-mail is marked as failed.
pub max_email_attempts: i32,
/// Unset: analyses stay pending.
pub openrouter_api_key: Option<String>,
pub openrouter_base_url: String,
/// Frontend address, sent to OpenRouter as `HTTP-Referer`.
pub public_base_url: Option<String>,
/// Analyses written at the same time.
pub analysis_concurrency: usize,
/// After this many failed attempts an analysis fails and is refunded.
pub max_analysis_attempts: i32,
}
fn var(name: &str) -> Option<String> {
std::env::var(name).ok().map(|v| v.trim().to_owned()).filter(|v| !v.is_empty())
}
fn parsed<T: std::str::FromStr>(name: &str, default: T) -> anyhow::Result<T>
where
T::Err: std::fmt::Display,
{
match var(name) {
Some(raw) => raw.parse().map_err(|e| anyhow::anyhow!("invalid {name} '{raw}': {e}")),
None => Ok(default),
}
}
impl Config {
pub fn from_env() -> anyhow::Result<Self> {
let mail_from = var("MAIL_FROM").unwrap_or_else(|| "Sorsfonalak <noreply@sorsfonalak.local>".into());
Ok(Self {
database_url: var("DATABASE_URL").context("DATABASE_URL is required")?,
smtp_url: var("SMTP_URL"),
mail_from: mail_from.parse().with_context(|| format!("invalid MAIL_FROM '{mail_from}'"))?,
poll_interval: Duration::from_secs(parsed("POLL_INTERVAL_SECS", 30)?),
max_email_attempts: parsed("MAX_EMAIL_ATTEMPTS", 8)?,
// The lower-case name is accepted too (as written in some secrets files).
openrouter_api_key: var("OPENROUTER_API_KEY").or_else(|| var("openrouter_api_key")),
openrouter_base_url: var("OPENROUTER_BASE_URL")
.unwrap_or_else(|| crate::analysis::openrouter::DEFAULT_BASE_URL.into()),
public_base_url: var("PUBLIC_BASE_URL"),
analysis_concurrency: parsed("ANALYSIS_CONCURRENCY", 4)?,
max_analysis_attempts: parsed("MAX_ANALYSIS_ATTEMPTS", 3)?,
})
}
}