Set up a database and serve character sheets from it #277

Merged
savanni merged 7 commits from visions-database into main 2024-11-30 23:56:21 +00:00
4 changed files with 16 additions and 10 deletions
Showing only changes of commit b506d479d3 - Show all commits

2
Cargo.lock generated
View File

@ -4284,6 +4284,8 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
name = "visions" name = "visions"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-std",
"async-trait",
"authdb", "authdb",
"cool_asserts", "cool_asserts",
"futures", "futures",

View File

@ -25,6 +25,7 @@ lazy_static = "1.5.0"
include_dir = "0.7.4" include_dir = "0.7.4"
async-trait = "0.1.83" async-trait = "0.1.83"
futures = "0.3.31" futures = "0.3.31"
async-std = "1.13.0"
[dev-dependencies] [dev-dependencies]
cool_asserts = "2.0.3" cool_asserts = "2.0.3"

View File

@ -7,7 +7,8 @@ tasks:
test: test:
cmds: cmds:
- cargo watch -x 'test -- --nocapture' # - cargo watch -x 'test -- --nocapture'
- cargo watch -x 'nextest run'
dev: dev:
cmds: cmds:

View File

@ -10,7 +10,7 @@ use rusqlite::Connection;
use rusqlite_migration::Migrations; use rusqlite_migration::Migrations;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
use tokio::sync::{mpsc, oneshot}; use async_std::channel::{Sender, Receiver, bounded};
use uuid::Uuid; use uuid::Uuid;
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations"); static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
@ -39,7 +39,7 @@ enum Request {
#[derive(Debug)] #[derive(Debug)]
struct DatabaseRequest { struct DatabaseRequest {
tx: oneshot::Sender<DatabaseResponse>, tx: Sender<DatabaseResponse>,
req: Request, req: Request,
} }
@ -161,16 +161,18 @@ impl DiskDb {
} }
} }
async fn db_handler(db: DiskDb, mut requestor: mpsc::Receiver<DatabaseRequest>) { async fn db_handler(db: DiskDb, mut requestor: Receiver<DatabaseRequest>) {
println!("Starting db_handler"); println!("Starting db_handler");
while let Some(DatabaseRequest{ tx, req }) = requestor.recv().await { while let Ok(DatabaseRequest{ tx, req }) = requestor.recv().await {
println!("Request received: {:?}", req); println!("Request received: {:?}", req);
match req { match req {
Request::Charsheet(id) => { Request::Charsheet(id) => {
let sheet = db.charsheet(id); let sheet = db.charsheet(id);
println!("sheet retrieved: {:?}", sheet); println!("sheet retrieved: {:?}", sheet);
match sheet { match sheet {
Ok(sheet) => tx.send(DatabaseResponse::Charsheet(sheet)).unwrap(), Ok(sheet) => {
tx.send(DatabaseResponse::Charsheet(sheet)).await.unwrap();
},
_ => unimplemented!(), _ => unimplemented!(),
} }
} }
@ -180,7 +182,7 @@ async fn db_handler(db: DiskDb, mut requestor: mpsc::Receiver<DatabaseRequest>)
} }
pub struct DbConn { pub struct DbConn {
conn: mpsc::Sender<DatabaseRequest>, conn: Sender<DatabaseRequest>,
handle: tokio::task::JoinHandle<()>, handle: tokio::task::JoinHandle<()>,
} }
@ -189,7 +191,7 @@ impl DbConn {
where where
P: AsRef<Path>, P: AsRef<Path>,
{ {
let (tx, rx) = mpsc::channel(5); let (tx, rx) = bounded::<DatabaseRequest>(5);
let db = DiskDb::new(path).unwrap(); let db = DiskDb::new(path).unwrap();
let handle = tokio::spawn(async move { db_handler(db, rx).await; }); let handle = tokio::spawn(async move { db_handler(db, rx).await; });
@ -204,14 +206,14 @@ impl Database for DbConn {
&mut self, &mut self,
id: CharacterId, id: CharacterId,
) -> Result<Option<CharsheetRow>, Error> { ) -> Result<Option<CharsheetRow>, Error> {
let (tx, rx) = oneshot::channel::<DatabaseResponse>(); let (tx, rx) = bounded::<DatabaseResponse>(1);
let request = DatabaseRequest{ let request = DatabaseRequest{
tx, tx,
req: Request::Charsheet(id), req: Request::Charsheet(id),
}; };
self.conn.send(request).await.unwrap(); self.conn.send(request).await.unwrap();
match rx.await { match rx.recv().await {
Ok(DatabaseResponse::Charsheet(row)) => Ok(row), Ok(DatabaseResponse::Charsheet(row)) => Ok(row),
Ok(_) => Err(Error::MessageMismatch), Ok(_) => Err(Error::MessageMismatch),
Err(err) => { Err(err) => {