Added a bunch of things
This commit is contained in:
1 parent
c365289914
commit
ce92916942
15 files changed
+131
-55
No files matched your search
@@ -1,4 +1,4 @@
|
||||
use crate::webui::services::Services;
|
||||
use crate::webui::services::ServicesFactory;
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
|
||||
use actix_web::{web, HttpRequest, Responder};
|
||||
@@ -15,7 +15,7 @@ pub struct LoginPayload {
|
||||
pub async fn login(
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
web::Json(form): web::Json<LoginPayload>,
|
||||
service_factory: web::Data<Services>,
|
||||
service_factory: web::Data<ServicesFactory>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let authentication_service = service_factory.get_authentication_service()?;
|
||||
let cookie = authentication_service.create_login_cookie(&form.username);
|
||||
@@ -30,7 +30,7 @@ pub async fn login(
|
||||
|
||||
#[actix_web::post("/logout")]
|
||||
pub async fn logout(
|
||||
service_factory: web::Data<Services>,
|
||||
service_factory: web::Data<ServicesFactory>,
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let authentication_service = service_factory.get_authentication_service()?;
|
||||
@@ -43,7 +43,7 @@ pub async fn logout(
|
||||
#[actix_web::get("/")]
|
||||
pub async fn index(
|
||||
req: HttpRequest,
|
||||
service_factory: web::Data<Services>,
|
||||
service_factory: web::Data<ServicesFactory>,
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let authentication_service = service_factory.get_authentication_service()?;
|
||||
|
||||
+3
-3
@@ -18,7 +18,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::events::Event;
|
||||
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
||||
use crate::webui::services::{creat_db_pool, Services};
|
||||
use crate::webui::services::{creat_db_pool, ServicesFactory};
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
use crate::webui::worker::{event_processor_loop, EventProcessor};
|
||||
|
||||
@@ -80,11 +80,12 @@ pub async fn web_ui_main(
|
||||
) -> CustomResult<()> {
|
||||
let (event_sender, event_receiver) = async_channel::unbounded::<(Uuid, Event)>();
|
||||
let pool = creat_db_pool(&conn_fact.path().unwrap());
|
||||
let services = Services::new(pool);
|
||||
let services = ServicesFactory::new(pool);
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
let template_reloader = create_auto_reloader(enable_template_autoreload);
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.app_data(web::Data::new(services.clone()))
|
||||
.app_data(web::Data::new(template_reloader))
|
||||
.app_data(web::Data::new(event_sender.clone()))
|
||||
@@ -92,7 +93,6 @@ pub async fn web_ui_main(
|
||||
.service(create_api_scope())
|
||||
.service(create_views_scope())
|
||||
.wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found))
|
||||
.wrap(Logger::default())
|
||||
})
|
||||
.workers(2)
|
||||
.bind(("0.0.0.0", port))
|
||||
|
||||
+12
-5
@@ -1,4 +1,4 @@
|
||||
use crate::services::{SessionService, UserService};
|
||||
use crate::services::{SecurityService, SessionService, UserService};
|
||||
|
||||
use actix_web::{error, Result};
|
||||
use r2d2::PooledConnection;
|
||||
@@ -19,17 +19,17 @@ struct ServicesCache {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Services {
|
||||
pub struct ServicesFactory {
|
||||
pool: DbPool,
|
||||
cache: ServicesCache,
|
||||
}
|
||||
|
||||
impl Services {
|
||||
pub fn new(pool: DbPool) -> Services {
|
||||
impl ServicesFactory {
|
||||
pub fn new(pool: DbPool) -> ServicesFactory {
|
||||
let mut conn = pool.get().unwrap();
|
||||
let session_service = SessionService::from_conn(&mut conn).unwrap();
|
||||
let cache = ServicesCache { session_service };
|
||||
Services { pool, cache }
|
||||
ServicesFactory { pool, cache }
|
||||
}
|
||||
|
||||
pub fn get_connection(&self) -> Result<PooledConnection<SqliteConnectionManager>> {
|
||||
@@ -48,4 +48,11 @@ impl Services {
|
||||
) -> Result<UserService> {
|
||||
Ok(UserService::new(conn))
|
||||
}
|
||||
|
||||
pub fn get_security_service<'a>(
|
||||
&'a self,
|
||||
conn: &'a PooledConnection<SqliteConnectionManager>,
|
||||
) -> Result<SecurityService> {
|
||||
Ok(SecurityService::new(conn))
|
||||
}
|
||||
}
|
||||
+17
-8
@@ -1,14 +1,16 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{error, web, HttpRequest, Responder};
|
||||
use actix_web_lab::respond::Html;
|
||||
|
||||
use log::info;
|
||||
use minijinja::context;
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::events::Event;
|
||||
|
||||
use crate::webui::services::Services;
|
||||
use crate::webui::services::ServicesFactory;
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
|
||||
#[actix_web::get("/actions")]
|
||||
@@ -24,7 +26,7 @@ pub async fn accounts(tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Res
|
||||
#[actix_web::get("/overview")]
|
||||
pub async fn overview(
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
service_factory: web::Data<Services>,
|
||||
service_factory: web::Data<ServicesFactory>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let conn = service_factory.get_connection()?;
|
||||
let user_service = service_factory.get_user_service(&conn)?;
|
||||
@@ -40,7 +42,7 @@ pub async fn overview(
|
||||
pub async fn overview_submit_button(
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
) -> actix_web::Result<Html> {
|
||||
let ctx = context! {
|
||||
event_type => query.get("event_type").unwrap_or(&"".to_string()),
|
||||
};
|
||||
@@ -51,12 +53,18 @@ pub async fn overview_submit_button(
|
||||
pub async fn actions_partials(
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
service_factory: web::Data<ServicesFactory>,
|
||||
) -> actix_web::Result<Html> {
|
||||
let event_type = query
|
||||
.get("event_type")
|
||||
.unwrap_or(&"404".to_string())
|
||||
.to_string();
|
||||
let ctx = context! {event_type => event_type};
|
||||
let conn = service_factory.get_connection()?;
|
||||
let security_service = service_factory.get_security_service(&conn)?;
|
||||
let ctx = context! {
|
||||
event_type => event_type,
|
||||
securities => security_service.list_securities(),
|
||||
};
|
||||
let template_location = format!("partials/actions/{:}.html", event_type);
|
||||
info!("Getting template at: {:?}", template_location);
|
||||
tmpl_env.render(template_location.as_str(), ctx)
|
||||
@@ -66,15 +74,16 @@ pub async fn actions_partials(
|
||||
pub struct CreateEventPayload {
|
||||
event: Event,
|
||||
event_type: String,
|
||||
initiator: String,
|
||||
}
|
||||
|
||||
#[actix_web::post("/events/{event_type}")]
|
||||
pub async fn push_event(
|
||||
req: HttpRequest,
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
event_channel: web::Data<async_channel::Sender<Event>>,
|
||||
event_channel: web::Data<async_channel::Sender<(Uuid, Event)>>,
|
||||
web::Json(form): web::Json<CreateEventPayload>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
) -> actix_web::Result<Html> {
|
||||
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."));
|
||||
@@ -83,7 +92,7 @@ pub async fn push_event(
|
||||
if validation.is_err() {
|
||||
return Err(error::ErrorBadRequest(validation.err().unwrap()));
|
||||
}
|
||||
let _ = event_channel.send(form.event).await;
|
||||
let _ = event_channel.send((Uuid::new_v4(), form.event)).await;
|
||||
let ctx = context! {
|
||||
event_type => "event received.",
|
||||
};
|
||||
|
||||
+5
-5
@@ -109,7 +109,7 @@ pub mod test_worker {
|
||||
use super::*;
|
||||
use crate::events::CreateNewUser;
|
||||
use crate::migration;
|
||||
use futures::{future, TryFutureExt};
|
||||
use futures::future;
|
||||
use std::sync::Mutex;
|
||||
|
||||
struct StatefulEventProcessor {
|
||||
@@ -122,13 +122,13 @@ pub mod test_worker {
|
||||
}
|
||||
|
||||
fn call_count(&self) -> i32 {
|
||||
let mut m = self.m.lock().unwrap();
|
||||
let m = self.m.lock().unwrap();
|
||||
m.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl EventProcessor for StatefulEventProcessor {
|
||||
fn process_event(&self, conn: &mut Connection, event: Event) -> CustomResult<()> {
|
||||
fn process_event(&self, _conn: &mut Connection, event: Event) -> CustomResult<()> {
|
||||
let mut m = self.m.lock().unwrap();
|
||||
*m += 1;
|
||||
get_event_processing_result(&event)
|
||||
@@ -186,7 +186,7 @@ pub mod test_worker {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_on_close_channel() {
|
||||
let mut ep = StatefulEventProcessor::new();
|
||||
let ep = StatefulEventProcessor::new();
|
||||
let (event_sender, event_receiver) = async_channel::unbounded::<(Uuid, Event)>();
|
||||
event_sender.close();
|
||||
let r = event_processor_loop(&mut get_connection(), &ep, event_receiver).await;
|
||||
@@ -196,7 +196,7 @@ pub mod test_worker {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_basic() -> CustomResult<()> {
|
||||
let mut ep = StatefulEventProcessor::new();
|
||||
let ep = StatefulEventProcessor::new();
|
||||
let mut conn = get_connection();
|
||||
let (event_sender, event_receiver) = async_channel::unbounded::<(Uuid, Event)>();
|
||||
let f1 = send_events(event_sender, get_events());
|
||||
|
||||
Reference in new issue
Block a user