diff --git a/Caddyfile b/Caddyfile deleted file mode 100644 index 6cee112..0000000 --- a/Caddyfile +++ /dev/null @@ -1,27 +0,0 @@ -{ - auto_https off - - admin unix//test -} - -:80 { - handle_path /_sscdc/* { - reverse_proxy unix//some/socket - } -} - -http://moritzruth.de { - handle_path /_sscdc/* { - reverse_proxy unix//some/socket - } - - handle { - redir /kontakt /contact permanent - header /assets/* Cache-Control "public, max-age=604800, immutable" - root * /home/moritz/dev/moritzruth/sscdc/run/sites/moritzruth.de - try_files {path} {path}/index.html /index.html - file_server { - precompressed br gzip - } - } -} \ No newline at end of file diff --git a/src/api.rs b/src/api.rs index 5dae5ff..63f0e94 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,11 +1,11 @@ use crate::config::Config; use crate::sites::SitesWorker; +use async_std::task::JoinHandle; +use async_std::{fs, task}; use camino::Utf8Path; use color_eyre::eyre::{WrapErr, eyre}; use serde::Deserialize; use std::sync::Arc; -use async_std::{fs, task}; -use async_std::task::JoinHandle; use tide::{Request, StatusCode}; #[derive(Clone)] diff --git a/src/caddy.rs b/src/caddy.rs index 0382abf..eacb2b5 100644 --- a/src/caddy.rs +++ b/src/caddy.rs @@ -1,4 +1,6 @@ +use async_std::io::WriteExt; use async_std::os::unix::net::UnixStream; +use async_std::process::Command; use camino::{Utf8Path, Utf8PathBuf}; use color_eyre::Result; use color_eyre::eyre::{OptionExt, eyre}; @@ -9,8 +11,6 @@ use serde_json::json; use std::process::Stdio; use std::str::FromStr; use std::time::Duration; -use async_std::io::WriteExt; -use async_std::process::Command; pub struct CaddyController { api_socket_path: Utf8PathBuf, @@ -158,7 +158,7 @@ pub async fn start_caddy(api_socket_path: &Utf8Path, sockets_directory_path: &Ut let caddy_path = option_env!("CADDY_PATH").unwrap_or("caddy"); debug!("Caddy path: {}", caddy_path); - + // Spawn a new proxy. let process = Command::new(caddy_path).args(&["run", "--config", "-"]).stdin(Stdio::piped()).spawn()?; diff --git a/src/config.rs b/src/config.rs index f50ec7b..e50b728 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,12 @@ -use color_eyre::Result; -use figment::providers::{Format, Toml}; -use figment::Figment; -use serde::Deserialize; -use std::collections::HashMap; use async_std::task; +use color_eyre::Result; use color_eyre::eyre::WrapErr; +use figment::Figment; +use figment::providers::{Format, Toml}; use once_cell::sync::Lazy; use regex::Regex; +use serde::Deserialize; +use std::collections::HashMap; use validator::Validate; #[derive(Deserialize, Debug, Validate)] @@ -15,7 +15,7 @@ pub struct Config { pub sites_directory: String, pub sockets_directory: String, #[validate(nested)] - pub scopes: HashMap + pub scopes: HashMap, } static SECRET_PATTERN: Lazy = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9]*$").unwrap()); @@ -25,15 +25,18 @@ pub struct ConfigScope { #[serde(with = "serde_regex")] pub domain_pattern: Regex, #[validate(length(equal = 100), regex(path = *SECRET_PATTERN))] - pub secret: String + pub secret: String, } pub async fn load_config() -> Result { - let config: Config = task::spawn_blocking(|| Figment::new() - .merge(Toml::file("./config.toml")) - .extract() - .wrap_err("Failed to load the configuration.")).await?; + let config: Config = task::spawn_blocking(|| { + Figment::new() + .merge(Toml::file("./config.toml")) + .extract() + .wrap_err("Failed to load the configuration.") + }) + .await?; config.validate().wrap_err("Failed to validate the configuration.")?; Ok(config) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index c5f4ee5..5f50950 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,12 @@ use crate::api::start_api_server; use crate::caddy::start_caddy; use crate::config::load_config; use crate::sites::start_sites_worker; +use async_std::fs; use camino::Utf8Path; use color_eyre::Result; use color_eyre::eyre::WrapErr; use log::LevelFilter; use std::sync::Arc; -use async_std::fs; mod api; mod caddy; @@ -27,7 +27,9 @@ async fn main() -> Result<()> { let config = load_config().await?; let sockets_directory_path = Utf8Path::new(&config.sockets_directory); - fs::create_dir_all(sockets_directory_path.as_std_path()).await.wrap_err("Failed to access or create the sockets directory.")?; + fs::create_dir_all(sockets_directory_path.as_std_path()) + .await + .wrap_err("Failed to access or create the sockets directory.")?; let sockets_directory_path = sockets_directory_path.canonicalize_utf8().unwrap(); let api_socket_path = sockets_directory_path.join("api.sock"); diff --git a/src/sites.rs b/src/sites.rs index 17e5a23..448d43b 100644 --- a/src/sites.rs +++ b/src/sites.rs @@ -1,6 +1,5 @@ use crate::caddy::CaddyController; -use async_std::io::{BufReader, BufWriter, WriteExt}; -use async_std::net::TcpStream; +use async_std::io::WriteExt; use async_std::stream::StreamExt; use async_std::sync::RwLock; use async_std::task::JoinHandle; @@ -157,7 +156,7 @@ async fn handle_download(ureq_agent: Arc, sites: &Sites, caddy_cont move || ureq_agent.get(url).call() }) .await?; - + let status = response.status(); if !status.is_success() { return Err(eyre!("Download request failed with status code {status}")); @@ -221,11 +220,7 @@ async fn handle_download(ureq_agent: Arc, sites: &Sites, caddy_cont Ok(()) } -async fn sites_worker( - mut download_tasks_receiver: async_std::channel::Receiver, - sites: Arc, - mut caddy_controller: CaddyController, -) -> Result { +async fn sites_worker(download_tasks_receiver: async_std::channel::Receiver, sites: Arc, mut caddy_controller: CaddyController) -> Result { let ureq_agent = Arc::new(ureq::Agent::new_with_config( ureq::Agent::config_builder() .timeout_resolve(Some(Duration::from_secs(10)))