Update config

This commit is contained in:
Ellie Huxtable 2021-04-09 12:40:21 +01:00
parent b5845bc3a1
commit 9f16f76bd8
6 changed files with 83 additions and 26 deletions

37
config.toml Normal file
View file

@ -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"

View file

@ -6,13 +6,32 @@ use crate::settings::Settings;
#[derive(StructOpt)]
pub enum Cmd {
Start { host: Vec<String> },
#[structopt(
about="starts the server",
aliases=&["s", "st", "sta", "star"],
)]
Start {
#[structopt(about = "specify the host address to bind", long, short)]
host: Option<String>,
#[structopt(about = "specify the port to bind", long, short)]
port: Option<u16>,
},
}
#[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(())
}
}

View file

@ -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)?;

View file

@ -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))
}

View file

@ -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])

View file

@ -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())?;