Skip to main content

atuin_server/
settings.rs

1use std::{io::prelude::*, path::PathBuf};
2
3use atuin_server_database::DbSettings;
4use config::{Config, Environment, File as ConfigFile, FileFormat};
5use eyre::{Result, eyre};
6use fs_err::{File, create_dir_all};
7use serde::{Deserialize, Serialize};
8
9static EXAMPLE_CONFIG: &str = include_str!("../server.toml");
10
11#[derive(Clone, Debug, Deserialize, Serialize)]
12pub struct Metrics {
13    #[serde(alias = "enabled")]
14    pub enable: bool,
15    pub host: String,
16    pub port: u16,
17}
18
19impl Default for Metrics {
20    fn default() -> Self {
21        Self {
22            enable: false,
23            host: String::from("127.0.0.1"),
24            port: 9001,
25        }
26    }
27}
28
29#[derive(Clone, Debug, Deserialize, Serialize)]
30pub struct Settings {
31    pub host: String,
32    pub port: u16,
33    pub path: String,
34    pub open_registration: bool,
35    pub max_history_length: usize,
36    pub max_record_size: usize,
37    pub page_size: i64,
38    pub register_webhook_url: Option<String>,
39    pub register_webhook_username: String,
40    pub metrics: Metrics,
41
42    /// Enable legacy sync v1 routes (history-based sync)
43    /// Set to false to use only the newer record-based sync
44    pub sync_v1_enabled: bool,
45
46    /// Advertise a version that is not what we are _actually_ running
47    /// Many clients compare their version with api.atuin.sh, and if they differ, notify the user
48    /// that an update is available.
49    /// Now that we take beta releases, we should be able to advertise a different version to avoid
50    /// notifying users when the server runs something that is not a stable release.
51    pub fake_version: Option<String>,
52
53    #[serde(flatten)]
54    pub db_settings: DbSettings,
55}
56
57impl Settings {
58    pub fn new() -> Result<Self> {
59        let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
60            PathBuf::from(p)
61        } else {
62            let mut config_file = PathBuf::new();
63            let config_dir = atuin_common::utils::config_dir();
64            config_file.push(config_dir);
65            config_file
66        };
67
68        config_file.push("server.toml");
69
70        // create the config file if it does not exist
71        let mut config_builder = Config::builder()
72            .set_default("host", "127.0.0.1")?
73            .set_default("port", 8888)?
74            .set_default("open_registration", false)?
75            .set_default("max_history_length", 8192)?
76            .set_default("max_record_size", 1024 * 1024 * 1024)? // pretty chonky
77            .set_default("path", "")?
78            .set_default("register_webhook_username", "")?
79            .set_default("page_size", 1100)?
80            .set_default("metrics.enable", false)?
81            .set_default("metrics.host", "127.0.0.1")?
82            .set_default("metrics.port", 9001)?
83            .set_default("sync_v1_enabled", true)?
84            .add_source(
85                Environment::with_prefix("atuin")
86                    .prefix_separator("_")
87                    .separator("__"),
88            );
89
90        config_builder = if config_file.exists() {
91            config_builder.add_source(ConfigFile::new(
92                config_file.to_str().unwrap(),
93                FileFormat::Toml,
94            ))
95        } else {
96            create_dir_all(config_file.parent().unwrap())?;
97            let mut file = File::create(config_file)?;
98            file.write_all(EXAMPLE_CONFIG.as_bytes())?;
99
100            config_builder
101        };
102
103        let config = config_builder.build()?;
104
105        config
106            .try_deserialize()
107            .map_err(|e| eyre!("failed to deserialize: {}", e))
108    }
109}
110
111pub fn example_config() -> &'static str {
112    EXAMPLE_CONFIG
113}