diff --git a/src/api/v1/tests/auth.rs b/src/api/v1/tests/auth.rs index 4b9cc357..a6e668fa 100644 --- a/src/api/v1/tests/auth.rs +++ b/src/api/v1/tests/auth.rs @@ -125,7 +125,7 @@ async fn auth_works() { .await; assert_eq!(signout_resp.status(), StatusCode::OK); let headers = signout_resp.headers(); - assert_eq!(headers.get(header::LOCATION).unwrap(), "/login") + assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login); } #[actix_rt::test] diff --git a/src/main.rs b/src/main.rs index 8c49b6fa..7656c567 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,9 +28,9 @@ mod api; mod data; mod docs; mod errors; +mod pages; mod settings; mod static_assets; -mod templates; #[macro_use] mod routes; #[cfg(test)] @@ -40,6 +40,7 @@ mod middleware; pub use api::v1::ROUTES as V1_API_ROUTES; pub use data::Data; +pub use pages::routes::ROUTES as PAGES; pub use settings::Settings; use static_assets::FileMap; @@ -91,7 +92,7 @@ async fn main() -> std::io::Result<()> { .configure(v1::services) .configure(docs::services) .configure(static_assets::services) - .configure(templates::services) + .configure(pages::services) .app_data(get_json_err()) }) .bind(SETTINGS.server.get_ip()) diff --git a/src/middleware/auth.rs b/src/middleware/auth.rs index 20416cf2..e1aa22f6 100644 --- a/src/middleware/auth.rs +++ b/src/middleware/auth.rs @@ -19,16 +19,15 @@ use std::task::{Context, Poll}; use actix_identity::Identity; -//use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_service::{Service, Transform}; use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::{http, Error, FromRequest, HttpResponse}; use futures::future::{ok, Either, Ready}; -pub struct CheckLogin; +use crate::PAGES; -const LOGIN_ROUTE: &str = "/login"; +pub struct CheckLogin; impl Transform for CheckLogin where @@ -46,25 +45,6 @@ where ok(CheckLoginMiddleware { service }) } } -// -//pub fn auto_login(req: &HttpRequest, pl: &mut dev::Payload) -> Option<()> { -// dbg!("login"); -// if let Some(_) = Identity::from_request(req, pl) -// .into_inner() -// .map(|x| x.identity()) -// .unwrap() -// { -// Some(()) -// } else { -// None -// } -//} -// -//fn not_auth(path: &str) -> bool { -// let paths = ["/login", "/css", "/img", "/js"]; -// paths.iter().any(|x| path.starts_with(x)) -//} - pub struct CheckLoginMiddleware { service: S, } @@ -84,9 +64,6 @@ where } fn call(&mut self, req: ServiceRequest) -> Self::Future { - // if not_auth(req.path()) { - // return Either::Left(self.service.call(req)); - // }; let (r, mut pl) = req.into_parts(); // TODO investigate when the bellow statement will @@ -97,30 +74,14 @@ where { let req = ServiceRequest::from_parts(r, pl).ok().unwrap(); Either::Left(self.service.call(req)) - // Some(()) } else { let req = ServiceRequest::from_parts(r, pl).ok().unwrap(); Either::Right(ok(req.into_response( HttpResponse::Found() - .header(http::header::LOCATION, LOGIN_ROUTE) + .header(http::header::LOCATION, PAGES.auth.login) .finish() .into_body(), ))) - - //None } - - // let token = auto_login(&r, &mut pl); - // let req = ServiceRequest::from_parts(r, pl).ok().unwrap(); - // if token.is_some() { - // Either::Left(self.service.call(req)) - // } else { - // Either::Right(ok(req.into_response( - // HttpResponse::Found() - // .header(http::header::LOCATION, LOGIN_ROUTE) - // .finish() - // .into_body(), - // ))) - // } } } diff --git a/src/templates/auth/login.rs b/src/pages/auth/login.rs similarity index 95% rename from src/templates/auth/login.rs rename to src/pages/auth/login.rs index 1f627d0b..21a3c1fc 100644 --- a/src/templates/auth/login.rs +++ b/src/pages/auth/login.rs @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -use actix_web::{get, HttpResponse, Responder}; +use actix_web::{HttpResponse, Responder}; use sailfish::TemplateOnce; #[derive(Clone, TemplateOnce)] @@ -34,7 +34,6 @@ impl<'a> Default for IndexPage<'a> { } } -#[get("/login")] pub async fn login() -> impl Responder { let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() diff --git a/src/templates/auth/mod.rs b/src/pages/auth/mod.rs similarity index 58% rename from src/templates/auth/mod.rs rename to src/pages/auth/mod.rs index 145dfdbd..80303104 100644 --- a/src/templates/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -17,3 +17,26 @@ pub mod login; pub mod register; + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::PAGES; + + define_resource!(cfg, PAGES.auth.login, Methods::Get, login::login); + define_resource!(cfg, PAGES.auth.join, Methods::Get, register::join); +} + +pub mod routes { + pub struct Auth { + pub login: &'static str, + pub join: &'static str, + } + impl Auth { + pub const fn new() -> Auth { + Auth { + login: "/login", + join: "/join", + } + } + } +} diff --git a/src/templates/auth/register.rs b/src/pages/auth/register.rs similarity index 95% rename from src/templates/auth/register.rs rename to src/pages/auth/register.rs index c6801257..349f0258 100644 --- a/src/templates/auth/register.rs +++ b/src/pages/auth/register.rs @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -use actix_web::{get, HttpResponse, Responder}; +use actix_web::{HttpResponse, Responder}; use sailfish::TemplateOnce; #[derive(TemplateOnce, Clone)] @@ -34,7 +34,6 @@ impl<'a> Default for IndexPage<'a> { } } -#[get("/join")] pub async fn join() -> impl Responder { let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() diff --git a/src/templates/mod.rs b/src/pages/mod.rs similarity index 91% rename from src/templates/mod.rs rename to src/pages/mod.rs index 772c76bd..6e524cdf 100644 --- a/src/templates/mod.rs +++ b/src/pages/mod.rs @@ -19,12 +19,11 @@ use actix_web::web::ServiceConfig; mod auth; mod panel; +pub mod routes; pub fn services(cfg: &mut ServiceConfig) { - cfg.service(panel::panel); - cfg.service(panel::sitekey::add_sitekey); - cfg.service(auth::login::login); - cfg.service(auth::register::join); + auth::services(cfg); + panel::services(cfg); } #[cfg(not(tarpaulin_include))] @@ -60,7 +59,7 @@ mod tests { ) .await; - let urls = vec!["/", "/sitekey/add"]; + let urls = vec![PAGES.home, PAGES.panel.sitekey.add]; for url in urls.iter() { let resp = @@ -85,7 +84,7 @@ mod tests { #[actix_rt::test] async fn public_pages_tempaltes_work() { let mut app = test::init_service(App::new().configure(services)).await; - let urls = vec!["/join", "/login"]; + let urls = vec![PAGES.auth.login, PAGES.auth.join]; for url in urls.iter() { let resp = diff --git a/src/templates/panel/mod.rs b/src/pages/panel/mod.rs similarity index 67% rename from src/templates/panel/mod.rs rename to src/pages/panel/mod.rs index 1b1bd0aa..44d04455 100644 --- a/src/templates/panel/mod.rs +++ b/src/pages/panel/mod.rs @@ -15,11 +15,9 @@ * along with this program. If not, see . */ -use actix_web::{get, HttpResponse, Responder}; +use actix_web::{HttpResponse, Responder}; use sailfish::TemplateOnce; -use crate::CheckLogin; - pub mod sitekey; #[derive(TemplateOnce, Clone)] @@ -40,10 +38,34 @@ impl<'a> Default for IndexPage<'a> { } } -#[get("/", wrap = "CheckLogin")] -pub async fn panel() -> impl Responder { +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::PAGES; + + define_resource!(cfg, PAGES.panel.home, Methods::ProtectGet, panel); + sitekey::services(cfg); +} + +async fn panel() -> impl Responder { let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() .content_type("text/html; charset=utf-8") .body(body) } + +pub mod routes { + use super::sitekey::routes::Sitekey; + pub struct Panel { + pub home: &'static str, + pub sitekey: Sitekey, + } + + impl Panel { + pub const fn new() -> Self { + Panel { + home: "/", + sitekey: Sitekey::new(), + } + } + } +} diff --git a/src/templates/panel/sitekey/add.rs b/src/pages/panel/sitekey/add.rs similarity index 92% rename from src/templates/panel/sitekey/add.rs rename to src/pages/panel/sitekey/add.rs index 624249e7..1c3558fa 100644 --- a/src/templates/panel/sitekey/add.rs +++ b/src/pages/panel/sitekey/add.rs @@ -15,11 +15,9 @@ * along with this program. If not, see . */ -use actix_web::{get, HttpResponse, Responder}; +use actix_web::{HttpResponse, Responder}; use sailfish::TemplateOnce; -use crate::CheckLogin; - #[derive(TemplateOnce, Clone)] #[template(path = "panel/add-site-key/index.html")] pub struct IndexPage<'a> { @@ -44,7 +42,6 @@ impl<'a> Default for IndexPage<'a> { } } -#[get("/sitekey/add", wrap = "CheckLogin")] pub async fn add_sitekey() -> impl Responder { let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() diff --git a/src/templates/panel/sitekey/mod.rs b/src/pages/panel/sitekey/mod.rs similarity index 57% rename from src/templates/panel/sitekey/mod.rs rename to src/pages/panel/sitekey/mod.rs index cf3359c5..db1b2623 100644 --- a/src/templates/panel/sitekey/mod.rs +++ b/src/pages/panel/sitekey/mod.rs @@ -16,4 +16,31 @@ */ mod add; -pub use add::add_sitekey; + +pub mod routes { + pub struct Sitekey { + pub list: &'static str, + pub add: &'static str, + } + + impl Sitekey { + pub const fn new() -> Self { + Sitekey { + list: "/sitekey", + add: "/sitekey/add", + } + } + } +} + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::PAGES; + + define_resource!( + cfg, + PAGES.panel.sitekey.add, + Methods::ProtectGet, + add::add_sitekey + ); +} diff --git a/src/templates/routes.rs b/src/pages/routes.rs similarity index 64% rename from src/templates/routes.rs rename to src/pages/routes.rs index fbd851e7..66321d57 100644 --- a/src/templates/routes.rs +++ b/src/pages/routes.rs @@ -15,14 +15,24 @@ * along with this program. If not, see . */ -use sailfish::TemplateOnce; -//use au +use super::auth::routes::Auth; +use super::panel::routes::Panel; +pub const ROUTES: Routes = Routes::new(); -//#[get("/")] -//pub async fn login() -> impl Responder { -// let body = SignIn::default().render_once().unwrap(); -// // .map_err(|_| ServiceError::InternalError)?; -// HttpResponse::Ok() -// .content_type("text/html; charset=utf-8") -// .body(body) -//} +pub struct Routes { + pub home: &'static str, + pub auth: Auth, + pub panel: Panel, +} + +impl Routes { + const fn new() -> Routes { + let panel = Panel::new(); + let home = panel.home; + Routes { + auth: Auth::new(), + panel, + home, + } + } +} diff --git a/templates/auth/login/index.html b/templates/auth/login/index.html index b1f6e456..4b729ab9 100644 --- a/templates/auth/login/index.html +++ b/templates/auth/login/index.html @@ -37,7 +37,7 @@

New to mCaptcha? - Create account

diff --git a/templates/auth/register/index.html b/templates/auth/register/index.html index ec85e106..634c73c6 100644 --- a/templates/auth/register/index.html +++ b/templates/auth/register/index.html @@ -57,7 +57,7 @@

Already have an account? - Log in + Log in

diff --git a/templates/panel/header/sidebar/index.html b/templates/panel/header/sidebar/index.html index e2d1ac78..846e648e 100644 --- a/templates/panel/header/sidebar/index.html +++ b/templates/panel/header/sidebar/index.html @@ -11,7 +11,7 @@
  • -->
  • - + " alt="" />
  • - + " alt="" />
    Site Keys diff --git a/templates/panel/taskbar/index.html b/templates/panel/taskbar/index.html index bdb5feaa..5a1e536e 100644 --- a/templates/panel/taskbar/index.html +++ b/templates/panel/taskbar/index.html @@ -4,7 +4,7 @@ -->
  • - + @@ -22,7 +22,7 @@
  • - + " alt="Profile" />