Started to use db pool.
This commit is contained in:
1 parent
eebe848e4b
commit
5df6d5afd1
6 files changed
+66
-31
No files matched your search
@@ -87,4 +87,11 @@ impl SQLiteConnectionFactory {
|
||||
SQLiteConnectionCreator::Ephemeral(c) => c.readonly(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn path(&self) -> Option<PathBuf> {
|
||||
match &self.creator {
|
||||
SQLiteConnectionCreator::Path(c) => Some(c.path.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,10 @@
|
||||
use actix_web::{web, HttpRequest, Responder};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
use crate::services::ReadOnlyServiceFactory;
|
||||
use crate::webui::templates::MiniJinjaRenderer;
|
||||
use crate::SQLiteConnectionFactory;
|
||||
use minijinja::context;
|
||||
use serde::Deserialize;
|
||||
use crate::webui::services::{DbPool, Services};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct LoginPayload {
|
||||
@@ -54,13 +49,10 @@ pub async fn logout(
|
||||
#[actix_web::get("/")]
|
||||
pub async fn index(
|
||||
req: HttpRequest,
|
||||
conn_factory: web::Data<SQLiteConnectionFactory>,
|
||||
pool: web::Data<DbPool>,
|
||||
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 = Services::get_authentication_service(& pool).unwrap();
|
||||
let userid = authentication_service.get_login(&req);
|
||||
let username = userid.clone().unwrap_or("".to_string());
|
||||
let ctx = context! {
|
||||
|
||||
+7
-1
@@ -12,10 +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::templates::MiniJinjaRenderer;
|
||||
use crate::webui::worker::event_processor_loop;
|
||||
|
||||
@@ -61,6 +62,8 @@ fn create_views_scope() -> Scope {
|
||||
.service(views::actions_partials)
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn web_ui_main(
|
||||
conn_fact: SQLiteConnectionFactory,
|
||||
@@ -73,10 +76,13 @@ pub async fn web_ui_main(
|
||||
// https://github.com/ivanceras/r2d2-sqlite) use
|
||||
// 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 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(template_reloader))
|
||||
.app_data(web::Data::new(event_sender.clone()))
|
||||
.app_data(web::Data::new(cloned_conn_fact.clone()))
|
||||
|
||||
+15
-19
@@ -1,31 +1,27 @@
|
||||
use std::path::PathBuf;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use crate::services::{SessionService};
|
||||
use crate::util::{SQLiteConnectionFactory};
|
||||
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
||||
|
||||
pub type DbPool = r2d2::Pool<SqliteConnectionManager>;
|
||||
|
||||
pub fn creat_db_pool(path: & PathBuf) -> DbPool {
|
||||
let manager = SqliteConnectionManager::file(path);
|
||||
r2d2::Pool::new(manager).unwrap()
|
||||
}
|
||||
|
||||
struct ServicesCache {
|
||||
session_service: SessionService,
|
||||
}
|
||||
|
||||
struct Services {
|
||||
conn_fact: SQLiteConnectionFactory,
|
||||
cache: ServicesCache,
|
||||
}
|
||||
pub struct Services {}
|
||||
|
||||
impl Services {
|
||||
pub fn new(conn_fact: SQLiteConnectionFactory) -> Services {
|
||||
let mut tmp_conn = conn_fact.connection().unwrap();
|
||||
let session_service = SessionService::from_conn(&mut tmp_conn).unwrap();
|
||||
let cache = ServicesCache { session_service };
|
||||
Services { conn_fact, cache }
|
||||
|
||||
pub fn get_authentication_service(dbPool: & DbPool) -> CustomResult<SessionService> {
|
||||
let mut conn = dbPool.get().unwrap();
|
||||
SessionService::from_conn(&mut conn)
|
||||
}
|
||||
|
||||
pub fn get_authentication_service(&self) -> &SessionService {
|
||||
&self.cache.session_service
|
||||
}
|
||||
|
||||
pub fn get_user_service(&self) -> i32 {
|
||||
// let mut conn = self.conn_fact.readonly().unwrap(); // todo; to proper 500
|
||||
// let service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||
// service_factory.get_user_service()
|
||||
88
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user