Started to implement proper event processing.
This commit is contained in:
1 parent
af11915a52
commit
238cd9f2da
7 files changed
+75
-88
No files matched your search
+14
-11
@@ -4,19 +4,21 @@ use std::{env, path::PathBuf};
|
||||
|
||||
use actix_files;
|
||||
use actix_web::{
|
||||
App,
|
||||
dev::ServiceResponse,
|
||||
error,
|
||||
http::{header::ContentType, StatusCode},
|
||||
middleware::{ErrorHandlerResponse, ErrorHandlers, Logger},
|
||||
web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result,
|
||||
FromRequest,
|
||||
http::{header::ContentType, StatusCode}, HttpRequest, HttpResponse, HttpServer, middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, Responder, Result, web,
|
||||
};
|
||||
use actix_web_lab::respond::Html;
|
||||
use async_channel;
|
||||
use futures::{pin_mut, select, FutureExt};
|
||||
use futures::{FutureExt, pin_mut, select};
|
||||
use minijinja;
|
||||
use minijinja_autoreload::AutoReloader;
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::util::CustomResult;
|
||||
use crate::events::Event;
|
||||
use crate::util::{CustomResult, Error};
|
||||
use crate::webui::worker::worker_loop;
|
||||
|
||||
mod views;
|
||||
@@ -57,7 +59,9 @@ impl FromRequest for MiniJinjaRenderer {
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> CustomResult<()> {
|
||||
pub async fn web_ui_main(conn: &mut Connection,
|
||||
port: u16,
|
||||
enable_template_autoreload: bool) -> CustomResult<()> {
|
||||
// The closure is invoked every time the environment is outdated to recreate it.
|
||||
let tmpl_reloader = AutoReloader::new(move |notifier| {
|
||||
let mut env: minijinja::Environment<'static> = minijinja::Environment::new();
|
||||
@@ -73,7 +77,7 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> CustomR
|
||||
Ok(env)
|
||||
});
|
||||
|
||||
let (s, r) = async_channel::unbounded::<i32>();
|
||||
let (s, r) = async_channel::unbounded::<Event>();
|
||||
|
||||
let tmpl_reloader = web::Data::new(tmpl_reloader);
|
||||
|
||||
@@ -98,14 +102,13 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> CustomR
|
||||
.unwrap();
|
||||
|
||||
let t1 = server.run().fuse();
|
||||
let t2 = worker_loop(r).fuse();
|
||||
let t2 = worker_loop(conn, r).fuse();
|
||||
pin_mut!(t1, t2);
|
||||
|
||||
select! {
|
||||
x = t1 => x,
|
||||
x = t1 => x.map_err(Error::IOError),
|
||||
y = t2 => y,
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Error handler for a 404 Page not found error.
|
||||
|
||||
+10
-25
@@ -1,12 +1,12 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{web, HttpRequest, Responder};
|
||||
use actix_web::{error, HttpRequest, Responder, web};
|
||||
use clap::builder::TypedValueParser;
|
||||
use log::info;
|
||||
use minijinja::context;
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::events::Event;
|
||||
use crate::webui::MiniJinjaRenderer;
|
||||
|
||||
#[actix_web::get("/")]
|
||||
@@ -52,23 +52,6 @@ pub async fn actions_partials(
|
||||
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 {
|
||||
@@ -80,14 +63,16 @@ struct CreateEventPayload {
|
||||
pub async fn push_event(
|
||||
req: HttpRequest,
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
event_channel: web::Data<async_channel::Sender<i32>>,
|
||||
event_channel: web::Data<async_channel::Sender<Event>>,
|
||||
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 path_event_type = req.match_info().get("event_type").unwrap();
|
||||
if path_event_type.to_string() != form.event_type {
|
||||
return Err(error::ErrorBadRequest("Inconsistent event type."))
|
||||
}
|
||||
let _ = event_channel.send(form.event).await;
|
||||
let ctx = context! {
|
||||
event_type => shit,
|
||||
event_type => "bob le chef",
|
||||
};
|
||||
tmpl_env.render("partials/v1/event_processed.html", ctx)
|
||||
}
|
||||
+13
-5
@@ -1,13 +1,21 @@
|
||||
use std::io;
|
||||
|
||||
pub async fn worker_loop(r: async_channel::Receiver<i32>) -> io::Result<()> {
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::event_processor::process_event;
|
||||
use crate::events::Event;
|
||||
use crate::util::CustomResult;
|
||||
use crate::util::Error::FuckingError;
|
||||
|
||||
pub async fn worker_loop(conn: &mut Connection,
|
||||
r: async_channel::Receiver<Event>) -> CustomResult<()> {
|
||||
loop {
|
||||
let x = r.recv().await;
|
||||
match x {
|
||||
Ok(v) => println!("Processed value: {:?}", v),
|
||||
let res = r.recv().await;
|
||||
match res {
|
||||
Ok(event) => process_event(conn, event)?, // todo, remove ?
|
||||
Err(e) => {
|
||||
println!("Error: {:?}", e);
|
||||
return Ok(());
|
||||
return Err(FuckingError(e.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user