98 lines
2.6 KiB
Rust
98 lines
2.6 KiB
Rust
use actix_web::{HttpRequest, Responder, web};
|
|
use actix_web::http::header::q;
|
|
use clap::builder::TypedValueParser;
|
|
use minijinja::context;
|
|
use serde::Deserialize;
|
|
use std::collections::HashMap;
|
|
use log::info;
|
|
use resource::resource_str;
|
|
|
|
use crate::webui::MiniJinjaRenderer;
|
|
|
|
#[actix_web::get("/")]
|
|
pub async fn index(
|
|
tmpl_env: MiniJinjaRenderer,
|
|
) -> actix_web::Result<impl Responder> {
|
|
tmpl_env.render("index.html", ())
|
|
}
|
|
|
|
#[actix_web::get("/actions")]
|
|
pub async fn actions(
|
|
tmpl_env: MiniJinjaRenderer,
|
|
) -> actix_web::Result<impl Responder> {
|
|
tmpl_env.render("actions.html", ())
|
|
}
|
|
|
|
#[actix_web::get("/overview")]
|
|
pub async fn overview(
|
|
tmpl_env: MiniJinjaRenderer,
|
|
) -> actix_web::Result<impl Responder> {
|
|
tmpl_env.render("overview.html", ())
|
|
}
|
|
|
|
#[actix_web::get("/partials/overview/submit_button")]
|
|
pub async fn overview_submit_button(
|
|
req: HttpRequest,
|
|
tmpl_env: MiniJinjaRenderer,
|
|
query: web::Query<HashMap<String, String>>,
|
|
) -> actix_web::Result<impl Responder> {
|
|
let ctx = context! {
|
|
event_type => query.get("event_type").unwrap_or(&"".to_string()),
|
|
};
|
|
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<HashMap<String, String>>,
|
|
) -> actix_web::Result<impl Responder> {
|
|
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)
|
|
}
|
|
|
|
|
|
#[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: Event,
|
|
event_type: String,
|
|
}
|
|
|
|
#[actix_web::post("/v1/events/{event_type}")]
|
|
pub async fn push_event(
|
|
req: HttpRequest,
|
|
tmpl_env: MiniJinjaRenderer,
|
|
event_channel: web::Data<async_channel::Sender<i32>>,
|
|
web::Json(form): web::Json<CreateEventPayload>,
|
|
) -> actix_web::Result<impl Responder> {
|
|
let shit = req.match_info().get("event_type").unwrap();
|
|
println!("payload: {:?}", form);
|
|
let _ = event_channel.send(56).await;
|
|
let ctx = context! {
|
|
event_type => shit,
|
|
};
|
|
tmpl_env.render("partials/v1/event_processed.html", ctx)
|
|
}
|