From 76b1f53120a3ac71d2f1bb07777966011bad3142 Mon Sep 17 00:00:00 2001 From: Dietrich Date: Fri, 2 Apr 2021 17:41:27 +0200 Subject: [PATCH] Use the anyhow crate for Errors in the cli --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + sqlx-data.json | 28 ++++++++++++++++++++++++++++ src/bin/pslink/main.rs | 25 ++++++++++++++++--------- src/models.rs | 1 + 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0675883..79ab647 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -422,6 +422,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "anyhow" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" + [[package]] name = "arc-swap" version = "1.2.0" @@ -2376,6 +2382,7 @@ dependencies = [ "actix-slog", "actix-web", "actix-web-static-files", + "anyhow", "argonautica", "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index baa876d..2791fd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ clap = "2.33" fluent-templates = { version = "0.6", features = ["tera"] } fluent-langneg = "0.13" thiserror = "1.0" +anyhow = "1.0" [build-dependencies] actix-web-static-files = "3.0" diff --git a/sqlx-data.json b/sqlx-data.json index 1e5e6c5..5409d4a 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -96,6 +96,11 @@ "name": "role", "ordinal": 4, "type_info": "Int64" + }, + { + "name": "language", + "ordinal": 5, + "type_info": "Text" } ], "parameters": { @@ -106,6 +111,7 @@ false, false, false, + false, false ] } @@ -138,6 +144,11 @@ "name": "role", "ordinal": 4, "type_info": "Int64" + }, + { + "name": "language", + "ordinal": 5, + "type_info": "Text" } ], "parameters": { @@ -148,6 +159,7 @@ false, false, false, + false, false ] } @@ -230,6 +242,11 @@ "name": "role", "ordinal": 4, "type_info": "Int64" + }, + { + "name": "language", + "ordinal": 5, + "type_info": "Text" } ], "parameters": { @@ -240,6 +257,7 @@ false, false, false, + false, false ] } @@ -261,5 +279,15 @@ false ] } + }, + "ba18635aa20b30d92172fa60fa22c6dba7e5cb2f57106e9d13cdab556af80fd3": { + "query": "UPDATE users SET language = ? where id = ?", + "describe": { + "columns": [], + "parameters": { + "Right": 2 + }, + "nullable": [] + } } } \ No newline at end of file diff --git a/src/bin/pslink/main.rs b/src/bin/pslink/main.rs index ac78b8a..5cd7778 100644 --- a/src/bin/pslink/main.rs +++ b/src/bin/pslink/main.rs @@ -16,14 +16,15 @@ extern crate slog_async; mod cli; mod views; -use pslink::ServerConfig; use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_web::{web, App, HttpServer}; - +use anyhow::{Context, Result}; use fluent_templates::{static_loader, FluentLoader}; use tera::Tera; +use pslink::{ServerConfig, ServerError}; + include!(concat!(env!("OUT_DIR"), "/generated.rs")); static_loader! { @@ -33,7 +34,7 @@ static_loader! { }; } -fn build_tera() -> Tera { +fn build_tera() -> Result { let mut tera = Tera::default(); // Add translation support @@ -77,12 +78,12 @@ fn build_tera() -> Tera { include_str!("../../../templates/view_profile.html"), ), ]) - .expect("failed to parse templates"); - tera + .context("Failed to load Templates")?; + Ok(tera) } #[allow(clippy::future_not_send)] -async fn webservice(server_config: ServerConfig) -> std::io::Result<()> { +async fn webservice(server_config: ServerConfig) -> Result<()> { let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port); slog_info!( @@ -185,15 +186,21 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> { // redirect to the url hidden behind the code .route("/{redirect_id}", web::get().to(views::redirect)) }) - .bind(host_port)? + .bind(host_port) + .context("Failed to bind to port")? .run() .await + .context("Failed to run the webservice") } #[actix_web::main] -async fn main() -> Result<(), std::io::Error> { +async fn main() -> std::result::Result<(), ServerError> { match cli::setup().await { - Ok(Some(server_config)) => webservice(server_config).await, + Ok(Some(server_config)) => webservice(server_config).await.map_err(|e| { + println!("{:?}", e); + std::thread::sleep(std::time::Duration::from_millis(100)); + std::process::exit(0); + }), Ok(None) => { std::thread::sleep(std::time::Duration::from_millis(100)); std::process::exit(0); diff --git a/src/models.rs b/src/models.rs index 6d43ab6..2cdad31 100644 --- a/src/models.rs +++ b/src/models.rs @@ -151,6 +151,7 @@ impl NewUser { Ok(hash) } + /// Insert this user into the database /// /// # Errors