1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
use std::{io::prelude::*, path::PathBuf};
use crate::atuin_server_database::DbSettings;
use config::{Config, Environment, File as ConfigFile, FileFormat};
use eyre::{Result, eyre};
use fs_err::{File, create_dir_all};
use serde::{Deserialize, Serialize};
#[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) open_registration: bool,
pub(crate) max_history_length: usize,
pub(crate) max_record_size: usize,
pub(crate) page_size: i64,
pub(crate) register_webhook_url: Option<String>,
pub(crate) register_webhook_username: String,
pub(crate) metrics: Metrics,
/// Enable legacy sync v1 routes (history-based sync)
/// Set to false to use only the newer record-based sync
pub(crate) sync_v1_enabled: bool,
/// 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<String>,
#[serde(flatten)]
pub(crate) db_settings: DbSettings,
}
impl Settings {
pub(crate) fn new() -> Result<Self> {
let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
PathBuf::from(p)
} else {
let mut config_file = PathBuf::new();
let config_dir = crate::atuin_common::utils::config_dir();
config_file.push(config_dir);
config_file
};
config_file.push("server.toml");
// create the config file if it does not exist
let config_builder = Config::builder()
.set_default("host", "127.0.0.1")?
.set_default("port", 8888)?
.set_default("open_registration", false)?
.set_default("max_history_length", 8192)?
.set_default("max_record_size", 1024 * 1024 * 1024)? // pretty chonky
.set_default("path", "")?
.set_default("register_webhook_username", "")?
.set_default("page_size", 1100)?
.set_default("metrics.enable", false)?
.set_default("metrics.host", "127.0.0.1")?
.set_default("metrics.port", 9001)?
.set_default("sync_v1_enabled", true)?
.add_source(
Environment::with_prefix("atuin")
.prefix_separator("_")
.separator("__"),
);
let config = if config_file.exists() {
config_builder
.add_source(ConfigFile::new(
config_file.to_str().unwrap(),
FileFormat::Toml,
))
.build()?
} else {
create_dir_all(config_file.parent().unwrap())?;
let mut file = File::create(config_file)?;
let config = config_builder.build()?;
// TODO(@bpeetz): I'm quiet unsure, if this will work <2026-06-10>
file.write_all(config.cache.to_string().as_bytes())?;
config
};
config
.try_deserialize()
.map_err(|e| eyre!("failed to deserialize: {}", e))
}
}
|