more shit.

This commit is contained in:
didier amyot committed 2023-02-05 20:13:24 -05:00
1 parent ba7763cbb1
commit e7cec774d8
4 files changed
+112 -28

No files matched your search

+26 -4
View File
@@ -1,3 +1,5 @@
#![feature(future_join)]
mod views;
mod config;
@@ -7,6 +9,10 @@ use minijinja_autoreload::AutoReloader;
use std::io::{Error, ErrorKind};
use std::path::Path;
use std::{collections::HashMap, env, fs, io, path::PathBuf};
use tokio::time::{sleep, Duration};
use futures::{pin_mut, select, FutureExt};
use actix_web::{
dev::ServiceResponse,
@@ -55,6 +61,15 @@ 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);
i +=1;
}
}
#[tokio::main]
pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> std::io::Result<()> {
// The closure is invoked every time the environment is outdated to recreate it.
@@ -74,7 +89,7 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> std::io
let tmpl_reloader = web::Data::new(tmpl_reloader);
HttpServer::new(move || {
let server = HttpServer::new(move || {
App::new()
.app_data(tmpl_reloader.clone())
.service(views::index)
@@ -86,9 +101,16 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> std::io
.wrap(Logger::default())
})
.workers(2)
.bind(("0.0.0.0", port))?
.run()
.await
.bind(("0.0.0.0", port))?;
let t1 = server.run().fuse();
let t2 = print_shit().fuse();
pin_mut!(t1, t2);
select!{
x = t1 => x,
y = t2 => y,
}
}
/// Error handler for a 404 Page not found error.
+9 -7
View File
@@ -3,6 +3,7 @@ use actix_web::http::header::q;
use clap::builder::TypedValueParser;
use minijinja::context;
use serde::Deserialize;
use std::collections::HashMap;
use crate::webui::MiniJinjaRenderer;
@@ -10,7 +11,6 @@ use crate::webui::MiniJinjaRenderer;
#[actix_web::get("/")]
pub async fn index(
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>,
) -> actix_web::Result<impl Responder> {
tmpl_env.render("index.html", ())
}
@@ -18,7 +18,6 @@ pub async fn index(
#[actix_web::get("/actions")]
pub async fn actions(
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>,
) -> actix_web::Result<impl Responder> {
tmpl_env.render("actions.html", ())
}
@@ -26,7 +25,6 @@ pub async fn actions(
#[actix_web::get("/overview")]
pub async fn overview(
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>,
) -> actix_web::Result<impl Responder> {
tmpl_env.render("overview.html", ())
}
@@ -35,7 +33,7 @@ pub async fn overview(
pub async fn overview_submit_button(
req: HttpRequest,
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>
query: web::Query<HashMap<String, String>>,
) -> actix_web::Result<impl Responder> {
let ctx = context! {
event_type => query.get("event_type").unwrap_or(&"".to_string()),
@@ -43,15 +41,19 @@ pub async fn overview_submit_button(
tmpl_env.render("partials/overview/submit_button.html", ctx)
}
#[derive(Deserialize, Debug)]
struct CreateEventPayload {
event: String,
event_type: String,
}
#[actix_web::post("/v1/events/{event_type}")]
pub async fn push_event(
req: HttpRequest,
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>,
web::Form(form): web::Form<CreateEventPayload>,
) -> actix_web::Result<impl Responder> {
println!(">> {:?}", req);
let shit = req.match_info().get("event_type").unwrap();
let event = req.match_info().get("event").unwrap();
let ctx = context! {
event_type => shit,
};