diff --git a/.justfile b/.justfile index da5dbc6..94285c5 100644 --- a/.justfile +++ b/.justfile @@ -10,7 +10,7 @@ docker-compose +ARGS: start-dev-services: (docker-compose "up --remove-orphans") dev: - watchexec --restart --clear --watch src cargo run + watchexec --restart --clear --watch src --watch templates cargo run migrate: (docker-compose "run dbmate migrate") diff --git a/Cargo.lock b/Cargo.lock index a4bdc19..81af35d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,7 +115,9 @@ name = "compendium" version = "0.1.0" dependencies = [ "axum", + "minijinja", "notify", + "serde", "tokio", "tower-livereload", ] @@ -363,6 +365,15 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "minijinja" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff7b8df5e85e30b87c2b0b3f58ba3a87b68e133738bf512a7713769326dbca9" +dependencies = [ + "serde", +] + [[package]] name = "miniz_oxide" version = "0.8.4" @@ -699,7 +710,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 214691e..ce91065 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" [dependencies] axum = "0.8.1" +minijinja = "2.7.0" notify = "8.0.0" +serde = "1.0.217" tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] } tower-livereload = "0.9.6" diff --git a/src/main.rs b/src/main.rs index d90740f..6835694 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,28 @@ +use std::sync::Arc; + +use axum::extract::State; +use axum::http::StatusCode; use axum::response::Html; use axum::routing::get; use axum::Router; +use minijinja::{context, Environment}; + +struct AppState { + tmpl_env: Environment<'static>, +} #[tokio::main] async fn main() { - let app = Router::new().route("/", get(|| async { Html("

Hello, World! 2

") })); + let mut tmpl_env = Environment::new(); + tmpl_env + .add_template("base", include_str!("../templates/base.html")) + .unwrap(); + + let app_state = Arc::new(AppState { tmpl_env }); + + let app = Router::new() + .route("/", get(handler_home)) + .with_state(app_state); // Add hot reload only on dev mode #[cfg(debug_assertions)] @@ -13,3 +31,11 @@ async fn main() { let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } + +async fn handler_home(State(state): State>) -> Result, StatusCode> { + let template = state.tmpl_env.get_template("base").unwrap(); + + let content = template.render(context!()).unwrap(); + + Ok(Html(content)) +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..efeb70d --- /dev/null +++ b/templates/base.html @@ -0,0 +1,10 @@ + + + + + Compendium + + +

Hello world!

+ +