atuin_server/
settings.rs

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