From 9f16f76bd8d15824e27e971245ea93271fe76b29 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 9 Apr 2021 12:40:21 +0100 Subject: [PATCH] Update config --- config.toml | 37 +++++++++++++++++++++++++++++++++++++ src/command/server.rs | 25 ++++++++++++++++++++++--- src/main.rs | 2 +- src/remote/database.rs | 2 +- src/remote/server.rs | 9 +++++---- src/settings.rs | 34 +++++++++++++++++----------------- 6 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 config.toml diff --git a/config.toml b/config.toml new file mode 100644 index 00000000..60ca33bb --- /dev/null +++ b/config.toml @@ -0,0 +1,37 @@ +# A'tuin example config + +# This section specifies the config for a local client, +# ie where your shell history is on your local machine +[local] +# (optional) +# where to store your database, default is your system data directory +# mac: ~/Library/Application Support/com.elliehuxtable.atuin/history.db +# linux: ~/.local/share/atuin/history.db +db_path = "~/.history.db" +# (optional, default us) +# date format used, either "us" or "uk" +dialect = "uk" +# (optional, default false) +# whether to enable sync of history. requires authentication +sync = false +# (optional, default 5m) +# how often to sync history. note that this is only triggered when a command is ran, and the last sync was >= this value ago +sync_frequency = "5m" +# (optional, default https://atuin.elliehuxtable.com) +# address of the sync server +sync_address = "https://atuin.elliehuxtable.com" + +# This section configures the sync server, if you decide to host your own +[remote] +# (optional, default 127.0.0.1) +# host to bind, can also be passed via CLI args +host = "127.0.0.1" +# (optional, default 8888) +# port to bind, can also be passed via CLI args +port = 8888 +# (optional, default false) +# whether to allow anyone to register an account +open_registration = false +# (required) +# URI for postgres (using development creds here) +db_uri="postgres://username:password@localhost/atuin" diff --git a/src/command/server.rs b/src/command/server.rs index 9d9bcb3a..5156f409 100644 --- a/src/command/server.rs +++ b/src/command/server.rs @@ -6,13 +6,32 @@ use crate::settings::Settings; #[derive(StructOpt)] pub enum Cmd { - Start { host: Vec }, + #[structopt( + about="starts the server", + aliases=&["s", "st", "sta", "star"], + )] + Start { + #[structopt(about = "specify the host address to bind", long, short)] + host: Option, + + #[structopt(about = "specify the port to bind", long, short)] + port: Option, + }, } -#[allow(clippy::unused_self)] // I'll use it later impl Cmd { pub fn run(&self, settings: &Settings) -> Result<()> { - server::launch(settings); + match self { + Self::Start { host, port } => { + let host = host.as_ref().map_or( + settings.remote.host.clone(), + std::string::ToString::to_string, + ); + let port = port.map_or(settings.remote.port, |p| p); + + server::launch(settings, host, port); + } + } Ok(()) } } diff --git a/src/main.rs b/src/main.rs index 3c4a05e4..bac75362 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,7 @@ impl Atuin { let path = shellexpand::full(path)?; PathBuf::from(path.as_ref()) } else { - PathBuf::from(settings.local.db.path.as_str()) + PathBuf::from(settings.local.db_path.as_str()) }; let mut db = Sqlite::new(db_path)?; diff --git a/src/remote/database.rs b/src/remote/database.rs index 4f386def..fabd07de 100644 --- a/src/remote/database.rs +++ b/src/remote/database.rs @@ -8,7 +8,7 @@ pub struct AtuinDbConn(diesel::PgConnection); // TODO: connection pooling pub fn establish_connection(settings: &Settings) -> PgConnection { - let database_url = &settings.remote.db.url; + let database_url = &settings.remote.db_uri; PgConnection::establish(database_url) .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } diff --git a/src/remote/server.rs b/src/remote/server.rs index 4409f646..cd2ca7b8 100644 --- a/src/remote/server.rs +++ b/src/remote/server.rs @@ -16,25 +16,26 @@ use super::auth::*; embed_migrations!("migrations"); -pub fn launch(settings: &Settings) { +pub fn launch(settings: &Settings, host: String, port: u16) { let mut database_config = HashMap::new(); let mut databases = HashMap::new(); - database_config.insert("url", Value::from(settings.remote.db.url.clone())); + database_config.insert("url", Value::from(settings.remote.db_uri.clone())); databases.insert("atuin", Value::from(database_config)); let connection = establish_connection(settings); embedded_migrations::run(&connection).expect("failed to run migrations"); let config = Config::build(Environment::Production) - .address("0.0.0.0") + .address(host) .log_level(LoggingLevel::Normal) - .port(8080) + .port(port) .extra("databases", databases) .finalize() .unwrap(); let app = rocket::custom(config); + app.mount("/", routes![index, register, add_history, login]) .attach(AtuinDbConn::fairing()) .register(catchers![internal_error, bad_request]) diff --git a/src/settings.rs b/src/settings.rs index 6f29afd2..0e554bed 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -5,26 +5,21 @@ use directories::ProjectDirs; use eyre::{eyre, Result}; use std::fs; -#[derive(Debug, Deserialize)] -pub struct LocalDatabase { - pub path: String, -} - -#[derive(Debug, Deserialize)] -pub struct RemoteDatabase { - pub url: String, -} - #[derive(Debug, Deserialize)] pub struct Local { - pub server_address: String, pub dialect: String, - pub db: LocalDatabase, + pub sync: bool, + pub sync_address: String, + pub sync_frequency: String, + pub db_path: String, } #[derive(Debug, Deserialize)] pub struct Remote { - pub db: RemoteDatabase, + pub host: String, + pub port: u16, + pub db_uri: String, + pub open_registration: bool, } #[derive(Debug, Deserialize)] @@ -56,18 +51,23 @@ impl Settings { .data_dir() .join("history.db"); - s.set_default("local.server_address", "https://atuin.elliehuxtable.com")?; + s.set_default("local.db_path", db_path.to_str())?; s.set_default("local.dialect", "us")?; - s.set_default("local.db.path", db_path.to_str())?; + s.set_default("local.sync", false)?; + s.set_default("local.sync_frequency", "5m")?; + s.set_default("local.sync_address", "https://atuin.ellie.wtf")?; - s.set_default("remote.db.url", "please set a postgres url")?; + s.set_default("remote.host", "127.0.0.1")?; + s.set_default("remote.port", 8888)?; + s.set_default("remote.open_registration", false)?; + s.set_default("remote.db_uri", "please set a postgres url")?; if config_file.exists() { s.merge(File::with_name(config_file.to_str().unwrap()))?; } // all paths should be expanded - let db_path = s.get_str("local.db.path")?; + let db_path = s.get_str("local.db_path")?; let db_path = shellexpand::full(db_path.as_str())?; s.set("local.db.path", db_path.to_string())?;