use std::{ fs::{self, File}, io::{self, Write}, path::Path, }; use actix_files::Files; use actix_web::{web, App, HttpServer}; use glob::glob; use minify_html::{minify, Cfg}; mod config; mod template; #[path = "routes/agreements.rs"] mod agreements; #[path = "routes/not_found.rs"] mod not_found; #[path = "routes/index.rs"] mod index; #[path = "routes/networks.rs"] mod networks; #[path = "routes/portfolio.rs"] mod portfolio; #[actix_web::main] async fn main() -> io::Result<()> { let config = config::get_config("/config/config.toml"); let addr = ("127.0.0.1", config.port); println!("Listening to {}://{}:{}", config.scheme, addr.0, addr.1); let static_folder = "static"; let dist_folder = format!("dist/{static_folder}"); // The static folder is minimized only in release mode let folder = if cfg!(debug_assertions) { format!("{static_folder}/") } else { let cfg = Cfg::spec_compliant(); for entry in glob(&format!("{static_folder}/**/*.*")).unwrap() { let path = entry.unwrap(); let path_with_dist = path.to_string_lossy().replace(static_folder, &dist_folder); // Create folders let new_path = Path::new(&path_with_dist); fs::create_dir_all(new_path.parent().unwrap()).unwrap(); let mut copy = true; if let Some(ext) = path.extension() { // List of files who should be minified if ["html", "css", "js", "svg", "webmanifest", "xml"] .iter() .any(|item| ext.to_string_lossy().to_lowercase().contains(item)) { // We won't copy, we'll minify copy = false; // Minify let data = fs::read(&path).unwrap(); let minified = minify(&data, &cfg); // Write files let mut file = File::create(&path_with_dist)?; file.write_all(&minified)?; } } if copy { // If no minification is needed fs::copy(path, path_with_dist)?; } } format!("{dist_folder}/") }; HttpServer::new(move || { App::new() .app_data(web::Data::new(config.clone())) .service(index::page) .service(agreements::security) .service(agreements::humans) .service(agreements::robots) .service(agreements::sitemap) .service(networks::page) .service(portfolio::page) .service(Files::new("/", &folder)) .default_service(web::to(not_found::page)) }) .bind(addr)? .run() .await }