refactor config
This commit is contained in:
parent
d90052cfd1
commit
04ece6afb1
5 changed files with 127 additions and 74 deletions
67
src/config.rs
Normal file
67
src/config.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
use std::env;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Config {
|
||||
pub database_url: String,
|
||||
pub port: u16,
|
||||
pub zen_api_key: Option<String>,
|
||||
pub tavily_api_key: Option<String>,
|
||||
pub authentik_issuer: String,
|
||||
pub authentik_client_id: String,
|
||||
pub authentik_client_secret: String,
|
||||
pub cors_allowed_origins: Option<String>,
|
||||
pub cookie_secure: bool,
|
||||
pub agent_max_turns: u32,
|
||||
pub agent_max_duration_secs: u64,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_env() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let database_url = env::var("DATABASE_URL").map_err(|_| "DATABASE_URL must be set")?;
|
||||
|
||||
let port = env::var("PORT")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(3000);
|
||||
|
||||
let zen_api_key = env::var("ZEN_API_KEY").ok();
|
||||
let tavily_api_key = env::var("TAVILY_API_KEY").ok();
|
||||
|
||||
let authentik_issuer =
|
||||
env::var("AUTHENTIK_ISSUER").map_err(|_| "AUTHENTIK_ISSUER must be set")?;
|
||||
let authentik_client_id =
|
||||
env::var("AUTHENTIK_CLIENT_ID").map_err(|_| "AUTHENTIK_CLIENT_ID must be set")?;
|
||||
let authentik_client_secret = env::var("AUTHENTIK_CLIENT_SECRET")
|
||||
.map_err(|_| "AUTHENTIK_CLIENT_SECRET must be set")?;
|
||||
|
||||
let cors_allowed_origins = env::var("CORS_ALLOWED_ORIGINS").ok();
|
||||
|
||||
let cookie_secure = env::var("COOKIE_SECURE")
|
||||
.map(|v| v == "true")
|
||||
.unwrap_or(false);
|
||||
|
||||
let agent_max_turns = env::var("AGENT_MAX_TURNS")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(20);
|
||||
|
||||
let agent_max_duration_secs = env::var("AGENT_MAX_DURATION_SECS")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(120);
|
||||
|
||||
Ok(Config {
|
||||
database_url,
|
||||
port,
|
||||
zen_api_key,
|
||||
tavily_api_key,
|
||||
authentik_issuer,
|
||||
authentik_client_id,
|
||||
authentik_client_secret,
|
||||
cors_allowed_origins,
|
||||
cookie_secure,
|
||||
agent_max_turns,
|
||||
agent_max_duration_secs,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue