diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index d6cc9b05..ddf39c28 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -103,6 +103,7 @@ jobs: GIT_HASH: 8e77345f1597e40c2e266cb4e6dee74888918a61 OPEN_API_DOCS: 8e77345f1597e40c2e266cb4e6dee74888918a61 CACHE_BUSTER_FILE_MAP: '{"map":{"./static/bundle/main.js":"./prod/bundle/main.1417115E59909BE0A01040A45A398ADB09D928DF89CCF038FA44B14850442096.js"},"base_dir":"./prod"}' + COMPILED_DATE: "2021-07-21" - name: Upload to Codecov if: matrix.version == '1.51.0' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request') diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 70a3ad3d..aa713c26 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -93,6 +93,7 @@ jobs: DATABASE_URL: postgres://postgres:password@localhost:5432/postgres GIT_HASH: 8e77345f1597e40c2e266cb4e6dee74888918a61 # dummy value OPEN_API_DOCS: 8e77345f1597e40c2e266cb4e6dee74888918a61 + COMPILED_DATE: "2021-07-21" - name: Deploy to GitHub Pages if: matrix.version == 'stable' && (github.repository == 'mCaptcha/mCaptcha') diff --git a/Cargo.lock b/Cargo.lock index 4805bce1..05237e1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -783,9 +783,9 @@ dependencies = [ [[package]] name = "crypto-mac" -version = "0.10.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4857fd85a0c34b3c3297875b747c1e02e06b6a0ea32dd892d8192b9ce0813ea6" +checksum = "bff07008ec701e8028e2ceb8f83f0e4274ee62bd2dbdc4fefff2e9a91824081a" dependencies = [ "generic-array", "subtle", @@ -1984,18 +1984,18 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7509cc106041c40a4518d2af7a61530e1eed0e6285296a3d8c5472806ccc4a4" +checksum = "576bc800220cc65dac09e99e97b08b358cfab6e17078de8dc5fee223bd2d0c08" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c950132583b500556b1efd71d45b319029f2b71518d979fcc208e16b42426f" +checksum = "6e8fe8163d14ce7f0cdac2e040116f22eac817edabff0be91e8aff7e9accf389" dependencies = [ "proc-macro2", "quote", @@ -3042,9 +3042,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b5220f05bb7de7f3f53c7c065e1199b3172696fe2db9f9c4d8ad9b4ee74c342" +checksum = "4ac2e1d4bd0f75279cfd5a076e0d578bbf02c22b7c39e766c437dd49b3ec43e0" dependencies = [ "tinyvec_macros", ] @@ -3208,9 +3208,9 @@ checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" [[package]] name = "universal-hash" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8326b2c654932e3e4f9196e69d08fdf7cfd718e1dc6f66b347e6024a0c961402" +checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" dependencies = [ "generic-array", "subtle", diff --git a/Cargo.toml b/Cargo.toml index 96e5e2a8..b9216f53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,6 +94,7 @@ serde_json = "1" yaml-rust = "0.4.5" cache-buster = { version = "0.2.0", git = "https://github.com/realaravinth/cache-buster" } mime = "0.3.16" +sqlx = { version = "0.5.5", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] } [dev-dependencies] pow_sha256 = { version = "0.2.1", git = "https://github.com/mcaptcha/pow_sha256" } diff --git a/build.rs b/build.rs index f546c8b6..fafd88a4 100644 --- a/build.rs +++ b/build.rs @@ -1,22 +1,23 @@ /* -* Copyright (C) 2021 Aravinth Manivannan -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Affero General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with this program. If not, see . -*/ + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use std::process::Command; use cache_buster::{BusterBuilder, NoHashCategory}; -use std::process::Command; +use sqlx::types::time::OffsetDateTime; fn main() { // note: add error checking yourself. @@ -33,6 +34,10 @@ fn main() { "cargo:rustc-env=OPEN_API_DOCS={}", serde_json::to_string(&api_json).unwrap() ); + + let now = OffsetDateTime::now_utc().format("%y-%m-%d"); + println!("cargo:rustc-env=COMPILED_DATE={}", &now); + cache_bust(); } diff --git a/src/main.rs b/src/main.rs index 17b273da..5a273201 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,7 @@ lazy_static! { } pub const OPEN_API_DOC: &str = env!("OPEN_API_DOCS"); +pub const COMPILED_DATE: &str = env!("COMPILED_DATE"); pub const GIT_COMMIT_HASH: &str = env!("GIT_HASH"); pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const PKG_NAME: &str = env!("CARGO_PKG_NAME"); diff --git a/src/pages/auth/mod.rs b/src/pages/auth/mod.rs index e276d3ec..84af1c32 100644 --- a/src/pages/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -36,5 +36,10 @@ pub mod routes { join: "/join", } } + + pub const fn get_sitemap() -> [&'static str; 2] { + const AUTH: Auth = Auth::new(); + [AUTH.login, AUTH.join] + } } } diff --git a/src/pages/mod.rs b/src/pages/mod.rs index c7845a76..e460bbdf 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -1,18 +1,18 @@ /* -* Copyright (C) 2021 Aravinth Manivannan -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Affero General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License along with this program. If not, see . -*/ + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + */ use actix_web::web::ServiceConfig; @@ -20,6 +20,7 @@ mod auth; pub mod errors; mod panel; pub mod routes; +mod sitemap; pub const NAME: &str = "mCaptcha"; @@ -27,6 +28,7 @@ pub fn services(cfg: &mut ServiceConfig) { auth::services(cfg); panel::services(cfg); errors::services(cfg); + cfg.service(sitemap::sitemap); } #[cfg(not(tarpaulin_include))] @@ -94,7 +96,7 @@ mod tests { #[actix_rt::test] async fn public_pages_tempaltes_work() { let app = test::init_service(App::new().configure(services)).await; - let urls = vec![PAGES.auth.login, PAGES.auth.join]; + let urls = vec![PAGES.auth.login, PAGES.auth.join, PAGES.sitemap]; for url in urls.iter() { let resp = diff --git a/src/pages/panel/mod.rs b/src/pages/panel/mod.rs index 7ed5c275..a1679de8 100644 --- a/src/pages/panel/mod.rs +++ b/src/pages/panel/mod.rs @@ -77,5 +77,18 @@ pub mod routes { settings: Settings::new(), } } + + pub const fn get_sitemap() -> [&'static str; 5] { + const PANEL: Panel = Panel::new(); + const S: [&str; 2] = Sitekey::get_sitemap(); + + [ + PANEL.home, + PANEL.notifications, + S[0], + S[1], + Settings::get_sitemap()[0], + ] + } } } diff --git a/src/pages/panel/settings.rs b/src/pages/panel/settings.rs index 36ba5d1b..2a4778d0 100644 --- a/src/pages/panel/settings.rs +++ b/src/pages/panel/settings.rs @@ -37,6 +37,12 @@ pub mod routes { update_secret: "/settings/secret/update", } } + + pub const fn get_sitemap() -> [&'static str; 1] { + const S: Settings = Settings::new(); + + [S.home] + } } } diff --git a/src/pages/panel/sitekey/mod.rs b/src/pages/panel/sitekey/mod.rs index f8714c5d..9043251f 100644 --- a/src/pages/panel/sitekey/mod.rs +++ b/src/pages/panel/sitekey/mod.rs @@ -40,6 +40,10 @@ pub mod routes { delete: "/sitekey/{key}/delete", } } + pub const fn get_sitemap() -> [&'static str; 2] { + const S: Sitekey = Sitekey::new(); + [S.list, S.add] + } } } diff --git a/src/pages/routes.rs b/src/pages/routes.rs index a7f4e24d..4fc6f22a 100644 --- a/src/pages/routes.rs +++ b/src/pages/routes.rs @@ -26,6 +26,7 @@ pub struct Routes { pub panel: Panel, pub errors: Errors, pub about: &'static str, + pub sitemap: &'static str, pub thanks: &'static str, pub donate: &'static str, pub security: &'static str, @@ -42,10 +43,27 @@ impl Routes { home, errors: Errors::new(), about: "/about", + sitemap: "/sitemap.xml", thanks: "/thanks", donate: "/donate", security: "/security", privacy: "/privacy-policy", } } + + pub const fn get_sitemap() -> [&'static str; 7] { + let a = Auth::get_sitemap(); + let p = Panel::get_sitemap(); + [a[0], a[1], p[0], p[1], p[2], p[3], p[4]] + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sitemap_works() { + Routes::get_sitemap(); + } } diff --git a/src/pages/sitemap.rs b/src/pages/sitemap.rs new file mode 100644 index 00000000..298ba413 --- /dev/null +++ b/src/pages/sitemap.rs @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + */ + +use actix_web::{HttpResponse, Responder}; +use lazy_static::lazy_static; +use my_codegen::get; +use sailfish::TemplateOnce; + +use super::routes::Routes; +use crate::PAGES; + +#[derive(Clone, TemplateOnce)] +#[template(path = "sitemap.html")] +struct IndexPage { + urls: [&'static str; 7], +} + +impl Default for IndexPage { + fn default() -> Self { + let urls = Routes::get_sitemap(); + Self { urls } + } +} + +lazy_static! { + static ref INDEX: String = IndexPage::default().render_once().unwrap(); +} + +#[get(path = "PAGES.sitemap")] +pub async fn sitemap() -> impl Responder { + HttpResponse::Ok() + .content_type("application/xml; charset=utf-8") + .body(&*INDEX) +} diff --git a/templates/sitemap.html b/templates/sitemap.html new file mode 100644 index 00000000..fc57e6ae --- /dev/null +++ b/templates/sitemap.html @@ -0,0 +1,11 @@ + + +<. for url in urls.iter(){ .> + + <.= url .> + <.= crate::COMPILED_DATE .> + weekly + 0.8 + + <. } .> +