From fe02c43c2c1017760b746b466371dd7e06bb1dd5 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 4 May 2021 15:45:53 +0530 Subject: [PATCH] static pages are rendered and cached --- src/pages/auth/login.rs | 24 +++++++++++------------- src/pages/auth/register.rs | 26 ++++++++++++-------------- src/pages/mod.rs | 2 +- src/pages/panel/mod.rs | 28 ++++++++++++---------------- src/pages/panel/sitekey/add.rs | 18 ++++++++---------- src/pages/panel/sitekey/list.rs | 14 ++++---------- templates/components/headers.html | 2 +- 7 files changed, 49 insertions(+), 65 deletions(-) diff --git a/src/pages/auth/login.rs b/src/pages/auth/login.rs index 3ce4a97a..0e1d87b6 100644 --- a/src/pages/auth/login.rs +++ b/src/pages/auth/login.rs @@ -16,29 +16,27 @@ */ use actix_web::{HttpResponse, Responder}; +use lazy_static::lazy_static; use sailfish::TemplateOnce; -use crate::pages::TITLE; - #[derive(Clone, TemplateOnce)] #[template(path = "auth/login/index.html")] -struct IndexPage<'a> { - name: &'a str, - title: &'a str, -} +struct IndexPage; -impl<'a> Default for IndexPage<'a> { +const PAGE: &str = "Login"; + +impl Default for IndexPage { fn default() -> Self { - IndexPage { - name: TITLE, - title: "Login", - } + IndexPage } } +lazy_static! { + static ref INDEX: String = IndexPage::default().render_once().unwrap(); +} + pub async fn login() -> impl Responder { - let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(body) + .body(&*INDEX) } diff --git a/src/pages/auth/register.rs b/src/pages/auth/register.rs index 74722abe..22c03532 100644 --- a/src/pages/auth/register.rs +++ b/src/pages/auth/register.rs @@ -16,29 +16,27 @@ */ use actix_web::{HttpResponse, Responder}; +use lazy_static::lazy_static; use sailfish::TemplateOnce; -use crate::pages::TITLE; - -#[derive(TemplateOnce, Clone)] +#[derive(Clone, TemplateOnce)] #[template(path = "auth/register/index.html")] -struct IndexPage<'a> { - name: &'a str, - title: &'a str, -} +struct IndexPage; -impl<'a> Default for IndexPage<'a> { +const PAGE: &str = "Join"; + +impl Default for IndexPage { fn default() -> Self { - IndexPage { - name: TITLE, - title: "Join", - } + IndexPage } } +lazy_static! { + static ref INDEX: String = IndexPage::default().render_once().unwrap(); +} + pub async fn join() -> impl Responder { - let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(body) + .body(&*INDEX) } diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 3396ce40..1ace6cb6 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -21,7 +21,7 @@ mod auth; mod panel; pub mod routes; -pub const TITLE: &str = "mCaptcha"; +pub const NAME: &str = "mCaptcha"; pub fn services(cfg: &mut ServiceConfig) { auth::services(cfg); diff --git a/src/pages/panel/mod.rs b/src/pages/panel/mod.rs index dffde348..66eb42c3 100644 --- a/src/pages/panel/mod.rs +++ b/src/pages/panel/mod.rs @@ -16,28 +16,25 @@ */ use actix_web::{HttpResponse, Responder}; +use lazy_static::lazy_static; use sailfish::TemplateOnce; -use crate::pages::TITLE; - pub mod sitekey; #[derive(TemplateOnce, Clone)] #[template(path = "panel/index.html")] -pub struct IndexPage<'a> { - pub name: &'a str, - pub title: &'a str, +pub struct IndexPage; + +const PAGE: &str = "Dashboard"; + +impl Default for IndexPage { + fn default() -> Self { + IndexPage + } } -const COMPONENT: &str = "Dashboard"; - -impl<'a> Default for IndexPage<'a> { - fn default() -> Self { - IndexPage { - name: TITLE, - title: COMPONENT, - } - } +lazy_static! { + static ref INDEX: String = IndexPage::default().render_once().unwrap(); } pub fn services(cfg: &mut actix_web::web::ServiceConfig) { @@ -49,10 +46,9 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) { } async fn panel() -> impl Responder { - let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(body) + .body(&*INDEX) } pub mod routes { diff --git a/src/pages/panel/sitekey/add.rs b/src/pages/panel/sitekey/add.rs index d7b93fe4..57665840 100644 --- a/src/pages/panel/sitekey/add.rs +++ b/src/pages/panel/sitekey/add.rs @@ -16,39 +16,37 @@ */ use actix_web::{HttpResponse, Responder}; +use lazy_static::lazy_static; use sailfish::TemplateOnce; -use crate::pages::TITLE; +const PAGE: &str = "Add Sitekey"; + +lazy_static! { + static ref INDEX: String = IndexPage::default().render_once().unwrap(); +} #[derive(TemplateOnce, Clone)] #[template(path = "panel/add-site-key/index.html")] pub struct IndexPage<'a> { - pub name: &'a str, - pub title: &'a str, pub levels: usize, pub form_title: &'a str, pub form_description: &'a str, pub form_duration: usize, } -const COMPONENT: &str = "Add Site Key"; - impl<'a> Default for IndexPage<'a> { fn default() -> Self { IndexPage { - name: TITLE, - title: COMPONENT, levels: 1, form_description: "", - form_title: "Add Site Key", + form_title: PAGE, form_duration: 30, } } } pub async fn add_sitekey() -> impl Responder { - let body = IndexPage::default().render_once().unwrap(); HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(body) + .body(&*INDEX) } diff --git a/src/pages/panel/sitekey/list.rs b/src/pages/panel/sitekey/list.rs index d03a806d..dfa710fd 100644 --- a/src/pages/panel/sitekey/list.rs +++ b/src/pages/panel/sitekey/list.rs @@ -20,19 +20,13 @@ use sailfish::TemplateOnce; #[derive(TemplateOnce, Clone)] #[template(path = "panel/site-keys/index.html")] -pub struct IndexPage<'a> { - pub name: &'a str, - pub title: &'a str, -} +pub struct IndexPage; -const TITLE: &str = "Add Site Key"; +const PAGE: &str = "SiteKeys"; -impl<'a> Default for IndexPage<'a> { +impl Default for IndexPage { fn default() -> Self { - IndexPage { - name: "mCaptcha", - title: TITLE, - } + IndexPage } } diff --git a/templates/components/headers.html b/templates/components/headers.html index fba52e50..cd0d6edd 100644 --- a/templates/components/headers.html +++ b/templates/components/headers.html @@ -3,6 +3,6 @@ - <.= title .> | <.= name .> + <.= PAGE .> | <.= crate::pages::NAME .>