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
Generated
+32
@@ -754,6 +754,8 @@ dependencies = [
|
||||
"log",
|
||||
"minijinja",
|
||||
"minijinja-autoreload",
|
||||
"r2d2",
|
||||
"r2d2_sqlite",
|
||||
"regex",
|
||||
"resource",
|
||||
"rusqlite",
|
||||
@@ -1622,6 +1624,27 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r2d2"
|
||||
version = "0.8.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51de85fb3fb6524929c8a2eb85e6b6d363de4e8c48f9e2c2eac4944abc181c93"
|
||||
dependencies = [
|
||||
"log",
|
||||
"parking_lot",
|
||||
"scheduled-thread-pool",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r2d2_sqlite"
|
||||
version = "0.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4f5d0337e99cd5cacd91ffc326c6cc9d8078def459df560c4f9bf9ba4a51034"
|
||||
dependencies = [
|
||||
"r2d2",
|
||||
"rusqlite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
@@ -1749,6 +1772,15 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scheduled-thread-pool"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19"
|
||||
dependencies = [
|
||||
"parking_lot",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
|
||||
@@ -31,6 +31,8 @@ futures = "0.3.26"
|
||||
actix-files = "0.6.2"
|
||||
async-channel = "1.8.0"
|
||||
base64 = "0.21.0"
|
||||
r2d2_sqlite = "0.21.0"
|
||||
r2d2 = "0.8.10"
|
||||
|
||||
[dependencies.apache-avro]
|
||||
version = "0.14.0"
|
||||
|
||||
@@ -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