Fixed code styling.

This commit is contained in:
didier amyot committed 2023-02-11 10:27:24 -05:00
1 parent 7b716c1115
commit c8ae3d3ff9
11 files changed
+85 -96

No files matched your search

-3
View File
@@ -1,3 +0,0 @@
struct Config {
path: str,
}
+16 -31
View File
@@ -1,38 +1,35 @@
#![feature(future_join)]
use std::{collections::HashMap, env, fs, io, path::PathBuf};
use std::io::{Error, ErrorKind};
use std::path::Path;
use std::{env, path::PathBuf};
use crate::webui::worker::worker_loop;
use actix_files;
use actix_utils::future::{ready, Ready};
use actix_web::{
App,
dev::ServiceResponse,
error,
FromRequest,
http::{header::ContentType, StatusCode}, HttpRequest, HttpResponse, HttpServer, middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, Responder, Result, web,
http::{header::ContentType, StatusCode},
middleware::{ErrorHandlerResponse, ErrorHandlers, Logger},
web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result,
};
use actix_web_lab::respond::Html;
use async_channel;
use futures::{FutureExt, pin_mut, select};
use futures::{pin_mut, select, FutureExt};
use minijinja;
use minijinja_autoreload::AutoReloader;
use tokio::time::{Duration, sleep};
mod views;
mod config;
mod worker;
struct MiniJinjaRenderer {
tmpl_env: web::Data<minijinja_autoreload::AutoReloader>,
}
impl MiniJinjaRenderer {
fn render(
&self,
tmpl: &str,
ctx: impl Into<minijinja::value::Value>,
) -> Result<Html> {
fn render(&self, tmpl: &str, ctx: impl Into<minijinja::value::Value>) -> Result<Html> {
log::info!("Rendering template {:?}", tmpl);
self.tmpl_env
.acquire_env()
@@ -61,19 +58,6 @@ impl FromRequest for MiniJinjaRenderer {
}
}
async fn worker_loop(r: async_channel::Receiver<i32>) -> io::Result<()> {
loop {
let x = r.recv().await;
match x {
Ok(v) => println!("Processed value: {:?}", v),
Err(e) => {
println!("Error: {:?}", e);
return Ok(())
}
}
}
}
#[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.
@@ -99,8 +83,9 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> std::io
App::new()
.app_data(tmpl_reloader.clone())
.app_data(web::Data::new(s.clone()))
.service(actix_files::Files::new("/static", "./resources/static/")
.use_last_modified(true))
.service(
actix_files::Files::new("/static", "./resources/static/").use_last_modified(true),
)
.service(views::index)
.service(views::actions)
.service(views::overview)
@@ -117,7 +102,7 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> std::io
let t2 = worker_loop(r).fuse();
pin_mut!(t1, t2);
select!{
select! {
x = t1 => x,
y = t2 => y,
}
+17 -21
View File
@@ -1,38 +1,32 @@
use actix_web::{HttpRequest, Responder, web};
use actix_web::http::header::q;
use actix_web::{web, HttpRequest, Responder};
use clap::builder::TypedValueParser;
use log::info;
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> {
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> {
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> {
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,
_req: HttpRequest,
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>,
) -> actix_web::Result<impl Responder> {
@@ -44,32 +38,34 @@ pub async fn overview_submit_button(
#[actix_web::get("/partials/actions/action-form")]
pub async fn actions_partials(
req: HttpRequest,
_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 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{
struct Event1 {
name: String,
age: i64,
}
#[derive(Deserialize, Debug)]
struct Event2{
struct Event2 {
name: String,
score: i64,
}
#[derive(Deserialize, Debug)]
enum Event{
enum Event {
Event1(Event1),
Event2(Event2),
}
+14
View File
@@ -0,0 +1,14 @@
use std::{io};
pub async fn worker_loop(r: async_channel::Receiver<i32>) -> io::Result<()> {
loop {
let x = r.recv().await;
match x {
Ok(v) => println!("Processed value: {:?}", v),
Err(e) => {
println!("Error: {:?}", e);
return Ok(());
}
}
}
}