feat: mod navbars and base templates, setup loading templates and fix

ctx loading
This commit is contained in:
Aravinth Manivannan 2022-05-19 15:45:44 +05:30
parent ac470262d1
commit 2d07f7f0ca
8 changed files with 30 additions and 55 deletions

View file

@ -33,6 +33,7 @@ define test_workspaces
$(call test_databases) $(call test_databases)
$(call test_forges) $(call test_forges)
$(call test_federation) $(call test_federation)
DATABASE_URL=${SQLITE_DATABASE_URL}\
cargo test --no-fail-fast cargo test --no-fail-fast
endef endef

View file

@ -64,9 +64,9 @@ async fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
lazy_static::initialize(&pages::TEMPLATES); lazy_static::initialize(&pages::TEMPLATES);
let ctx = Ctx::new(settings.clone()).await; let ctx = WebCtx::new( Ctx::new(settings.clone()).await);
let db = sqlite::get_data(Some(settings.clone())).await; let db = WebDB::new(sqlite::get_data(Some(settings.clone())).await);
let federate = get_federate(Some(settings.clone())).await; let federate = WebFederate::new(get_federate(Some(settings.clone())).await);
let socket_addr = settings.server.get_ip(); let socket_addr = settings.server.get_ip();

View file

@ -23,12 +23,17 @@ use tera::*;
use crate::settings::Settings; use crate::settings::Settings;
use crate::static_assets::ASSETS; use crate::static_assets::ASSETS;
use crate::PAGES;
use crate::{GIT_COMMIT_HASH, VERSION}; use crate::{GIT_COMMIT_HASH, VERSION};
pub mod auth;
mod errors; mod errors;
pub mod routes; pub mod routes;
pub use errors::ERROR_KEY;
pub use routes::PAGES;
pub const TITLE_KEY: &str = "title";
pub struct TemplateFile { pub struct TemplateFile {
pub name: &'static str, pub name: &'static str,
pub path: &'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 BASE: TemplateFile = TemplateFile::new("base", "components/base.html");
pub const FOOTER: TemplateFile = TemplateFile::new("footer", "components/footer.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 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! { lazy_static! {
pub static ref TEMPLATES: Tera = { pub static ref TEMPLATES: Tera = {
let mut tera = Tera::default(); 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(); t.register(&mut tera).unwrap();
} }
errors::register_templates(&mut tera); errors::register_templates(&mut tera);
auth::register_templates(&mut tera);
tera.autoescape_on(vec![".html", ".sql"]); tera.autoescape_on(vec![".html", ".sql"]);
//auth::register_templates(&mut tera); //auth::register_templates(&mut tera);
//gists::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)] #[cfg(test)]
mod tests { mod tests {
@ -129,7 +136,8 @@ mod tests {
let mut tera2 = Tera::default(); let mut tera2 = Tera::default();
for t in [ for t in [
BASE, FOOTER, PUB_NAV, BASE, FOOTER, PUB_NAV,
AUTH_NAV, auth::AUTH_CHALLENGE,
auth::AUTH_ADD,
// auth::AUTH_BASE, // auth::AUTH_BASE,
// auth::login::LOGIN, // auth::login::LOGIN,
// auth::register::REGISTER, // auth::register::REGISTER,

View file

@ -25,13 +25,16 @@ pub const PAGES: Pages = Pages::new();
pub struct Pages { pub struct Pages {
/// home page /// home page
pub home: &'static str, pub home: &'static str,
/// auth routes
pub auth: Auth,
} }
impl Pages { impl Pages {
/// create new instance of Routes /// create new instance of Routes
const fn new() -> Pages { const fn new() -> Pages {
let home = "/"; let home = "/";
Pages { home } let auth = Auth::new();
Pages { home, auth }
} }
} }
@ -41,7 +44,7 @@ pub struct Auth {
/// logout route /// logout route
pub logout: &'static str, pub logout: &'static str,
/// login route /// login route
pub login: &'static str, pub add: &'static str,
/// verify route /// verify route
pub verify: &'static str, pub verify: &'static str,
@ -50,11 +53,11 @@ pub struct Auth {
impl Auth { impl Auth {
/// create new instance of Authentication route /// create new instance of Authentication route
pub const fn new() -> Auth { pub const fn new() -> Auth {
let login = "/login"; let add = "/add";
let logout = "/logout"; let logout = "/logout";
let verify = "/verify"; let verify = "/verify";
Auth { Auth {
login, add,
logout, logout,
verify, verify,
} }

View file

@ -24,8 +24,9 @@ pub use crate::db::BoxDB;
pub use crate::federate::{get_federate, ArcFederate}; pub use crate::federate::{get_federate, ArcFederate};
use crate::settings::{DBType, Settings}; use crate::settings::{DBType, Settings};
//use actix_web::cookie::Cookie; //use actix_web::cookie::Cookie;
use crate::errors::*;
use crate::*;
use actix_web::test; use actix_web::test;
use actix_web::{ use actix_web::{
body::{BoxBody, EitherBody}, body::{BoxBody, EitherBody},
@ -34,8 +35,6 @@ use actix_web::{
http::StatusCode, http::StatusCode,
}; };
use serde::Serialize; use serde::Serialize;
use crate::errors::*;
use crate::*;
//pub mod sqlx_postgres { //pub mod sqlx_postgres {
// use super::*; // use super::*;
@ -71,7 +70,6 @@ pub mod sqlx_sqlite {
} }
} }
#[macro_export] #[macro_export]
macro_rules! get_cookie { macro_rules! get_cookie {
($resp:expr) => { ($resp:expr) => {

View file

@ -4,8 +4,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="{{ assets.css }}" /> <link rel="stylesheet" href="{{ assets.css }}" />
<title>Gists</title> <title>{% block title %} {% endblock %} | Starchart </title>
<title>{% block title %} {% endblock %}</title>
</head> </head>
<body> <body>
<header>{% block nav %} {% endblock %}</header> <header>{% block nav %} {% endblock %}</header>

View file

@ -1,28 +0,0 @@
<nav class="nav__container">
<input type="checkbox" class="nav__toggle" id="nav__toggle" />
<div class="nav__header">
<a class="nav__logo-container" href="/">
<p class="nav__home-btn">GitPad</p>
</a>
<label class="nav__hamburger-menu" for="nav__toggle">
<span class="nav__hamburger-inner"></span>
</label>
</div>
<div class="nav__spacer"></div>
<div class="nav__link-group">
<div class="nav__link-container">
<a class="nav__link" rel="noreferrer" href="{{ page.gist.new }}">New Paste</a>
</div>
{% if loggedin_user %}
<div class="nav__link-container">
<a class="nav__link" rel="noreferrer" href="{{ loggedin_user }}">Profile</a>
</div>
{% endif %}
<div class="nav__link-container">
<a class="nav__link" rel="noreferrer" href="{{ page.auth.logout }}">Log out</a>
</div>
</div>
</nav>

View file

@ -3,7 +3,7 @@
<div class="nav__header"> <div class="nav__header">
<a class="nav__logo-container" href="/"> <a class="nav__logo-container" href="/">
<p class="nav__home-btn">GitPad</p> <p class="nav__home-btn">STARCHART</p>
</a> </a>
<label class="nav__hamburger-menu" for="nav__toggle"> <label class="nav__hamburger-menu" for="nav__toggle">
<span class="nav__hamburger-inner"></span> <span class="nav__hamburger-inner"></span>
@ -14,13 +14,7 @@
<div class="nav__link-group"> <div class="nav__link-group">
<div class="nav__link-container"> <div class="nav__link-container">
<a class="nav__link" rel="noreferrer" href="/explore">Explore</a> <a class="nav__link" rel="noreferrer" href="{{ page.auth.add }}">Spider Forge</a>
</div>
<div class="nav__link-container">
<a class="nav__link" rel="noreferrer" href="{{ page.auth.login }}">Login</a>
</div>
<div class="nav__link-container">
<a class="nav__link" rel="noreferrer" href="{{ page.auth.register }}">Register</a>
</div> </div>
</div> </div>
</nav> </nav>