diff options
| author | Ellie Huxtable <e@elm.sh> | 2021-04-20 21:53:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-20 20:53:07 +0000 |
| commit | a21737e2b7f8d1e426726bdd7536033f299d476a (patch) | |
| tree | e940afdff9c145d25d9a2895fd44a77d70719a2e /atuin-server/src/settings.rs | |
| parent | Switch to Warp + SQLx, use async, switch to Rust stable (#36) (diff) | |
| download | atuin-a21737e2b7f8d1e426726bdd7536033f299d476a.zip | |
Use cargo workspaces (#37)
* Switch to Cargo workspaces
Breaking things into "client", "server" and "common" makes managing the
codebase much easier!
client - anything running on a user's machine for adding history
server - handles storing/syncing history and running a HTTP server
common - request/response API definitions, common utils, etc
* Update dockerfile
Diffstat (limited to '')
| -rw-r--r-- | atuin-server/src/settings.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/atuin-server/src/settings.rs b/atuin-server/src/settings.rs new file mode 100644 index 00000000..596b9018 --- /dev/null +++ b/atuin-server/src/settings.rs @@ -0,0 +1,57 @@ +use std::fs::{create_dir_all, File}; +use std::io::prelude::*; +use std::path::PathBuf; + +use config::{Config, Environment, File as ConfigFile}; +use directories::ProjectDirs; +use eyre::{eyre, Result}; + +pub const HISTORY_PAGE_SIZE: i64 = 100; + +#[derive(Clone, Debug, Deserialize)] +pub struct Settings { + pub host: String, + pub port: u16, + pub db_uri: String, + pub open_registration: bool, +} + +impl Settings { + pub fn new() -> Result<Self> { + let config_dir = ProjectDirs::from("com", "elliehuxtable", "atuin").unwrap(); + let config_dir = config_dir.config_dir(); + + create_dir_all(config_dir)?; + + let config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG") { + PathBuf::from(p) + } else { + let mut config_file = PathBuf::new(); + config_file.push(config_dir); + config_file.push("server.toml"); + config_file + }; + + // create the config file if it does not exist + + let mut s = Config::new(); + + if config_file.exists() { + s.merge(ConfigFile::with_name(config_file.to_str().unwrap()))?; + } else { + let example_config = include_bytes!("../server.toml"); + let mut file = File::create(config_file)?; + file.write_all(example_config)?; + } + + s.set_default("host", "127.0.0.1")?; + s.set_default("port", 8888)?; + s.set_default("open_registration", false)?; + s.set_default("db_uri", "default_uri")?; + + s.merge(Environment::with_prefix("atuin").separator("_"))?; + + s.try_into() + .map_err(|e| eyre!("failed to deserialize: {}", e)) + } +} |
