pages use const routes

This commit is contained in:
realaravinth 2021-05-02 18:11:56 +05:30
parent 0829ee1c74
commit 9d6b27a95b
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
15 changed files with 119 additions and 81 deletions

View file

@ -125,7 +125,7 @@ async fn auth_works() {
.await; .await;
assert_eq!(signout_resp.status(), StatusCode::OK); assert_eq!(signout_resp.status(), StatusCode::OK);
let headers = signout_resp.headers(); 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] #[actix_rt::test]

View file

@ -28,9 +28,9 @@ mod api;
mod data; mod data;
mod docs; mod docs;
mod errors; mod errors;
mod pages;
mod settings; mod settings;
mod static_assets; mod static_assets;
mod templates;
#[macro_use] #[macro_use]
mod routes; mod routes;
#[cfg(test)] #[cfg(test)]
@ -40,6 +40,7 @@ mod middleware;
pub use api::v1::ROUTES as V1_API_ROUTES; pub use api::v1::ROUTES as V1_API_ROUTES;
pub use data::Data; pub use data::Data;
pub use pages::routes::ROUTES as PAGES;
pub use settings::Settings; pub use settings::Settings;
use static_assets::FileMap; use static_assets::FileMap;
@ -91,7 +92,7 @@ async fn main() -> std::io::Result<()> {
.configure(v1::services) .configure(v1::services)
.configure(docs::services) .configure(docs::services)
.configure(static_assets::services) .configure(static_assets::services)
.configure(templates::services) .configure(pages::services)
.app_data(get_json_err()) .app_data(get_json_err())
}) })
.bind(SETTINGS.server.get_ip()) .bind(SETTINGS.server.get_ip())

View file

@ -19,16 +19,15 @@
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use actix_identity::Identity; use actix_identity::Identity;
//use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::{http, Error, FromRequest, HttpResponse}; use actix_web::{http, Error, FromRequest, HttpResponse};
use futures::future::{ok, Either, Ready}; use futures::future::{ok, Either, Ready};
pub struct CheckLogin; use crate::PAGES;
const LOGIN_ROUTE: &str = "/login"; pub struct CheckLogin;
impl<S, B> Transform<S> for CheckLogin impl<S, B> Transform<S> for CheckLogin
where where
@ -46,25 +45,6 @@ where
ok(CheckLoginMiddleware { service }) 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<S> { pub struct CheckLoginMiddleware<S> {
service: S, service: S,
} }
@ -84,9 +64,6 @@ where
} }
fn call(&mut self, req: ServiceRequest) -> Self::Future { 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(); let (r, mut pl) = req.into_parts();
// TODO investigate when the bellow statement will // TODO investigate when the bellow statement will
@ -97,30 +74,14 @@ where
{ {
let req = ServiceRequest::from_parts(r, pl).ok().unwrap(); let req = ServiceRequest::from_parts(r, pl).ok().unwrap();
Either::Left(self.service.call(req)) Either::Left(self.service.call(req))
// Some(())
} else { } else {
let req = ServiceRequest::from_parts(r, pl).ok().unwrap(); let req = ServiceRequest::from_parts(r, pl).ok().unwrap();
Either::Right(ok(req.into_response( Either::Right(ok(req.into_response(
HttpResponse::Found() HttpResponse::Found()
.header(http::header::LOCATION, LOGIN_ROUTE) .header(http::header::LOCATION, PAGES.auth.login)
.finish() .finish()
.into_body(), .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(),
// )))
// }
} }
} }

View file

@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use actix_web::{get, HttpResponse, Responder}; use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce; use sailfish::TemplateOnce;
#[derive(Clone, TemplateOnce)] #[derive(Clone, TemplateOnce)]
@ -34,7 +34,6 @@ impl<'a> Default for IndexPage<'a> {
} }
} }
#[get("/login")]
pub async fn login() -> impl Responder { pub async fn login() -> impl Responder {
let body = IndexPage::default().render_once().unwrap(); let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok() HttpResponse::Ok()

View file

@ -17,3 +17,26 @@
pub mod login; pub mod login;
pub mod register; 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",
}
}
}
}

View file

