Used r2d2 for db connection pool.
This commit is contained in:
1 parent
5df6d5afd1
commit
18f07ac215
8 files changed
+56
-52
No files matched your search
@@ -5,6 +5,7 @@ use cookie::time::OffsetDateTime;
|
||||
use cookie::{Cookie, CookieJar, Key};
|
||||
use rusqlite::Connection;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SessionService {
|
||||
key: Key,
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ use rusqlite::Connection;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
|
||||
use crate::db::users::{list_users, User};
|
||||
use crate::util::CustomResult;
|
||||
|
||||
|
||||
+1
-5
@@ -1,4 +1,4 @@
|
||||
use crate::{CustomResult};
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub use crate::services::_authentication::SessionService;
|
||||
@@ -23,8 +23,4 @@ impl ReadOnlyServiceFactory<'_> {
|
||||
pub fn get_user_service(&self) -> UserService {
|
||||
UserService::new(self.conn)
|
||||
}
|
||||
|
||||
pub fn get_authentication_service(&mut self) -> CustomResult<SessionService> {
|
||||
SessionService::from_conn(self.conn)
|
||||
}
|
||||
}
|
||||
+10
-15
@@ -1,10 +1,10 @@
|
||||
use actix_web::{web, HttpRequest, Responder};
|
||||
use crate::services::ReadOnlyServiceFactory;
|
||||
|
||||
use crate::webui::services::{Services};
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
use crate::SQLiteConnectionFactory;
|
||||
|
||||
use actix_web::{web, HttpRequest, Responder};
|
||||
use minijinja::context;
|
||||
use serde::Deserialize;
|
||||
use crate::webui::services::{DbPool, Services};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct LoginPayload {
|
||||
@@ -16,11 +16,9 @@ pub struct LoginPayload {
|
||||
pub async fn login(
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
web::Json(form): web::Json<LoginPayload>,
|
||||
conn_factory: web::Data<SQLiteConnectionFactory>,
|
||||
service_factory: web::Data<Services>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||
let authentication_service = service_factory.get_authentication_service()?;
|
||||
let cookie = authentication_service.create_login_cookie(&form.username);
|
||||
let ctx = context! {
|
||||
name => form.username,
|
||||
@@ -33,13 +31,10 @@ pub async fn login(
|
||||
|
||||
#[actix_web::post("/logout")]
|
||||
pub async fn logout(
|
||||
conn_factory: web::Data<SQLiteConnectionFactory>,
|
||||
service_factory: web::Data<Services>,
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||
|
||||
let authentication_service = service_factory.get_authentication_service()?;
|
||||
let res = tmpl_env.render("partials/index/form.html", context! {})?;
|
||||
Ok(res
|
||||
.customize()
|
||||
@@ -49,10 +44,10 @@ pub async fn logout(
|
||||
#[actix_web::get("/")]
|
||||
pub async fn index(
|
||||
req: HttpRequest,
|
||||
pool: web::Data<DbPool>,
|
||||
service_factory: web::Data<Services>,
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let authentication_service = Services::get_authentication_service(& pool).unwrap();
|
||||
let authentication_service = service_factory.get_authentication_service()?;
|
||||
let userid = authentication_service.get_login(&req);
|
||||
let username = userid.clone().unwrap_or("".to_string());
|
||||
let ctx = context! {
|
||||
|
||||
+5
-8
@@ -12,11 +12,11 @@ use async_channel;
|
||||
use futures::{pin_mut, select, FutureExt};
|
||||
use minijinja;
|
||||
use minijinja_autoreload::AutoReloader;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
|
||||
use crate::events::Event;
|
||||
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
||||
use crate::webui::services::creat_db_pool;
|
||||
use crate::webui::services::{creat_db_pool, Services};
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
use crate::webui::worker::event_processor_loop;
|
||||
|
||||
@@ -62,8 +62,6 @@ fn create_views_scope() -> Scope {
|
||||
.service(views::actions_partials)
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn web_ui_main(
|
||||
conn_fact: SQLiteConnectionFactory,
|
||||
@@ -77,15 +75,14 @@ pub async fn web_ui_main(
|
||||
// https://docs.rs/actix-web/4.3.1/actix_web/web/fn.block.html
|
||||
// in handlers.
|
||||
let pool = creat_db_pool(&conn_fact.path().unwrap());
|
||||
// let db_pool = web::Data::new();
|
||||
let cloned_conn_fact = conn_fact.clone();
|
||||
let services = Services::new(pool);
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
let template_reloader = create_auto_reloader(enable_template_autoreload);
|
||||
App::new()
|
||||
.app_data(web::Data::new(pool.clone()))
|
||||
.app_data(web::Data::new(services.clone()))
|
||||
.app_data(web::Data::new(template_reloader))
|
||||
.app_data(web::Data::new(event_sender.clone()))
|
||||
.app_data(web::Data::new(cloned_conn_fact.clone()))
|
||||
.service(create_static_scope())
|
||||
.service(create_api_scope())
|
||||
.service(create_views_scope())
|
||||
|
||||
+32
-8
@@ -1,7 +1,10 @@
|
||||
use std::path::PathBuf;
|
||||
use crate::services::{SessionService, UserService};
|
||||
|
||||
use actix_web::{error, Result};
|
||||
use r2d2::PooledConnection;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use crate::services::{SessionService};
|
||||
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub type DbPool = r2d2::Pool<SqliteConnectionManager>;
|
||||
|
||||
@@ -10,18 +13,39 @@ pub fn creat_db_pool(path: & PathBuf) -> DbPool {
|
||||
r2d2::Pool::new(manager).unwrap()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ServicesCache {
|
||||
session_service: SessionService,
|
||||
}
|
||||
|
||||
pub struct Services {}
|
||||
#[derive(Clone)]
|
||||
pub struct Services {
|
||||
pool: DbPool,
|
||||
cache: ServicesCache,
|
||||
}
|
||||
|
||||
impl Services {
|
||||
|
||||
pub fn get_authentication_service(dbPool: & DbPool) -> CustomResult<SessionService> {
|
||||
let mut conn = dbPool.get().unwrap();
|
||||
SessionService::from_conn(&mut conn)
|
||||
pub fn new(pool: DbPool) -> Services {
|
||||
let mut conn = pool.get().unwrap();
|
||||
let session_service = SessionService::from_conn(&mut conn).unwrap();
|
||||
let cache = ServicesCache { session_service };
|
||||
Services { pool, cache }
|
||||
}
|
||||
|
||||
pub fn get_connection(&self) -> Result<PooledConnection<SqliteConnectionManager>> {
|
||||
self.pool
|
||||
.get()
|
||||
.map_err(|_e| error::ErrorBadRequest("Cannot get DB connection."))
|
||||
}
|
||||
|
||||
pub fn get_authentication_service(&self) -> Result<SessionService> {
|
||||
Ok(self.cache.session_service.clone())
|
||||
}
|
||||
|
||||
pub fn get_user_service<'a>(
|
||||
&'a self,
|
||||
conn: &'a PooledConnection<SqliteConnectionManager>,
|
||||
) -> Result<UserService> {
|
||||
Ok(UserService::new(conn))
|
||||
}
|
||||
}
|
||||
+7
-9
@@ -6,10 +6,10 @@ use log::info;
|
||||
use minijinja::context;
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
use crate::events::Event;
|
||||
use crate::services::ReadOnlyServiceFactory;
|
||||
use crate::util::SQLiteConnectionFactory;
|
||||
|
||||
|
||||
use crate::webui::services::Services;
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
|
||||
#[actix_web::get("/actions")]
|
||||
@@ -25,11 +25,11 @@ pub async fn accounts(tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Res
|
||||
#[actix_web::get("/overview")]
|
||||
pub async fn overview(
|
||||
tmpl_env: MiniJinjaRenderer,
|
||||
conn_factory: web::Data<SQLiteConnectionFactory>,
|
||||
service_factory: web::Data<Services>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||
let service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||
let user_service = service_factory.get_user_service();
|
||||
let conn = service_factory.get_connection()?;
|
||||
let user_service = service_factory.get_user_service(&conn)?;
|
||||
|
||||
let users = user_service.list_users().unwrap();
|
||||
let ctx = context! {
|
||||
users => users,
|
||||
@@ -39,7 +39,6 @@ pub async fn overview(
|
||||
|
||||
#[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> {
|
||||
@@ -51,7 +50,6 @@ pub async fn overview_submit_button(
|
||||
|
||||
#[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> {
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
use crate::event_processor::process_event;
|
||||
use crate::events::Event;
|
||||
use crate::util::Error::FuckingError;
|
||||
|
||||
Reference in new issue
Block a user