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_record_size: usize,
36    pub register_webhook_url: Option<url::Url>,
37    pub register_webhook_username: String,
38    pub metrics: Metrics,
39
40    /// Advertise a version that is not what we are _actually_ running
41    /// Many clients compare their version with api.atuin.sh, and if they differ, notify the user
42    /// that an update is available.
43    /// Now that we take beta releases, we should be able to advertise a different version to avoid
44    /// notifying users when the server runs something that is not a stable release.
45    pub fake_version: Option<String>,
46
47    #[serde(flatten)]
48    pub db_settings: DbSettings,
49}
50
51impl Settings {
52    pub fn new() -> Result<Self> {
53        let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
54            PathBuf::from(p)
55        } else {
56            let mut config_file = PathBuf::new();
57            let config_dir = atuin_common::utils::config_dir();
58            config_file.push(config_dir);
59            config_file
60        };
61
62        config_file.push("server.toml");
63
64        // create the config file if it does not exist
65        let mut config_builder = Config::builder()
66            .set_default("host", "127.0.0.1")?
67            .set_default("port", 8888)?
68            .set_default("open_registration", false)?
69            .set_default("max_record_size", 1024 * 1024 * 1024)? // pretty chonky
70            .set_default("path", "")?
71            .set_default("register_webhook_username", "")?
72            .set_default("metrics.enable", false)?
73            .set_default("metrics.host", "127.0.0.1")?
74            .set_default("metrics.port", 9001)?
75            .add_source(
76                Environment::with_prefix("atuin")
77                    .prefix_separator("_")
78                    .separator("__"),
79            );
80
81        config_builder = if config_file.exists() {
82            config_builder.add_source(ConfigFile::new(
83                config_file.to_str().unwrap(),
84                FileFormat::Toml,
85            ))
86        } else {
87            create_dir_all(config_file.parent().unwrap())?;
88            let mut file = File::create(config_file)?;
89            file.write_all(EXAMPLE_CONFIG.as_bytes())?;
90
91            config_builder
92        };
93
94        let config = config_builder.build()?;
95
96        config
97            .try_deserialize()
98            .map_err(|e| eyre!("failed to deserialize: {}", e))
99    }
100}
101
102pub fn example_config() -> &'static str {
103    EXAMPLE_CONFIG
104}