diff --git a/.gitignore b/.gitignore index 135676c..4cdd3e0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ /.direnv # Nix -/result* +result* diff --git a/nixie-server/src/main.rs b/nixie-server/src/main.rs index e2a7493..27be1d7 100644 --- a/nixie-server/src/main.rs +++ b/nixie-server/src/main.rs @@ -2,6 +2,7 @@ use std::sync::LazyLock; use std::time::Duration; use axum::Router; +use axum::extract::FromRequestParts; use axum::http::StatusCode; use axum::response::Html; use axum::response::IntoResponse; @@ -44,7 +45,9 @@ impl IntoResponse for AppError { ( StatusCode::INTERNAL_SERVER_ERROR, Html( - render_template("internal_error.tera.html", &error_context) + TERA.read() + .unwrap() + .render("internal_error.tera.html", &error_context) .unwrap_or_else(|_| "ERROR RENDERING ERROR! FATAL".to_string()), ), ) @@ -59,7 +62,7 @@ async fn main() -> anyhow::Result<()> { type AuthSession = axum_login::AuthSession; -#[derive(Clone)] +#[derive(Debug, Clone)] struct Backend { db: SqlitePool, } @@ -150,6 +153,7 @@ async fn run() -> anyhow::Result<()> { .route("/protected", get(show_protected)) .route_layer(login_required!(Backend, login_url = "/login")) .merge(users::routes()) + .route("/", get(show_index)) .layer(auth_layer) .layer(livereload) .with_state(AppState { db }); @@ -194,6 +198,10 @@ async fn run() -> anyhow::Result<()> { Ok(()) } +async fn show_index(renderer: Renderer) -> WebResult> { + renderer.render_template("index.tera.html", None).map(Html) +} + async fn shutdown_signal(handle: AbortHandle) { let ctrl_c = async { tokio::signal::ctrl_c() @@ -216,10 +224,33 @@ async fn shutdown_signal(handle: AbortHandle) { handle.abort(); } -pub fn render_template(name: &str, context: &Context) -> tera::Result { - TERA.read().unwrap().render(name, context) +#[derive(Debug, FromRequestParts)] +pub struct Renderer { + auth: AuthSession, } -async fn show_protected() -> Html { - Html(render_template("protected.tera.html", &Context::default()).unwrap()) +impl Renderer { + pub fn render_template( + &self, + name: &str, + context: impl Into>, + ) -> WebResult { + let mut main_context = Context::default(); + if let Some(user) = self.auth.user.as_ref() { + main_context.insert("logged_in", &true); + main_context.insert("current_user", user); + } + + main_context.extend(context.into().unwrap_or_default()); + + Ok(TERA.read().unwrap().render(name, &main_context)?) + } +} + +async fn show_protected(renderer: Renderer) -> Html { + Html( + renderer + .render_template("protected.tera.html", None) + .unwrap(), + ) } diff --git a/nixie-server/src/users/login.rs b/nixie-server/src/users/login.rs index 9e45694..9d5a575 100644 --- a/nixie-server/src/users/login.rs +++ b/nixie-server/src/users/login.rs @@ -3,15 +3,16 @@ use axum::http::StatusCode; use axum::response::Html; use axum::response::IntoResponse; use axum::response::Redirect; -use tera::Context; use crate::AuthSession; +use crate::Renderer; use crate::UserCredentials; use crate::WebResult; -use crate::render_template; -pub async fn show_login() -> WebResult> { - Ok(render_template("users/login.tera.html", &Context::default()).map(Html)?) +pub async fn show_login(renderer: Renderer) -> WebResult> { + renderer + .render_template("users/login.tera.html", None) + .map(Html) } pub async fn do_login( diff --git a/nixie-server/src/users/register.rs b/nixie-server/src/users/register.rs index 82e7fbb..2684bec 100644 --- a/nixie-server/src/users/register.rs +++ b/nixie-server/src/users/register.rs @@ -4,15 +4,16 @@ use axum::response::Html; use axum::response::IntoResponse; use axum::response::Redirect; use password_auth::generate_hash; -use tera::Context; use crate::AppState; +use crate::Renderer; use crate::UserCredentials; use crate::WebResult; -use crate::render_template; -pub async fn show_register() -> WebResult> { - Ok(render_template("users/register.tera.html", &Context::default()).map(Html)?) +pub async fn show_register(renderer: Renderer) -> WebResult> { + renderer + .render_template("users/register.tera.html", None) + .map(Html) } pub async fn do_register( diff --git a/nixie-server/templates/base.tera.html b/nixie-server/templates/base.tera.html index c0b50f0..cfe8f8f 100644 --- a/nixie-server/templates/base.tera.html +++ b/nixie-server/templates/base.tera.html @@ -5,6 +5,7 @@ + {% block head %} {% block title %}{% endblock title%} - Nixie CI {% endblock head%} @@ -12,15 +13,32 @@
diff --git a/nixie-server/templates/index.tera.html b/nixie-server/templates/index.tera.html new file mode 100644 index 0000000..6d1ceeb --- /dev/null +++ b/nixie-server/templates/index.tera.html @@ -0,0 +1,10 @@ +{% extends "base.tera.html" %} +{% import "inputs.tera.html" as inputs %} + +{% block title %} +Home +{% endblock title %} + +{% block content %} +Hi this is the main page +{% endblock content %} diff --git a/nixie-server/templates/users/register.tera.html b/nixie-server/templates/users/register.tera.html new file mode 100644 index 0000000..bcc4828 --- /dev/null +++ b/nixie-server/templates/users/register.tera.html @@ -0,0 +1,28 @@ +{% extends "base.tera.html" %} +{% import "inputs.tera.html" as inputs %} + +{% block title %} +Register +{% endblock title %} + +{% block content %} +
+
+

Login

+
+ {{ inputs::text_input(label="Username", name="username", id="username") }} + {{ inputs::text_input(label="Password", name="password", id="password", type="password") }} +
+ +
+ +
+
+
+{% endblock content %}