Use the anyhow crate for Errors in the cli

This commit is contained in:
Dietrich 2021-04-02 17:41:27 +02:00
parent 583e9908bf
commit 76b1f53120
Signed by: dietrich
GPG Key ID: 9F3C20C0F85DF67C
5 changed files with 53 additions and 9 deletions

7
Cargo.lock generated
View File

@ -422,6 +422,12 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "anyhow"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b"
[[package]] [[package]]
name = "arc-swap" name = "arc-swap"
version = "1.2.0" version = "1.2.0"
@ -2376,6 +2382,7 @@ dependencies = [
"actix-slog", "actix-slog",
"actix-web", "actix-web",
"actix-web-static-files", "actix-web-static-files",
"anyhow",
"argonautica", "argonautica",
"chrono", "chrono",
"clap", "clap",

View File

@ -36,6 +36,7 @@ clap = "2.33"
fluent-templates = { version = "0.6", features = ["tera"] } fluent-templates = { version = "0.6", features = ["tera"] }
fluent-langneg = "0.13" fluent-langneg = "0.13"
thiserror = "1.0" thiserror = "1.0"
anyhow = "1.0"
[build-dependencies] [build-dependencies]
actix-web-static-files = "3.0" actix-web-static-files = "3.0"

View File

@ -96,6 +96,11 @@
"name": "role", "name": "role",
"ordinal": 4, "ordinal": 4,
"type_info": "Int64" "type_info": "Int64"
},
{
"name": "language",
"ordinal": 5,
"type_info": "Text"
} }
], ],
"parameters": { "parameters": {
@ -106,6 +111,7 @@
false, false,
false, false,
false, false,
false,
false false
] ]
} }
@ -138,6 +144,11 @@
"name": "role", "name": "role",
"ordinal": 4, "ordinal": 4,
"type_info": "Int64" "type_info": "Int64"
},
{
"name": "language",
"ordinal": 5,
"type_info": "Text"
} }
], ],
"parameters": { "parameters": {
@ -148,6 +159,7 @@
false, false,
false, false,
false, false,
false,
false false
] ]
} }
@ -230,6 +242,11 @@
"name": "role", "name": "role",
"ordinal": 4, "ordinal": 4,
"type_info": "Int64" "type_info": "Int64"
},
{
"name": "language",
"ordinal": 5,
"type_info": "Text"
} }
], ],
"parameters": { "parameters": {
@ -240,6 +257,7 @@
false, false,
false, false,
false, false,
false,
false false
] ]
} }
@ -261,5 +279,15 @@
false false
] ]
} }
},
"ba18635aa20b30d92172fa60fa22c6dba7e5cb2f57106e9d13cdab556af80fd3": {
"query": "UPDATE users SET language = ? where id = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
}
} }
} }

View File

@ -16,14 +16,15 @@ extern crate slog_async;
mod cli; mod cli;
mod views; mod views;
use pslink::ServerConfig;
use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use anyhow::{Context, Result};
use fluent_templates::{static_loader, FluentLoader}; use fluent_templates::{static_loader, FluentLoader};
use tera::Tera; use tera::Tera;
use pslink::{ServerConfig, ServerError};
include!(concat!(env!("OUT_DIR"), "/generated.rs")); include!(concat!(env!("OUT_DIR"), "/generated.rs"));
static_loader! { static_loader! {
@ -33,7 +34,7 @@ static_loader! {
}; };
} }
fn build_tera() -> Tera { fn build_tera() -> Result<Tera> {
let mut tera = Tera::default(); let mut tera = Tera::default();
// Add translation support // Add translation support
@ -77,12 +78,12 @@ fn build_tera() -> Tera {
include_str!("../../../templates/view_profile.html"), include_str!("../../../templates/view_profile.html"),
), ),
]) ])
.expect("failed to parse templates"); .context("Failed to load Templates")?;
tera Ok(tera)
} }
#[allow(clippy::future_not_send)] #[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); let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
slog_info!( slog_info!(
@ -185,15 +186,21 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> {
// redirect to the url hidden behind the code // redirect to the url hidden behind the code
.route("/{redirect_id}", web::get().to(views::redirect)) .route("/{redirect_id}", web::get().to(views::redirect))
}) })
.bind(host_port)? .bind(host_port)
.context("Failed to bind to port")?
.run() .run()
.await .await
.context("Failed to run the webservice")
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> std::result::Result<(), ServerError> {
match cli::setup().await { 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) => { Ok(None) => {
std::thread::sleep(std::time::Duration::from_millis(100)); std::thread::sleep(std::time::Duration::from_millis(100));
std::process::exit(0); std::process::exit(0);

View File

@ -151,6 +151,7 @@ impl NewUser {
Ok(hash) Ok(hash)
} }
/// Insert this user into the database /// Insert this user into the database
/// ///
/// # Errors /// # Errors