2022-04-28 11:53:59 -06:00
|
|
|
use std::{io::prelude::*, path::PathBuf};
|
2021-04-20 14:53:07 -06:00
|
|
|
|
2022-03-17 15:26:57 -06:00
|
|
|
use config::{Config, Environment, File as ConfigFile, FileFormat};
|
2021-04-20 14:53:07 -06:00
|
|
|
use eyre::{eyre, Result};
|
2022-04-28 11:53:59 -06:00
|
|
|
use fs_err::{create_dir_all, File};
|
2023-06-12 02:04:35 -06:00
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
2021-04-20 14:53:07 -06:00
|
|
|
|
2022-03-17 15:26:57 -06:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2023-06-12 02:04:35 -06:00
|
|
|
pub struct Settings<DbSettings> {
|
2021-04-20 14:53:07 -06:00
|
|
|
pub host: String,
|
|
|
|
pub port: u16,
|
2022-07-26 01:05:34 -06:00
|
|
|
pub path: String,
|
2021-04-20 14:53:07 -06:00
|
|
|
pub open_registration: bool,
|
2022-06-10 03:00:59 -06:00
|
|
|
pub max_history_length: usize,
|
2023-07-14 13:44:08 -06:00
|
|
|
pub max_record_size: usize,
|
2023-05-21 09:21:51 -06:00
|
|
|
pub page_size: i64,
|
2023-03-07 15:09:19 -07:00
|
|
|
pub register_webhook_url: Option<String>,
|
|
|
|
pub register_webhook_username: String,
|
2023-06-12 02:04:35 -06:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub db_settings: DbSettings,
|
2021-04-20 14:53:07 -06:00
|
|
|
}
|
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
impl<DbSettings: DeserializeOwned> Settings<DbSettings> {
|
2021-04-20 14:53:07 -06:00
|
|
|
pub fn new() -> Result<Self> {
|
2021-04-21 11:13:51 -06:00
|
|
|
let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
|
2021-04-20 14:53:07 -06:00
|
|
|
PathBuf::from(p)
|
|
|
|
} else {
|
|
|
|
let mut config_file = PathBuf::new();
|
2022-05-12 23:07:46 -06:00
|
|
|
let config_dir = atuin_common::utils::config_dir();
|
2021-04-20 14:53:07 -06:00
|
|
|
config_file.push(config_dir);
|
|
|
|
config_file
|
|
|
|
};
|
|
|
|
|
2021-04-21 11:13:51 -06:00
|
|
|
config_file.push("server.toml");
|
|
|
|
|
2021-04-20 14:53:07 -06:00
|
|
|
// create the config file if it does not exist
|
2022-03-17 15:26:57 -06:00
|
|
|
let mut config_builder = Config::builder()
|
|
|
|
.set_default("host", "127.0.0.1")?
|
|
|
|
.set_default("port", 8888)?
|
|
|
|
.set_default("open_registration", false)?
|
2022-06-10 03:00:59 -06:00
|
|
|
.set_default("max_history_length", 8192)?
|
2023-07-14 13:44:08 -06:00
|
|
|
.set_default("max_record_size", 1024 * 1024 * 1024)? // pretty chonky
|
2022-07-26 01:05:34 -06:00
|
|
|
.set_default("path", "")?
|
2023-03-07 15:09:19 -07:00
|
|
|
.set_default("register_webhook_username", "")?
|
2023-05-21 09:21:51 -06:00
|
|
|
.set_default("page_size", 1100)?
|
2022-04-12 14:47:07 -06:00
|
|
|
.add_source(
|
|
|
|
Environment::with_prefix("atuin")
|
|
|
|
.prefix_separator("_")
|
|
|
|
.separator("__"),
|
|
|
|
);
|
2022-03-17 15:26:57 -06:00
|
|
|
|
|
|
|
config_builder = if config_file.exists() {
|
|
|
|
config_builder.add_source(ConfigFile::new(
|
|
|
|
config_file.to_str().unwrap(),
|
|
|
|
FileFormat::Toml,
|
|
|
|
))
|
2021-04-20 14:53:07 -06:00
|
|
|
} else {
|
|
|
|
let example_config = include_bytes!("../server.toml");
|
2022-05-12 23:07:46 -06:00
|
|
|
create_dir_all(config_file.parent().unwrap())?;
|
2021-04-20 14:53:07 -06:00
|
|
|
let mut file = File::create(config_file)?;
|
|
|
|
file.write_all(example_config)?;
|
|
|
|
|
2022-03-17 15:26:57 -06:00
|
|
|
config_builder
|
|
|
|
};
|
2021-04-20 14:53:07 -06:00
|
|
|
|
2022-03-17 15:26:57 -06:00
|
|
|
let config = config_builder.build()?;
|
2021-04-20 14:53:07 -06:00
|
|
|
|
2022-03-17 15:26:57 -06:00
|
|
|
config
|
|
|
|
.try_deserialize()
|
2021-04-20 14:53:07 -06:00
|
|
|
.map_err(|e| eyre!("failed to deserialize: {}", e))
|
|
|
|
}
|
|
|
|
}
|