diff --git a/README.md b/README.md index 0194ec8..131f13f 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,6 @@ Yes, another rewrite was needed. Again. - STRETCH: Contacts support. Same notes as caldav support. -## Known bugs -- Offline edits in the canvas (excalidraw) note type cause all state to be lost. This might be even more severe, not requiring offline edits. - - ## Ideas - Maybe rename Todos to Tasks, since they also contain doing, done, deadline, and idea types. Plus it's just a nicer name. diff --git a/backend/src/main.rs b/backend/src/main.rs index cb05424..479d7d1 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -20,6 +20,7 @@ use openidconnect::{ use rand::{distr::Alphanumeric, rng, Rng}; use rocksdb::TransactionDB; use sha2::Sha256; +use std::fs; use std::{ char, collections::HashMap, @@ -96,37 +97,37 @@ impl Reject for AuthError {} struct Connection { bcast: BroadcastGroup, - // This is purely here to keep the connection to the DB alive while there is at least one - // connection. + db: Arc, #[allow(dyn_drop)] - _db_subscription: Arc, + _db_sub: Arc, } struct Server { // There is something to be said for keeping the broadcast group in memory for a bit after all // clients disconnect, but for now we don't bother. pub open_docs: RwLock>>, - pub db: Arc, pub http_client: reqwest::Client, pub openid_client: OpenidClient, pub signing_key: Hmac, + + pub data_dir: PathBuf, } impl Server { pub fn new( - db: TransactionDB, http_client: reqwest::Client, openid_client: OpenidClient, signing_key: Hmac, + data_dir: PathBuf, ) -> Self { Self { open_docs: RwLock::default(), - db: Arc::new(db), http_client, openid_client, signing_key, + data_dir, } } pub async fn get_or_create_doc(&self, name: String) -> Arc { @@ -138,10 +139,17 @@ impl Server { let mut open_docs = self.open_docs.write().await; let doc = Doc::new(); + let data_dir = self.data_dir.join(name.clone()); + if let Err(e) = fs::create_dir_all(&data_dir) { + panic!("Was unable to create data directory for note {}, due to the following error. Something is very wrong!\n{}", name, e) + } + + let db = + Arc::new(TransactionDB::open_default(data_dir).expect("Failed to open DB")); // Subscribe the DB to updates let sub = { - let db = self.db.clone(); + let db = db.clone(); let name = name.clone(); doc.observe_update_v1(move |_, e| { let txn = RocksDBStore::from(db.transaction()); @@ -157,7 +165,7 @@ impl Server { { // Load document from DB let mut txn = doc.transact_mut(); - let db_txn = RocksDBStore::from(self.db.transaction()); + let db_txn = RocksDBStore::from(db.transaction()); db_txn.load_doc(&name, &mut txn).unwrap(); } @@ -165,7 +173,8 @@ impl Server { let group = BroadcastGroup::new(awareness, 32).await; let connection = Arc::new(Connection { bcast: group, - _db_subscription: sub, + db, + _db_sub: sub, }); open_docs.insert(name, Arc::downgrade(&connection)); @@ -225,8 +234,12 @@ async fn main() { ) .await; - let db = TransactionDB::open_default(&data_dir).expect("Failed to open DB"); - let server = Arc::new(Server::new(db, http_client, openid_client, signing_key)); + let server = Arc::new(Server::new( + http_client, + openid_client, + signing_key, + data_dir, + )); let ws = { let server = server.clone();