@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use actix_web::{get, HttpResponse, Responder}; use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce; use sailfish::TemplateOnce;
#[derive(TemplateOnce, Clone)] #[derive(TemplateOnce, Clone)]
@ -34,7 +34,6 @@ impl<'a> Default for IndexPage<'a> {
} }
} }
#[get("/join")]
pub async fn join() -> impl Responder { pub async fn join() -> impl Responder {
let body = IndexPage::default().render_once().unwrap(); let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok() HttpResponse::Ok()

View file

@ -19,12 +19,11 @@ use actix_web::web::ServiceConfig;
mod auth; mod auth;
mod panel; mod panel;
pub mod routes;
pub fn services(cfg: &mut ServiceConfig) { pub fn services(cfg: &mut ServiceConfig) {
cfg.service(panel::panel); auth::services(cfg);
cfg.service(panel::sitekey::add_sitekey); panel::services(cfg);
cfg.service(auth::login::login);
cfg.service(auth::register::join);
} }
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
@ -60,7 +59,7 @@ mod tests {
) )
.await; .await;
let urls = vec!["/", "/sitekey/add"]; let urls = vec![PAGES.home, PAGES.panel.sitekey.add];
for url in urls.iter() { for url in urls.iter() {
let resp = let resp =
@ -85,7 +84,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn public_pages_tempaltes_work() { async fn public_pages_tempaltes_work() {
let mut app = test::init_service(App::new().configure(services)).await; 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() { for url in urls.iter() {
let resp = let resp =

View file

@ -15,11 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use actix_web::{get, HttpResponse, Responder}; use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce; use sailfish::TemplateOnce;
use crate::CheckLogin;
pub mod sitekey; pub mod sitekey;
#[derive(TemplateOnce, Clone)] #[derive(TemplateOnce, Clone)]
@ -40,10 +38,34 @@ impl<'a> Default for IndexPage<'a> {
} }
} }
#[get("/", wrap = "CheckLogin")] pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
pub async fn panel() -> impl Responder { 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(); let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok() HttpResponse::Ok()
.content_type("text/html; charset=utf-8") .content_type("text/html; charset=utf-8")
.body(body) .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(),
}
}
}
}

View file

@ -15,11 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use actix_web::{get, HttpResponse, Responder}; use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce; use sailfish::TemplateOnce;
use crate::CheckLogin;
#[derive(TemplateOnce, Clone)] #[derive(TemplateOnce, Clone)]
#[template(path = "panel/add-site-key/index.html")] #[template(path = "panel/add-site-key/index.html")]
pub struct IndexPage<'a> { 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 { pub async fn add_sitekey() -> impl Responder {
let body = IndexPage::default().render_once().unwrap(); let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok() HttpResponse::Ok()

View file

@ -16,4 +16,31 @@
*/ */
mod add; 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
);
}

View file

@ -15,14 +15,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use sailfish::TemplateOnce; use super::auth::routes::Auth;
//use au use super::panel::routes::Panel;
pub const ROUTES: Routes = Routes::new();
//#[get("/")] pub struct Routes {
//pub async fn login() -> impl Responder { pub home: &'static str,
// let body = SignIn::default().render_once().unwrap(); pub auth: Auth,
// // .map_err(|_| ServiceError::InternalError)?; pub panel: Panel,
// HttpResponse::Ok() }
// .content_type("text/html; charset=utf-8")
// .body(body) impl Routes {
//} const fn new() -> Routes {
let panel = Panel::new();
let home = panel.home;
Routes {
auth: Auth::new(),
panel,
home,
}
}
}

View file

@ -37,7 +37,7 @@
<div class="form__secondary-action"> <div class="form__secondary-action">
<p class="form__secondary-action__banner"> <p class="form__secondary-action__banner">
New to mCaptcha? New to mCaptcha?
<a href="/join" class="form__secondary-action__link" <a href="<.= crate::PAGES.auth.join .>" class="form__secondary-action__link"
>Create account</a >Create account</a
> >
</p> </p>

View file

@ -57,7 +57,7 @@
<div class="form__secondary-action"> <div class="form__secondary-action">
<p class="form__secondary-action__banner"> <p class="form__secondary-action__banner">
Already have an account? Already have an account?
<a href="/login" class="form__secondary-action__link">Log in</a> <a href="<.= crate::PAGES.auth.login .>" class="form__secondary-action__link">Log in</a>
</p> </p>
</div> </div>
</div> </div>

View file

@ -11,7 +11,7 @@
<li class="secondary-menu__section-partition"></li> <li class="secondary-menu__section-partition"></li>
--> -->
<li class="secondary-menu__item"> <li class="secondary-menu__item">
<a class="secondary-menu__item-link" href="/panel"> <a class="secondary-menu__item-link" href="<.= crate::PAGES.home .>">
<img class="secondary-menu__icon" src="<.= crate::FILES.get("./static-assets/img/svg/home.svg").unwrap() .>" alt="" /> <img class="secondary-menu__icon" src="<.= crate::FILES.get("./static-assets/img/svg/home.svg").unwrap() .>" alt="" />
<div class="secondary-menu__item-name"> <div class="secondary-menu__item-name">
Overview Overview
@ -19,7 +19,7 @@
</a> </a>
</li> </li>
<li class="secondary-menu__item"> <li class="secondary-menu__item">
<a class="secondary-menu__item-link" href="/panel/sitekey"> <a class="secondary-menu__item-link" href="<.= crate::PAGES.panel.sitekey.list .>">
<img class="secondary-menu__icon" src="<.= crate::FILES.get("./static-assets/img/svg/key.svg").unwrap() .>" alt="" /> <img class="secondary-menu__icon" src="<.= crate::FILES.get("./static-assets/img/svg/key.svg").unwrap() .>" alt="" />
<div class="secondary-menu__item-name"> <div class="secondary-menu__item-name">
Site Keys Site Keys

View file

@ -4,7 +4,7 @@
--> -->
<li class="task-bar__spacer"></li> <li class="task-bar__spacer"></li>
<li class="task-bar__action"> <li class="task-bar__action">
<a class="task-bar__link" href="/sitekey/add"> <a class="task-bar__link" href="<.= crate::PAGES.panel.sitekey.add .>">
<button class="main-menu__add-site"> <button class="main-menu__add-site">
+ New Site + New Site
</button> </button>
@ -22,7 +22,7 @@
</li> </li>
<li class="task-bar__action"> <li class="task-bar__action">
<a href="/logout"> <a href="<.= crate::V1_API_ROUTES.auth.logout .>">
<img class="task-bar__icon" src="<.= <img class="task-bar__icon" src="<.=
crate::FILES.get("./static-assets/img/svg/log-out.svg").unwrap() .>" alt="Profile" crate::FILES.get("./static-assets/img/svg/log-out.svg").unwrap() .>" alt="Profile"
/></a> /></a>