diff options
author | Ellie Huxtable <e@elm.sh> | 2021-04-14 18:40:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-14 17:40:50 +0000 |
commit | f6de558070c4ed4dbecf4bbbf4693e396a5577dc (patch) | |
tree | 174dcd5f1341e2845ea30cff6521e36170e8a0d5 /src | |
parent | 983f9e6b7bc801a8ffb03c5241498df134a2d69b (diff) |
Optimise docker (#34)
* Smaller dockerfile, better error handling
* Add config dir
Diffstat (limited to 'src')
-rw-r--r-- | src/command/server.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/remote/database.rs | 16 | ||||
-rw-r--r-- | src/remote/server.rs | 12 | ||||
-rw-r--r-- | src/settings.rs | 2 |
5 files changed, 24 insertions, 11 deletions
diff --git a/src/command/server.rs b/src/command/server.rs index ba2a9a2f..bf757948 100644 --- a/src/command/server.rs +++ b/src/command/server.rs @@ -29,9 +29,8 @@ impl Cmd { ); let port = port.map_or(settings.server.port, |p| p); - server::launch(settings, host, port); + server::launch(settings, host, port) } } - Ok(()) } } diff --git a/src/main.rs b/src/main.rs index ae459807..94c7366d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use eyre::{eyre, Result}; +use human_panic::setup_panic; use structopt::{clap::AppSettings, StructOpt}; #[macro_use] @@ -73,6 +74,7 @@ impl Atuin { } fn main() -> Result<()> { + setup_panic!(); let settings = Settings::new()?; fern::Dispatch::new() diff --git a/src/remote/database.rs b/src/remote/database.rs index ddcffda0..03973ca1 100644 --- a/src/remote/database.rs +++ b/src/remote/database.rs @@ -1,5 +1,6 @@ use diesel::pg::PgConnection; use diesel::prelude::*; +use eyre::{eyre, Result}; use crate::settings::Settings; @@ -7,8 +8,15 @@ use crate::settings::Settings; pub struct AtuinDbConn(diesel::PgConnection); // TODO: connection pooling -pub fn establish_connection(settings: &Settings) -> PgConnection { - let database_url = &settings.server.db_uri; - PgConnection::establish(database_url) - .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) +pub fn establish_connection(settings: &Settings) -> Result<PgConnection> { + if settings.server.db_uri == "default_uri" { + Err(eyre!( + "Please configure your database! Set db_uri in config.toml" + )) + } else { + let database_url = &settings.server.db_uri; + let conn = PgConnection::establish(database_url)?; + + Ok(conn) + } } diff --git a/src/remote/server.rs b/src/remote/server.rs index de58397d..ee481ca4 100644 --- a/src/remote/server.rs +++ b/src/remote/server.rs @@ -1,5 +1,3 @@ -use rocket::config::{Config, Environment, LoggingLevel, Value}; - use std::collections::HashMap; use crate::remote::database::establish_connection; @@ -7,6 +5,9 @@ use crate::settings::Settings; use super::database::AtuinDbConn; +use eyre::Result; +use rocket::config::{Config, Environment, LoggingLevel, Value}; + // a bunch of these imports are generated by macros, it's easier to wildcard #[allow(clippy::clippy::wildcard_imports)] use super::views::*; @@ -16,7 +17,7 @@ use super::auth::*; embed_migrations!("migrations"); -pub fn launch(settings: &Settings, host: String, port: u16) { +pub fn launch(settings: &Settings, host: String, port: u16) -> Result<()> { let settings: Settings = settings.clone(); // clone so rocket can manage it let mut database_config = HashMap::new(); @@ -25,7 +26,8 @@ pub fn launch(settings: &Settings, host: String, port: u16) { database_config.insert("url", Value::from(settings.server.db_uri.clone())); databases.insert("atuin", Value::from(database_config)); - let connection = establish_connection(&settings); + let connection = establish_connection(&settings)?; + embedded_migrations::run(&connection).expect("failed to run migrations"); let config = Config::build(Environment::Production) @@ -54,4 +56,6 @@ pub fn launch(settings: &Settings, host: String, port: u16) { .attach(AtuinDbConn::fairing()) .register(catchers![internal_error, bad_request]) .launch(); + + Ok(()) } diff --git a/src/settings.rs b/src/settings.rs index 5d19699b..f3bc62e6 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -133,7 +133,7 @@ impl Settings { s.set_default("server.host", "127.0.0.1")?; s.set_default("server.port", 8888)?; s.set_default("server.open_registration", false)?; - s.set_default("server.db_uri", "DEFAULT POSTGRES URI, PLEASE CHANGE")?; + s.set_default("server.db_uri", "default_uri")?; if config_file.exists() { s.merge(ConfigFile::with_name(config_file.to_str().unwrap()))?; |