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"
version = "0.1.0"
dependencies = [
"async-std",
"async-trait",
"authdb",
"cool_asserts",
"futures",

View File

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

View File

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

View File

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