From de3defefa63a39d0512853e82bcdfa0e3f1d5b9f Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 23 Jul 2024 17:25:22 +0200 Subject: [PATCH] feat: add layout to all pages --- Cargo.lock | 4 +- Cargo.toml | 2 +- assets/styles/app.css | 6 +++ assets/styles/simple.css | 2 +- config.yaml | 6 +-- src/controllers.rs | 52 ++++++++++++++--------- src/main.rs | 13 ++++-- src/templates/layouts/base.html | 14 +++++- src/templates/pages/list_task_runs.html | 38 ++++++++++++++--- src/templates/pages/list_tasks.html | 47 ++++++++++++++------ src/templates/pages/run_task.html | 7 ++- src/templates/pages/task_run_details.html | 30 ++++++------- 12 files changed, 155 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e34789e..ef4e615 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -971,9 +971,9 @@ dependencies = [ [[package]] name = "minijinja" -version = "1.0.21" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55e877d961d4f96ce13615862322df7c0b6d169d40cab71a7ef3f9b9e594451e" +checksum = "45f7e8e35b6c7b169bf40b0176d2c79291ab8ee53290b84e0668ab21d841aa9d" dependencies = [ "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 36fe893..960f657 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ anyhow = "1.0.75" clap = "4.5.4" tokio = { version = "1.37.0", features = ["full"] } axum = { version = "0.7.5", features = ["json"] } -minijinja = { version = "1.0.20", features = ["builtins"] } +minijinja = { version = "2.1", features = ["builtins"] } uuid = { version = "1.8.0", features = ["serde", "v4"] } fully_pub = "0.1.4" log = "0.4.22" diff --git a/assets/styles/app.css b/assets/styles/app.css index e69de29..0aef0fe 100644 --- a/assets/styles/app.css +++ b/assets/styles/app.css @@ -0,0 +1,6 @@ +table { + width: 100%; +} +table tbody tr td:first-child { + padding-right: 3rem !important; +} diff --git a/assets/styles/simple.css b/assets/styles/simple.css index 6addf12..7994887 100644 --- a/assets/styles/simple.css +++ b/assets/styles/simple.css @@ -74,7 +74,7 @@ body { font-size: 1.15rem; line-height: 1.5; display: grid; - grid-template-columns: 1fr min(45rem, 90%) 1fr; + grid-template-columns: 1fr min(50rem, 90%) 1fr; margin: 0; } body > * { diff --git a/config.yaml b/config.yaml index e059de2..efac905 100644 --- a/config.yaml +++ b/config.yaml @@ -19,7 +19,7 @@ tasks: - ls - /etc/fstab schedule: - seconds: 15 + hours: 1 clean_up: name: Clean up things @@ -27,6 +27,6 @@ tasks: command: - cat - /etc/environment - schedule: - "0 * * * * *" + # schedule: + # "0 * * * * *" diff --git a/src/controllers.rs b/src/controllers.rs index 45759c7..3796ccb 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -19,11 +19,14 @@ pub async fn home( ) } -pub async fn list_tasks(State(app_state): State) -> Html { - Html(render!( - include_str!("./templates/pages/list_tasks.html"), - tasks => Vec::from_iter(app_state.config.tasks.iter()) - )) +pub async fn list_tasks(State(app_state): State) -> impl IntoResponse { + Html( + app_state.templating_env.get_template("pages/list_tasks.html").unwrap() + .render(context!( + tasks => Vec::from_iter(app_state.config.tasks.iter()) + )) + .unwrap() + ) } pub async fn trigger_task( @@ -50,10 +53,13 @@ pub async fn trigger_task( ( StatusCode::OK, - Html(render!( - include_str!("./templates/pages/run_task.html"), - task => task - )), + Html( + app_state.templating_env.get_template("pages/run_task.html").unwrap() + .render(context!( + task => task + )) + .unwrap() + ) ) } @@ -70,7 +76,7 @@ pub async fn list_task_runs( State(app_state): State, ExtractPath(task_id): ExtractPath, ) -> Html { - let runs = sqlx::query_as::<_, TaskRunSummary>("SELECT id,status,trigger_mode,submitted_at,started_at,ended_at FROM task_runs WHERE task_id = $1 LIMIT 100") + let runs = sqlx::query_as::<_, TaskRunSummary>("SELECT id,status,trigger_mode,submitted_at,started_at,ended_at FROM task_runs WHERE task_id = $1") .bind(&task_id) .fetch_all(&app_state.db) .await @@ -81,11 +87,15 @@ pub async fn list_task_runs( return Html("

Task not found

".to_string()); } }; - Html(render!( - include_str!("./templates/pages/list_task_runs.html"), - task => task, - runs => runs, - )) + Html( + app_state.templating_env.get_template("pages/list_task_runs.html").unwrap() + .render(context!( + task_id => task_id, + task => task, + runs => runs + )) + .unwrap() + ) } pub async fn get_task_run( @@ -102,8 +112,12 @@ pub async fn get_task_run( } }; - Html(render!( - include_str!("./templates/pages/task_run_details.html"), - run => run_details - )) + Html( + app_state.templating_env.get_template("pages/task_run_details.html").unwrap() + .render(context!( + run => run_details + )) + .unwrap() + ) } + diff --git a/src/main.rs b/src/main.rs index 37eeafb..c72c760 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,12 +48,19 @@ async fn main() -> Result<()> { let config: Config = get_config().expect("Cannot get config"); let mut templating_env = Environment::new(); + templating_env .add_template("layouts/base.html", include_str!("./templates/layouts/base.html")) .unwrap(); - templating_env - .add_template("pages/home.html", include_str!("./templates/pages/home.html")) - .unwrap(); + for path in fs::read_dir("./src/templates/pages").unwrap() { + let file_name = path.unwrap().file_name().into_string().unwrap(); + let path = format!("./src/templates/pages/{}", file_name); + let content: &'static str = Box::leak(fs::read_to_string(&path).unwrap().into_boxed_str()); + let path: &'static str = Box::leak(format!("pages/{}", file_name).into_boxed_str()); + templating_env + .add_template(&path, &content) + .unwrap(); + } let state = AppState { config, diff --git a/src/templates/layouts/base.html b/src/templates/layouts/base.html index d11c24d..b2aa9ac 100644 --- a/src/templates/layouts/base.html +++ b/src/templates/layouts/base.html @@ -8,8 +8,18 @@ -
+
+ +
+
{% block body %}{% endblock %} -
+ +
+ Autotasker +
diff --git a/src/templates/pages/list_task_runs.html b/src/templates/pages/list_task_runs.html index ceb0b54..6b71775 100644 --- a/src/templates/pages/list_task_runs.html +++ b/src/templates/pages/list_task_runs.html @@ -1,6 +1,32 @@ -

List of task runs for "{{ task.name }}"

-
    -{% for task_run in runs %} -
  • {{ task_run.id }} {{ task_run.status }}
  • -{% endfor %} -
+{% extends "layouts/base.html" %} +{% block body %} +

List of task runs for "{{ task.name }}"

+ + + + + + + + + + + {% for task_run in runs %} + + + + + + + {% endfor %} + +
IdDateStatusActions
+ {{ task_run.id | split("-") | first }} + + {{ task_run.started_at }} + + {{ task_run.status }} + + Details +
+{% endblock %} diff --git a/src/templates/pages/list_tasks.html b/src/templates/pages/list_tasks.html index 0aaa796..0a39086 100644 --- a/src/templates/pages/list_tasks.html +++ b/src/templates/pages/list_tasks.html @@ -1,13 +1,34 @@ -

Tasks

-{% if tasks | length == 0 %} -No tasks were configured. -{% endif %} - +{% extends "layouts/base.html" %} +{% block body %} +

Tasks

+ {% if tasks | length == 0 %} +

+ No tasks were configured. +

+ {% endif %} + + + + + + + + + + + {% for (id, task) in tasks %} + + + + + + {% endfor %} + +
NameTriggerSee runs
+ {{ task.name }} + + Trigger task + + See runs +
+{% endblock %} diff --git a/src/templates/pages/run_task.html b/src/templates/pages/run_task.html index 5d1788b..d9ecf6c 100644 --- a/src/templates/pages/run_task.html +++ b/src/templates/pages/run_task.html @@ -1 +1,6 @@ -Task "{{ task.name }}" triggered! +{% extends "layouts/base.html" %} +{% block body %} +

+ Task "{{ task.name }}" triggered! +

+{% endblock %} diff --git a/src/templates/pages/task_run_details.html b/src/templates/pages/task_run_details.html index 410d3cd..a33c381 100644 --- a/src/templates/pages/task_run_details.html +++ b/src/templates/pages/task_run_details.html @@ -1,16 +1,16 @@ -

Task run {{ run.id }}

+{% extends "layouts/base.html" %} +{% block body %} +

Task run details

+
    +
  • Id: {{ run.id }}
  • +
  • State: {{ run.status }}
  • +
  • Submitted at: {{ run.submitted_at }}
  • +
  • Started at: {{ run.started_at }}
  • +
  • Ended at: {{ run.ended_at }}
  • +
  • Trigger Mode: {{ run.trigger_mode }}
  • +
-
    -
  • Id: {{ run.id }}
  • -
  • State: {{ run.status }}
  • -
  • Submitted at: {{ run.submitted_at }}
  • -
  • Started at: {{ run.started_at }}
  • -
  • Ended at: {{ run.ended_at }}
  • -
  • Trigger Mode: {{ run.trigger_mode }}
  • -
- -

Logs

-

Exit code is {{ run.exit_code }}

-
-{{ run.logs }}
-
+

Logs

+

Exit code is {{ run.exit_code }}

+
{{ run.logs }}
+{% endblock %}