diff --git a/README.md b/README.md index 06a9a9d..5b3f38b 100644 --- a/README.md +++ b/README.md @@ -26,15 +26,19 @@ The rust source must be `/nix/store/pmih7jv9r5xfaif92bza8w05876qvnvc-rust-src/` nix develop ``` -## Workwork +## Todos + +#### Architecture + +* [ ] put all the ressources (migrations, templates and statics) into a ressources directory + +#### Backend * [ ] Parametrize `CURRENT_TIMESTAMP` -### Todo - -#### API - - #### Front end -* [ ] return hello world. +* [ ] Serve static files using actix +* [ ] Hack https://github.com/bigskysoftware/htmx/blob/master/src/ext/json-enc.js to all embedded fields. +* [ ] Create UI to create user +* [ ] Create UI to create security diff --git a/src/webui/mod.rs b/src/webui/mod.rs index aeee030..4cdb935 100644 --- a/src/webui/mod.rs +++ b/src/webui/mod.rs @@ -64,8 +64,8 @@ impl FromRequest for MiniJinjaRenderer { async fn print_shit() -> std::io::Result<()>{ let mut i = 0; loop { - sleep(Duration::from_millis(1000)).await; - println!("he he! {:?}", i); + sleep(Duration::from_millis(10_000)).await; + println!("Loop on a different thread {:?}", i); i +=1; } } @@ -96,6 +96,8 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> std::io .service(views::actions) .service(views::overview) .service(views::overview_submit_button) + .service(views::actions_partials) + .service(views::static_file) .service(views::push_event) .wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)) .wrap(Logger::default()) diff --git a/src/webui/views.rs b/src/webui/views.rs index acbaa78..e676af4 100644 --- a/src/webui/views.rs +++ b/src/webui/views.rs @@ -5,6 +5,8 @@ use minijinja::context; use serde::Deserialize; use std::collections::HashMap; +use log::info; +use resource::resource_str; use crate::webui::MiniJinjaRenderer; @@ -41,9 +43,55 @@ pub async fn overview_submit_button( tmpl_env.render("partials/overview/submit_button.html", ctx) } +#[actix_web::get("/partials/actions/action-form")] +pub async fn actions_partials( + req: HttpRequest, + tmpl_env: MiniJinjaRenderer, + query: web::Query>, +) -> actix_web::Result { + let event_type = query.get("event_type").unwrap_or(&"404".to_string()).to_string(); + let ctx = context!{event_type => event_type}; + let template_location = format!("partials/actions/{:}.html", event_type); + info!("Getting template at: {:?}", template_location); + tmpl_env.render(template_location.as_str(), ctx) +} + + + +#[actix_web::get("/static/{file_name}")] +pub async fn static_file( + req: HttpRequest +) -> actix_web::Result { + let file_name = req.match_info().get("file_name").unwrap_or(&"404.html".to_string()).to_string(); + let template_location = format!("static/{:}", file_name); + info!("Getting static file: {:?}", file_name); + Ok(template_location) +} + + + + +#[derive(Deserialize, Debug)] +struct Event1{ + name: String, + age: i64, +} + +#[derive(Deserialize, Debug)] +struct Event2{ + name: String, + score: i64, +} + +#[derive(Deserialize, Debug)] +enum Event{ + Event1(Event1), + Event2(Event2), +} + #[derive(Deserialize, Debug)] struct CreateEventPayload { - event: String, + event: Event, event_type: String, } @@ -51,9 +99,10 @@ struct CreateEventPayload { pub async fn push_event( req: HttpRequest, tmpl_env: MiniJinjaRenderer, - web::Form(form): web::Form, + web::Json(form): web::Json, ) -> actix_web::Result { let shit = req.match_info().get("event_type").unwrap(); + println!("payload: {:?}", form); let ctx = context! { event_type => shit, }; diff --git a/templates/actions.html b/templates/actions.html index 503e801..982b5c6 100644 --- a/templates/actions.html +++ b/templates/actions.html @@ -14,25 +14,19 @@ class="uk-select" aria-label="Select" class="uk-button uk-button-default" - hx-get="/partials/overview/submit_button" + hx-get="/partials/actions/action-form" hx-trigger="change,load" - hx-target="#submit-button" - hx-swap="outerHTML">> + hx-target="#action-form" + hx-swap="innerHTML">> -
- - -
+
+ +
-
diff --git a/templates/base.html b/templates/base.html index 06e4d67..247903c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -8,6 +8,7 @@ + diff --git a/templates/partials/actions/create_security.html b/templates/partials/actions/create_security.html new file mode 100644 index 0000000..2c0d246 --- /dev/null +++ b/templates/partials/actions/create_security.html @@ -0,0 +1,7 @@ + + + +{%include "partials/actions/submit_button.html"%} diff --git a/templates/partials/actions/create_user.html b/templates/partials/actions/create_user.html new file mode 100644 index 0000000..0df37e0 --- /dev/null +++ b/templates/partials/actions/create_user.html @@ -0,0 +1,10 @@ +
+ + +
+ +{%include "partials/actions/submit_button.html"%} diff --git a/templates/partials/overview/submit_button.html b/templates/partials/actions/submit_button.html similarity index 85% rename from templates/partials/overview/submit_button.html rename to templates/partials/actions/submit_button.html index 2c2500f..8c671c2 100644 --- a/templates/partials/overview/submit_button.html +++ b/templates/partials/actions/submit_button.html @@ -3,6 +3,7 @@ hx-post="/v1/events/{{event_type}}" hx-trigger="click" hx-target="#event-table-first-row" + hx-ext="json-enc" hx-swap="outerHTML"> - Submit + Submit ss \ No newline at end of file diff --git a/templates/static/embedded-json-enc.js b/templates/static/embedded-json-enc.js new file mode 100644 index 0000000..dcba3b8 --- /dev/null +++ b/templates/static/embedded-json-enc.js @@ -0,0 +1,14 @@ +(function() { + htmx.defineExtension('embedded-json-enc', { + onEvent: function (name, evt) { + if (name === "htmx:configRequest") { + evt.detail.headers['Content-Type'] = "application/json"; + } + }, + + encodeParameters : function(xhr, parameters, elt) { + xhr.overrideMimeType('text/json'); + return (JSON.stringify(parameters)); + } + }); +})()