From 544d8a6f899b0bbecea8d41dd53813a97825d721 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 23 Jul 2024 17:37:02 +0200 Subject: [PATCH] refactor: build templating env + add global template meta variable --- TODO.md | 4 ++-- src/main.rs | 36 ++++++++++++++++++++------------- src/templates/layouts/base.html | 2 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/TODO.md b/TODO.md index 056986b..dd8c799 100644 --- a/TODO.md +++ b/TODO.md @@ -2,12 +2,12 @@ ## TODO -- [ ] Implement basic scheduler +- [x] Implement basic scheduler +- [x] Add basic CSS - [ ] Implement basic auth with OAuth2 - [ ] Validating config file - validate schedule CRON syntax - [ ] Load config file from `/etc/` -- [ ] Add CSS style with bootstrap - [ ] Add `Dockerfile` and docker-compose example - [ ] Add CI/CD to build docker image - [ ] Add configuration to limit the logs head and tail diff --git a/src/main.rs b/src/main.rs index c72c760..e77146d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use log::info; use anyhow::{anyhow, Context, Result}; use axum::routing::get; use axum::Router; -use minijinja::Environment; +use minijinja::{context, Environment}; use scheduler::run_scheduler; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; use sqlx::{ConnectOptions, Pool, Sqlite}; @@ -35,18 +35,7 @@ fn get_config() -> Result { .map_err(|e| anyhow!("Failed to parse config, {:?}", e)) } - -#[tokio::main] -async fn main() -> Result<()> { - env_logger::init(); - - info!("Starting autotasker"); - let pool = prepare_database().await.context("Prepare db")?; - - // start channel to talk to executor daemon - let (tx, rx) = mpsc::channel::(32); - - let config: Config = get_config().expect("Cannot get config"); +fn build_templating_env() -> Environment<'static> { let mut templating_env = Environment::new(); templating_env @@ -61,12 +50,31 @@ async fn main() -> Result<()> { .add_template(&path, &content) .unwrap(); } + templating_env.add_global("gl", context! { + instance => context! { + version => "1.243".to_string() + } + }); + templating_env +} + +#[tokio::main] +async fn main() -> Result<()> { + env_logger::init(); + + info!("Starting autotasker"); + let pool = prepare_database().await.context("Prepare db")?; + + // start channel to talk to executor daemon + let (tx, rx) = mpsc::channel::(32); + + let config: Config = get_config().expect("Cannot get config"); let state = AppState { config, db: pool, executor_tx: Arc::new(tx), - templating_env + templating_env: build_templating_env() }; // start executor daemon diff --git a/src/templates/layouts/base.html b/src/templates/layouts/base.html index b2aa9ac..3bd6eaf 100644 --- a/src/templates/layouts/base.html +++ b/src/templates/layouts/base.html @@ -19,7 +19,7 @@ {% block body %}{% endblock %}