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