From 2d07f7f0ca16ce4ab180773967f5a879d68d86b8 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 19 May 2022 15:45:44 +0530 Subject: [PATCH] feat: mod navbars and base templates, setup loading templates and fix ctx loading --- Makefile | 3 ++- src/main.rs | 6 +++--- src/pages/mod.rs | 18 +++++++++++++----- src/pages/routes.rs | 11 +++++++---- src/tests.rs | 6 ++---- templates/components/base.html | 3 +-- templates/components/nav/auth.html | 28 ---------------------------- templates/components/nav/pub.html | 10 ++-------- 8 files changed, 30 insertions(+), 55 deletions(-) delete mode 100644 templates/components/nav/auth.html diff --git a/Makefile b/Makefile index a3b552d..55b0b63 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,8 @@ define test_workspaces $(call test_databases) $(call test_forges) $(call test_federation) - cargo test --no-fail-fast + DATABASE_URL=${SQLITE_DATABASE_URL}\ + cargo test --no-fail-fast endef default: ## Debug build diff --git a/src/main.rs b/src/main.rs index f78c15e..950cb1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,9 +64,9 @@ async fn main() { pretty_env_logger::init(); lazy_static::initialize(&pages::TEMPLATES); - let ctx = Ctx::new(settings.clone()).await; - let db = sqlite::get_data(Some(settings.clone())).await; - let federate = get_federate(Some(settings.clone())).await; + let ctx = WebCtx::new( Ctx::new(settings.clone()).await); + let db = WebDB::new(sqlite::get_data(Some(settings.clone())).await); + let federate = WebFederate::new(get_federate(Some(settings.clone())).await); let socket_addr = settings.server.get_ip(); diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 4f2cf6c..f550516 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -23,12 +23,17 @@ use tera::*; use crate::settings::Settings; use crate::static_assets::ASSETS; -use crate::PAGES; use crate::{GIT_COMMIT_HASH, VERSION}; +pub mod auth; mod errors; pub mod routes; +pub use errors::ERROR_KEY; +pub use routes::PAGES; + +pub const TITLE_KEY: &str = "title"; + pub struct TemplateFile { pub name: &'static str, pub path: &'static str, @@ -56,15 +61,15 @@ pub const PAYLOAD_KEY: &str = "payload"; pub const BASE: TemplateFile = TemplateFile::new("base", "components/base.html"); pub const FOOTER: TemplateFile = TemplateFile::new("footer", "components/footer.html"); pub const PUB_NAV: TemplateFile = TemplateFile::new("pub_nav", "components/nav/pub.html"); -pub const AUTH_NAV: TemplateFile = TemplateFile::new("auth_nav", "components/nav/auth.html"); lazy_static! { pub static ref TEMPLATES: Tera = { let mut tera = Tera::default(); - for t in [BASE, FOOTER, PUB_NAV, AUTH_NAV].iter() { + for t in [BASE, FOOTER, PUB_NAV ].iter() { t.register(&mut tera).unwrap(); } errors::register_templates(&mut tera); + auth::register_templates(&mut tera); tera.autoescape_on(vec![".html", ".sql"]); //auth::register_templates(&mut tera); //gists::register_templates(&mut tera); @@ -115,7 +120,9 @@ impl<'a> Footer<'a> { } } -pub fn services(cfg: &mut web::ServiceConfig) {} +pub fn services(cfg: &mut web::ServiceConfig) { + auth::services(cfg); +} #[cfg(test)] mod tests { @@ -129,7 +136,8 @@ mod tests { let mut tera2 = Tera::default(); for t in [ BASE, FOOTER, PUB_NAV, - AUTH_NAV, + auth::AUTH_CHALLENGE, + auth::AUTH_ADD, // auth::AUTH_BASE, // auth::login::LOGIN, // auth::register::REGISTER, diff --git a/src/pages/routes.rs b/src/pages/routes.rs index 5871aa6..2f4607c 100644 --- a/src/pages/routes.rs +++ b/src/pages/routes.rs @@ -25,13 +25,16 @@ pub const PAGES: Pages = Pages::new(); pub struct Pages { /// home page pub home: &'static str, + /// auth routes + pub auth: Auth, } impl Pages { /// create new instance of Routes const fn new() -> Pages { let home = "/"; - Pages { home } + let auth = Auth::new(); + Pages { home, auth } } } @@ -41,7 +44,7 @@ pub struct Auth { /// logout route pub logout: &'static str, /// login route - pub login: &'static str, + pub add: &'static str, /// verify route pub verify: &'static str, @@ -50,11 +53,11 @@ pub struct Auth { impl Auth { /// create new instance of Authentication route pub const fn new() -> Auth { - let login = "/login"; + let add = "/add"; let logout = "/logout"; let verify = "/verify"; Auth { - login, + add, logout, verify, } diff --git a/src/tests.rs b/src/tests.rs index d73b631..e2a3337 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -24,8 +24,9 @@ pub use crate::db::BoxDB; pub use crate::federate::{get_federate, ArcFederate}; use crate::settings::{DBType, Settings}; - //use actix_web::cookie::Cookie; +use crate::errors::*; +use crate::*; use actix_web::test; use actix_web::{ body::{BoxBody, EitherBody}, @@ -34,8 +35,6 @@ use actix_web::{ http::StatusCode, }; use serde::Serialize; -use crate::errors::*; -use crate::*; //pub mod sqlx_postgres { // use super::*; @@ -71,7 +70,6 @@ pub mod sqlx_sqlite { } } - #[macro_export] macro_rules! get_cookie { ($resp:expr) => { diff --git a/templates/components/base.html b/templates/components/base.html index 59e9598..5e6d225 100644 --- a/templates/components/base.html +++ b/templates/components/base.html @@ -4,8 +4,7 @@ - Gists - {% block title %} {% endblock %} + {% block title %} {% endblock %} | Starchart
{% block nav %} {% endblock %}
diff --git a/templates/components/nav/auth.html b/templates/components/nav/auth.html deleted file mode 100644 index 4137d4c..0000000 --- a/templates/components/nav/auth.html +++ /dev/null @@ -1,28 +0,0 @@ - diff --git a/templates/components/nav/pub.html b/templates/components/nav/pub.html index 18ae84b..97dc15c 100644 --- a/templates/components/nav/pub.html +++ b/templates/components/nav/pub.html @@ -3,7 +3,7 @@