diff --git a/Cargo.lock b/Cargo.lock index 0286bcd..c482690 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -500,9 +500,9 @@ dependencies = [ [[package]] name = "digest" -version = "0.10.5" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adfbc57365a37acbd2ebf2b64d7e69bb766e2fea813521ed536f5d0520dcf86c" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ "block-buffer", "crypto-common", @@ -642,7 +642,7 @@ dependencies = [ [[package]] name = "gnupg" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/gnupg-rs#c6938384d522b07c27a0f468e4f0fda737f3b12d" +source = "git+https://git.hydrar.de/jmarya/gnupg-rs#f823922c40b8abe5cb4b5b5e03b18ecb4814854a" [[package]] name = "h2" @@ -921,6 +921,7 @@ dependencies = [ "reqwest", "serde", "serde_json", + "web-base", ] [[package]] @@ -1208,9 +1209,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.12" +version = "0.11.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "431949c384f4e2ae07605ccaa56d1d9d2ecdb5cadd4f9577ccfab29f2e5149fc" +checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" dependencies = [ "base64", "bytes", @@ -1712,6 +1713,16 @@ version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +[[package]] +name = "web-base" +version = "0.1.0" +source = "git+https://git.hydrar.de/jmarya/web-base#14c3939c9964323ca9fe7c387742dc37999d30d4" +dependencies = [ + "actix-files", + "actix-web", + "reqwest", +] + [[package]] name = "web-sys" version = "0.3.60" diff --git a/Cargo.toml b/Cargo.toml index df44f5e..93dcce5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ chrono = "0.4.22" env_logger = "0.9.3" log = "0.4.17" gnupg = { git = "https://git.hydrar.de/jmarya/gnupg-rs" } -reqwest = "0.11.12" +web-base = { git = "https://git.hydrar.de/jmarya/web-base" } serde = {version = "1.0.147", features = ["derive"] } serde_json = "1.0.87" +reqwest = "0.11" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 412779e..6c1e775 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,26 +13,25 @@ async fn main() -> std::io::Result<()> { let conf = config::Config::new(); - pages::assets::cache_bootstrap().await; + web_base::cache_bootstrap().await; HttpServer::new(move || { let logger = actix_web::middleware::Logger::default(); - App::new() + let app = App::new() .wrap(logger) .app_data(web::Data::new(conf.clone())) .service(pages::index::index) // Assets - .service(pages::assets::bootstrap_js) - .service(pages::assets::bootstrap_css) - .service(pages::assets::bootstrap_icons) - .service(pages::assets::bootstrap_font1) - .service(pages::assets::bootstrap_font2) .service(pages::assets::wallpaper) .service(pages::assets::me_img) .service(pages::index::public_key) .service(pages::index::mirrors) .service(pages::index::message_page) - .service(pages::index::message_post) + .service(pages::index::message_post); + + let app = web_base::bootstrap::assign_pages(app); + + app }) .bind(("0.0.0.0", 8080))? .run() diff --git a/src/pages/assets.rs b/src/pages/assets.rs index 584ce0c..8e8a0ad 100644 --- a/src/pages/assets.rs +++ b/src/pages/assets.rs @@ -1,62 +1,5 @@ use actix_files::NamedFile; use actix_web::*; -use std::io::Write; - -// Bootstrap - -async fn download_file(url: &str, file: &str) { - let content = reqwest::get(url).await.expect("couldn't download file"); - std::fs::File::create(file) - .unwrap() - .write_all(&content.bytes().await.unwrap()) - .unwrap(); -} - -pub(crate) async fn cache_bootstrap() { - std::fs::create_dir_all("./cache/fonts").expect("couldn't create cache dir"); - download_file( - "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css", - "./cache/bootstrap.min.css", - ) - .await; - download_file( - "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css", - "./cache/bootstrap-icons.css", - ) - .await; - download_file( - "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js", - "./cache/bootstrap.bundle.min.js", - ) - .await; - download_file("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/fonts/bootstrap-icons.woff2?8d200481aa7f02a2d63a331fc782cfaf", "./cache/fonts/bootstrap-icons.woff2").await; - download_file("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/fonts/bootstrap-icons.woff?8d200481aa7f02a2d63a331fc782cfaf", "./cache/fonts/bootstrap-icons.woff").await; -} - -#[get("/bootstrap.min.css")] -pub(crate) async fn bootstrap_css() -> Result { - Ok(NamedFile::open("./cache/bootstrap.min.css")?) -} - -#[get("/bootstrap-icons.css")] -pub(crate) async fn bootstrap_icons() -> Result { - Ok(NamedFile::open("./cache/bootstrap-icons.css")?) -} - -#[get("/bootstrap.bundle.min.js")] -pub(crate) async fn bootstrap_js() -> Result { - Ok(NamedFile::open("./cache/bootstrap.bundle.min.js")?) -} - -#[get("/fonts/bootstrap-icons.woff2")] -pub(crate) async fn bootstrap_font1(_: HttpRequest) -> Result { - Ok(NamedFile::open("./cache/fonts/bootstrap-icons.woff2")?) -} - -#[get("/fonts/bootstrap-icons.woff")] -pub(crate) async fn bootstrap_font2(_: HttpRequest) -> Result { - Ok(NamedFile::open("./cache/fonts/bootstrap-icons.woff")?) -} // Assets diff --git a/src/pages/func.rs b/src/pages/func.rs deleted file mode 100644 index 9f8f44a..0000000 --- a/src/pages/func.rs +++ /dev/null @@ -1,51 +0,0 @@ -pub fn is_browser(req: &actix_web::HttpRequest) -> bool { - let ua = req - .headers() - .get("user-agent") - .unwrap() - .to_str() - .unwrap() - .to_lowercase(); - if ua.contains("chrome") || ua.contains("safari") || ua.contains("firefox") { - return true; - } - return false; -} - -pub fn get_host(r: &actix_web::HttpRequest) -> String { - let res = r.headers().get("HOST").unwrap().to_str().unwrap(); - return res.to_string(); -} - -pub fn get_host_address(r: &actix_web::HttpRequest) -> String { - let res = r.headers().get("HOST").unwrap().to_str().unwrap(); - let res: Vec<&str> = res.split(":").collect(); - let res = res.first().unwrap(); - return res.to_string(); -} - -pub fn is_onion(r: &actix_web::HttpRequest) -> bool { - return get_host_address(r).ends_with("onion"); -} - -pub fn is_i2p(r: &actix_web::HttpRequest) -> bool { - return get_host_address(r).ends_with("i2p"); -} - -pub fn dynamic_link( - inner: &str, - normal_link: &str, - onion: Option<&str>, - i2p: Option<&str>, - req: &actix_web::HttpRequest, -) -> String { - if is_onion(req) { - let href = onion.unwrap_or(normal_link); - return format!(" {inner} "); - } - if is_i2p(req) { - let href = i2p.unwrap_or(normal_link); - return format!(" {inner} "); - } - return format!(" {inner} "); -} diff --git a/src/pages/index.rs b/src/pages/index.rs index 7eeda92..6c8ed54 100644 --- a/src/pages/index.rs +++ b/src/pages/index.rs @@ -28,7 +28,7 @@ pub async fn message_post(r: HttpRequest, f: Form) -> impl Responde #[get("/message")] pub async fn message_page(r: HttpRequest) -> impl Responder { let config: &web::Data = r.app_data().unwrap(); - let host = pages::func::get_host(&r); + let host = web_base::func::get_host(&r); let resp = format!( r#"

Message

@@ -50,7 +50,7 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder { let config: &web::Data = r.app_data().unwrap(); if let Ok(mirror_file) = std::fs::File::open("/config/mirrors.txt") { let content = std::io::read_to_string(mirror_file).unwrap(); - if pages::func::is_browser(&r) { + if web_base::func::is_browser(&r) { let resp = format!( r#"
@@ -71,9 +71,9 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder { #[get("/public_key")] pub async fn public_key(r: HttpRequest) -> impl Responder { - if pages::func::is_browser(&r) { + if web_base::func::is_browser(&r) { let config: &web::Data = r.app_data().unwrap(); - let host = format!("http://{}", pages::func::get_host(&r)); + let host = format!("http://{}", web_base::func::get_host(&r)); let key = std::io::read_to_string(std::fs::File::open("/config/pub.key").unwrap()).unwrap(); let pgp = gnupg::GnuPG::new().unwrap(); diff --git a/src/pages/mod.rs b/src/pages/mod.rs index f29b77c..aca3a5a 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -1,4 +1,3 @@ pub mod assets; -pub mod func; pub mod html_fn; pub mod index;