extern crate sqlx; #[allow(unused_imports)] #[macro_use( slog_o, slog_info, slog_warn, slog_error, slog_log, slog_record, slog_record_static, slog_b, slog_kv )] extern crate slog; extern crate slog_async; pub mod forms; pub mod models; pub mod queries; use std::{fmt::Display, path::PathBuf, str::FromStr}; use actix_web::HttpResponse; use qrcode::types::QrError; use sqlx::{Pool, Sqlite}; use thiserror::Error; #[derive(Error, Debug)] pub enum ServerError { #[error("Failed to encrypt the password {0} - aborting!")] Argonautica(argonautica::Error), #[error("The database could not be used: {0}")] Database(#[from] sqlx::Error), #[error("The database could not be migrated: {0}")] DatabaseMigration(#[from] sqlx::migrate::MigrateError), #[error("The environment file could not be read")] Environment(#[from] std::env::VarError), #[error("The templates could not be rendered correctly: {0}")] Template(#[from] tera::Error), #[error("The qr-code could not be generated: {0}")] Qr(#[from] QrError), #[error("Some error happened during input and output: {0}")] Io(#[from] std::io::Error), #[error("Error: {0}")] User(String), } impl From for ServerError { fn from(e: argonautica::Error) -> Self { Self::Argonautica(e) } } impl actix_web::error::ResponseError for ServerError { fn error_response(&self) -> HttpResponse { match self { Self::Argonautica(_) => HttpResponse::InternalServerError().json("Argonautica Error"), Self::Database(e) => { HttpResponse::InternalServerError().json(format!("Database Error: {:?}", e)) } Self::DatabaseMigration(_) => { unimplemented!("A migration error should never be rendered") } Self::Environment(_) => HttpResponse::InternalServerError().json("Environment Error"), Self::Template(e) => { HttpResponse::InternalServerError().json(format!("Template Error: {:?}", e)) } Self::Qr(e) => { HttpResponse::InternalServerError().json(format!("Qr Code Error: {:?}", e)) } Self::Io(e) => HttpResponse::InternalServerError().json(format!("IO Error: {:?}", e)), Self::User(data) => HttpResponse::InternalServerError().json(data), } } } #[derive(Debug, Clone)] pub enum Protocol { Http, Https, } impl Display for Protocol { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Http => f.write_str("http"), Self::Https => f.write_str("https"), } } } impl FromStr for Protocol { type Err = ServerError; fn from_str(s: &str) -> Result { match s { "http" => Ok(Self::Http), "https" => Ok(Self::Https), _ => Err(ServerError::User("Failed to parse Protocol".to_owned())), } } } #[derive(Debug, Clone)] pub struct ServerConfig { pub secret: String, pub db: PathBuf, pub db_pool: Pool, pub public_url: String, pub internal_ip: String, pub port: u32, pub protocol: Protocol, pub log: slog::Logger, pub empty_forward_url: String, pub brand_name: String, } impl ServerConfig { #[must_use] pub fn to_env_strings(&self) -> Vec { vec![ format!("PSLINK_DATABASE=\"{}\"\n", self.db.display()), format!("PSLINK_PORT={}\n", self.port), format!("PSLINK_PUBLIC_URL=\"{}\"\n", self.public_url), format!("PSLINK_EMPTY_FORWARD_URL=\"{}\"\n", self.empty_forward_url), format!("PSLINK_BRAND_NAME=\"{}\"\n", self.brand_name), format!("PSLINK_IP=\"{}\"\n", self.internal_ip), format!("PSLINK_PROTOCOL=\"{}\"\n", self.protocol), concat!( "# The SECRET_KEY variable is used for password encryption.\n", "# If it is changed all existing passwords are invalid.\n" ) .to_owned(), format!("PSLINK_SECRET=\"{}\"\n", self.secret), ] } }