108 lines
3 KiB
Rust
108 lines
3 KiB
Rust
use actix_files::Files;
|
|
use actix_web::{middleware::DefaultHeaders, web, App, HttpServer};
|
|
use glob::glob;
|
|
use minify_html::{minify, Cfg};
|
|
use std::{
|
|
fs::{self, File},
|
|
io::{self, Write},
|
|
path::Path,
|
|
};
|
|
|
|
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 = ("0.0.0.0", config.port.unwrap());
|
|
|
|
println!(
|
|
"Listening to {}://{}:{}",
|
|
config.clone().scheme.unwrap(),
|
|
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()))
|
|
.wrap(DefaultHeaders::new().add((
|
|
"Onion-Location",
|
|
config.onion.as_deref().unwrap_or_default(),
|
|
)))
|
|
.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
|
|
}
|