use std::path::PathBuf; use config::{Config, Environment, File as ConfigFile, FileFormat}; use eyre::{Result, eyre}; use fs_err::create_dir_all; use serde::{Deserialize, Serialize}; use tracing::info; use crate::atuin_server::database::DbSettings; #[derive(Clone, Debug, Deserialize, Serialize)] pub(crate) struct Metrics { #[serde(alias = "enabled")] pub(crate) enable: bool, pub(crate) host: String, pub(crate) port: u16, } impl Default for Metrics { fn default() -> Self { Self { enable: false, host: String::from("127.0.0.1"), port: 9001, } } } #[derive(Clone, Debug, Deserialize, Serialize)] pub(crate) struct Settings { pub(crate) host: String, pub(crate) port: u16, pub(crate) path: String, pub(crate) max_history_length: usize, pub(crate) max_record_size: usize, pub(crate) page_size: i64, pub(crate) metrics: Metrics, /// Advertise a version that is not what we are _actually_ running /// Many clients compare their version with api.atuin.sh, and if they differ, notify the user /// that an update is available. /// Now that we take beta releases, we should be able to advertise a different version to avoid /// notifying users when the server runs something that is not a stable release. pub(crate) fake_version: Option, #[serde(flatten)] #[expect(clippy::struct_field_names)] pub(crate) db_settings: DbSettings, } impl Settings { pub(crate) fn new() -> Result { let mut config_file = std::env::var("ATUIN_CONFIG_DIR").map_or_else( |_| { let mut config_file = PathBuf::new(); let config_dir = crate::atuin_common::utils::config_dir(); config_file.push(config_dir); config_file }, PathBuf::from, ); config_file.push("server.toml"); // create the config file if it does not exist let mut config_builder = Config::builder() .set_default("host", "127.0.0.1")? .set_default("port", 8888)? .set_default("max_history_length", 8192)? .set_default("max_record_size", 1024 * 1024 * 1024)? // pretty chonky .set_default("path", "")? .set_default("page_size", 1100)? .set_default("metrics.enable", false)? .set_default("metrics.host", "127.0.0.1")? .set_default("metrics.port", 9001)? .add_source( Environment::with_prefix("atuin") .prefix_separator("_") .separator("__"), ); config_builder = if config_file.exists() { config_builder.add_source(ConfigFile::new( config_file.to_str().unwrap(), FileFormat::Toml, )) } else { // TODO(@bpeetz): Rework the config handling, so that we can actually auto-write a // file with defaults. <2026-06-13> create_dir_all(config_file.parent().unwrap())?; info!( "No config file at: `{}`. Not adding one.", config_file.display() ); config_builder }; let config = config_builder.build()?; config .try_deserialize() .map_err(|e| eyre!("failed to deserialize: {}", e)) } }