From a75c115761b0a009a8386f5df275be0b6f15bb2e Mon Sep 17 00:00:00 2001 From: "Philip (a-0)" <@ph:a-0.me> Date: Fri, 5 Jan 2024 21:34:37 +0100 Subject: [PATCH] Massive refactoring --- Cargo.lock | 19 +++- ubisync-lib/Cargo.toml | 1 - ubisync-lib/src/lib.rs | 87 +------------------ ubisync-lib/src/{comm => }/messages/mod.rs | 9 +- ubisync-lib/src/{comm/types.rs => peer.rs} | 2 +- ubisync-lib/src/{state => }/types/element.rs | 0 .../src/{state => }/types/element_content.rs | 0 .../src/{state => }/types/element_id.rs | 0 .../src/{state => }/types/message_id.rs | 0 ubisync-lib/src/{state => }/types/mod.rs | 0 ubisync-lib/src/{state => }/types/peer_id.rs | 0 ubisync-lib/src/{state => }/types/tag.rs | 0 ubisync/Cargo.toml | 20 +++++ {ubisync-lib => ubisync}/src/api/mod.rs | 0 {ubisync-lib => ubisync}/src/api/v0/app.rs | 0 .../src/api/v0/element.rs | 6 +- {ubisync-lib => ubisync}/src/api/v0/mod.rs | 0 .../src/comm/message_processor.rs | 6 +- {ubisync-lib => ubisync}/src/comm/mod.rs | 13 ++- {ubisync-lib => ubisync}/src/config.rs | 0 ubisync/src/lib.rs | 67 ++++++++++++++ ubisync/src/main.rs | 10 ++- .../src/state/api_state.rs | 17 ++-- .../src/state/comm_state.rs | 17 ++-- {ubisync-lib => ubisync}/src/state/mod.rs | 11 +-- .../src/state/queries/apps.rs | 0 .../src/state/queries/elements.rs | 3 +- .../src/state/queries/mod.rs | 0 .../src/state/queries/peers.rs | 3 +- {ubisync-lib => ubisync}/src/state/schema.rs | 0 {ubisync-lib => ubisync}/tests/api.rs | 9 +- 31 files changed, 160 insertions(+), 140 deletions(-) rename ubisync-lib/src/{comm => }/messages/mod.rs (86%) rename ubisync-lib/src/{comm/types.rs => peer.rs} (96%) rename ubisync-lib/src/{state => }/types/element.rs (100%) rename ubisync-lib/src/{state => }/types/element_content.rs (100%) rename ubisync-lib/src/{state => }/types/element_id.rs (100%) rename ubisync-lib/src/{state => }/types/message_id.rs (100%) rename ubisync-lib/src/{state => }/types/mod.rs (100%) rename ubisync-lib/src/{state => }/types/peer_id.rs (100%) rename ubisync-lib/src/{state => }/types/tag.rs (100%) rename {ubisync-lib => ubisync}/src/api/mod.rs (100%) rename {ubisync-lib => ubisync}/src/api/v0/app.rs (100%) rename {ubisync-lib => ubisync}/src/api/v0/element.rs (95%) rename {ubisync-lib => ubisync}/src/api/v0/mod.rs (100%) rename {ubisync-lib => ubisync}/src/comm/message_processor.rs (87%) rename {ubisync-lib => ubisync}/src/comm/mod.rs (97%) rename {ubisync-lib => ubisync}/src/config.rs (100%) create mode 100644 ubisync/src/lib.rs rename {ubisync-lib => ubisync}/src/state/api_state.rs (94%) rename {ubisync-lib => ubisync}/src/state/comm_state.rs (93%) rename {ubisync-lib => ubisync}/src/state/mod.rs (94%) rename {ubisync-lib => ubisync}/src/state/queries/apps.rs (100%) rename {ubisync-lib => ubisync}/src/state/queries/elements.rs (99%) rename {ubisync-lib => ubisync}/src/state/queries/mod.rs (100%) rename {ubisync-lib => ubisync}/src/state/queries/peers.rs (95%) rename {ubisync-lib => ubisync}/src/state/schema.rs (100%) rename {ubisync-lib => ubisync}/tests/api.rs (94%) diff --git a/Cargo.lock b/Cargo.lock index b5bb387..5044cbb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3044,6 +3044,24 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "ubisync" version = "0.1.0" +dependencies = [ + "anyhow", + "axum", + "chrono", + "cozo", + "i2p", + "itertools", + "jsonwebtoken", + "reqwest", + "serde", + "serde_json", + "serial_test", + "tokio", + "tracing", + "tracing-subscriber", + "ubisync-lib", + "uuid", +] [[package]] name = "ubisync-lib" @@ -3061,7 +3079,6 @@ dependencies = [ "serde", "serde_json", "serde_with", - "serial_test", "tokio", "tracing", "tracing-subscriber", diff --git a/ubisync-lib/Cargo.toml b/ubisync-lib/Cargo.toml index 0bbe303..2852b3d 100644 --- a/ubisync-lib/Cargo.toml +++ b/ubisync-lib/Cargo.toml @@ -13,7 +13,6 @@ jsonwebtoken = "9.2.0" serde = { version = "1.0.166", features = [ "derive" ] } serde_json = "1.0.99" serde_with = "3.3.0" -serial_test = "2.0.0" tokio = { version = "1.32.0", features = ["full"] } tracing = "0.1.37" tracing-subscriber = "0.3.17" diff --git a/ubisync-lib/src/lib.rs b/ubisync-lib/src/lib.rs index 3520438..8ed7f46 100644 --- a/ubisync-lib/src/lib.rs +++ b/ubisync-lib/src/lib.rs @@ -1,84 +1,3 @@ -use std::sync::Arc; - -use anyhow::bail; -use api::{Api, ApiBuilder}; -use comm::{CommHandle, Peer}; -use config::Config; -use i2p::net::I2pSocketAddr; -use serde::{Deserialize, Serialize}; -use state::{ - types::{MessageId, PeerId}, - ApiState, CommState, State, -}; - -pub mod api; -pub mod comm; -pub mod config; -pub mod state; - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct MessageRelations { - pub parents: Vec, -} - -pub struct Ubisync { - comm_handle: Arc, - state_handle: Arc, - api: Arc, -} - -impl Ubisync { - pub async fn new(config: &Config) -> anyhow::Result { - let state = State::new().await?; - let comm_handle = Arc::new(CommHandle::new(CommState::new(state.clone()), config)?); - state.set_comm_handle(comm_handle.clone()); - - let api = Arc::new( - ApiBuilder::from(config.api_config.clone()) - .build(ApiState::new(state.clone(), &config.jwt_secret)) - .await, - ); - - comm_handle.run().await; - Ok(Ubisync { - comm_handle: comm_handle, - state_handle: state, - api: api, - }) - } - - pub fn api(&self) -> Arc { - self.api.clone() - } - - pub fn add_peer(&self, p: impl TryInto) -> anyhow::Result<()> { - match p.try_into() { - Ok(peer) => self.state_handle.set_peer(&peer), - Err(e) => bail!(e), - } - } - - pub fn add_peer_from_id(&self, id: PeerId) -> anyhow::Result<()> { - // TODO: resolve peer's name before setting - self.state_handle.set_peer(&Peer::new(id, "".to_string())) - } - - pub fn get_peers(&self) -> Vec { - self.state_handle.get_peers().unwrap_or(vec![]) - } - - pub fn get_destination(&self) -> anyhow::Result { - self.comm_handle.i2p_address() - } -} - -#[cfg(test)] -mod tests { - #[test] - #[ignore] - fn full_system_test1() { - // Do a test requiring a specific system state (including homeserver state or such) - // Disabled by default, since files need to be set up explicitly for this test - todo!() - } -} +pub mod messages; +pub mod types; +pub mod peer; diff --git a/ubisync-lib/src/comm/messages/mod.rs b/ubisync-lib/src/messages/mod.rs similarity index 86% rename from ubisync-lib/src/comm/messages/mod.rs rename to ubisync-lib/src/messages/mod.rs index aa0625d..4f0240b 100644 --- a/ubisync-lib/src/comm/messages/mod.rs +++ b/ubisync-lib/src/messages/mod.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::state::types::{ElementContent, ElementId, MessageId}; +use crate::types::{MessageId, ElementId, ElementContent}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Message { @@ -9,6 +9,13 @@ pub struct Message { content: MessageContent, } + + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct MessageRelations { + pub parents: Vec, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub enum MessageContent { Hello { diff --git a/ubisync-lib/src/comm/types.rs b/ubisync-lib/src/peer.rs similarity index 96% rename from ubisync-lib/src/comm/types.rs rename to ubisync-lib/src/peer.rs index 204129f..a1b9711 100644 --- a/ubisync-lib/src/comm/types.rs +++ b/ubisync-lib/src/peer.rs @@ -3,7 +3,7 @@ use i2p::net::I2pSocketAddr; use serde::{Deserialize, Serialize}; -use crate::state::types::PeerId; +use crate::types::PeerId; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Peer { diff --git a/ubisync-lib/src/state/types/element.rs b/ubisync-lib/src/types/element.rs similarity index 100% rename from ubisync-lib/src/state/types/element.rs rename to ubisync-lib/src/types/element.rs diff --git a/ubisync-lib/src/state/types/element_content.rs b/ubisync-lib/src/types/element_content.rs similarity index 100% rename from ubisync-lib/src/state/types/element_content.rs rename to ubisync-lib/src/types/element_content.rs diff --git a/ubisync-lib/src/state/types/element_id.rs b/ubisync-lib/src/types/element_id.rs similarity index 100% rename from ubisync-lib/src/state/types/element_id.rs rename to ubisync-lib/src/types/element_id.rs diff --git a/ubisync-lib/src/state/types/message_id.rs b/ubisync-lib/src/types/message_id.rs similarity index 100% rename from ubisync-lib/src/state/types/message_id.rs rename to ubisync-lib/src/types/message_id.rs diff --git a/ubisync-lib/src/state/types/mod.rs b/ubisync-lib/src/types/mod.rs similarity index 100% rename from ubisync-lib/src/state/types/mod.rs rename to ubisync-lib/src/types/mod.rs diff --git a/ubisync-lib/src/state/types/peer_id.rs b/ubisync-lib/src/types/peer_id.rs similarity index 100% rename from ubisync-lib/src/state/types/peer_id.rs rename to ubisync-lib/src/types/peer_id.rs diff --git a/ubisync-lib/src/state/types/tag.rs b/ubisync-lib/src/types/tag.rs similarity index 100% rename from ubisync-lib/src/state/types/tag.rs rename to ubisync-lib/src/types/tag.rs diff --git a/ubisync/Cargo.toml b/ubisync/Cargo.toml index 7d8f32c..e447b1c 100644 --- a/ubisync/Cargo.toml +++ b/ubisync/Cargo.toml @@ -6,3 +6,23 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.71" +axum = { version = "0.7.2", features = [ "macros" ] } +chrono = "0.4.31" +itertools = "0.12.0" +cozo = { version = "0.7.5", features = [ "storage-rocksdb", "requests", "graph-algo" ] } +jsonwebtoken = "9.2.0" +serde = { version = "1.0.166", features = [ "derive" ] } +serde_json = "1.0.99" +serial_test = "2.0.0" +tokio = { version = "1.32.0", features = ["full"] } +tracing = "0.1.37" +tracing-subscriber = "0.3.17" +uuid = "1.4.1" + +i2p = { path = "../../i2p-rs" } +ubisync-lib = { path = "../ubisync-lib" } + + +[dev-dependencies] +reqwest = { version = "0.11.20", features = [ "json" ] } \ No newline at end of file diff --git a/ubisync-lib/src/api/mod.rs b/ubisync/src/api/mod.rs similarity index 100% rename from ubisync-lib/src/api/mod.rs rename to ubisync/src/api/mod.rs diff --git a/ubisync-lib/src/api/v0/app.rs b/ubisync/src/api/v0/app.rs similarity index 100% rename from ubisync-lib/src/api/v0/app.rs rename to ubisync/src/api/v0/app.rs diff --git a/ubisync-lib/src/api/v0/element.rs b/ubisync/src/api/v0/element.rs similarity index 95% rename from ubisync-lib/src/api/v0/element.rs rename to ubisync/src/api/v0/element.rs index 8f42322..04905e9 100644 --- a/ubisync-lib/src/api/v0/element.rs +++ b/ubisync/src/api/v0/element.rs @@ -8,10 +8,8 @@ use axum::{ }; use tracing::{debug, warn}; -use crate::state::{ - types::{ElementContent, ElementId}, - ApiState, -}; +use crate::state::ApiState; +use ubisync_lib::types::{ElementContent, ElementId}; pub(super) async fn get(Path(id): Path, s: Extension>) -> Response { let element = s.get_element(&id); diff --git a/ubisync-lib/src/api/v0/mod.rs b/ubisync/src/api/v0/mod.rs similarity index 100% rename from ubisync-lib/src/api/v0/mod.rs rename to ubisync/src/api/v0/mod.rs diff --git a/ubisync-lib/src/comm/message_processor.rs b/ubisync/src/comm/message_processor.rs similarity index 87% rename from ubisync-lib/src/comm/message_processor.rs rename to ubisync/src/comm/message_processor.rs index 3d82883..6ef2282 100644 --- a/ubisync-lib/src/comm/message_processor.rs +++ b/ubisync/src/comm/message_processor.rs @@ -1,8 +1,10 @@ use tracing::debug; -use crate::state::{types::PeerId, CommState}; +use ubisync_lib::types::PeerId; -use super::messages::{Message, MessageContent}; +use ubisync_lib::messages::{Message, MessageContent}; + +use crate::state::CommState; pub fn handle(state: &CommState, peer: &PeerId, message: Message) { debug!("Handling message now: {:?}", message); diff --git a/ubisync-lib/src/comm/mod.rs b/ubisync/src/comm/mod.rs similarity index 97% rename from ubisync-lib/src/comm/mod.rs rename to ubisync/src/comm/mod.rs index 9301dee..5b668f4 100644 --- a/ubisync-lib/src/comm/mod.rs +++ b/ubisync/src/comm/mod.rs @@ -1,8 +1,7 @@ pub mod message_processor; -pub mod messages; -mod types; use tracing::{debug, error, warn}; -pub use types::*; +use ubisync_lib::messages::Message; +use ubisync_lib::types::PeerId; use std::collections::HashMap; use std::io::{Read, Write}; @@ -15,11 +14,9 @@ use i2p::sam_options::SAMOptions; use tokio::sync::RwLock; use tokio::task::JoinHandle; -use crate::state::types::PeerId; use crate::state::CommState; use crate::Config; -use self::messages::Message; pub struct CommHandle { state: Arc, @@ -206,9 +203,9 @@ mod tests { use i2p::sam::StreamForward; use i2p::sam_options::SAMOptions; use i2p::Session; + use ubisync_lib::messages::{Message, self}; + use ubisync_lib::types::ElementId; - use crate::comm::{messages, Message}; - use crate::state::types::ElementId; use crate::state::{CommState, State}; use crate::Config; @@ -237,7 +234,7 @@ mod tests { &ch.i2p_address().unwrap(), Message::new(messages::MessageContent::CreateElement { id: ElementId::new(), - content: crate::state::types::ElementContent::Text(format!( + content: ubisync_lib::types::ElementContent::Text(format!( "hello world no. {}", i )), diff --git a/ubisync-lib/src/config.rs b/ubisync/src/config.rs similarity index 100% rename from ubisync-lib/src/config.rs rename to ubisync/src/config.rs diff --git a/ubisync/src/lib.rs b/ubisync/src/lib.rs new file mode 100644 index 0000000..2e18213 --- /dev/null +++ b/ubisync/src/lib.rs @@ -0,0 +1,67 @@ +use std::sync::Arc; + +use anyhow::bail; +use api::{Api, ApiBuilder}; +use comm::CommHandle; +use config::Config; +use i2p::net::I2pSocketAddr; +use state::{State, CommState, ApiState}; +use ubisync_lib::{peer::Peer, types::PeerId}; + + + +pub mod api; +pub mod comm; +pub mod config; +pub mod state; + +pub struct Ubisync { + comm_handle: Arc, + state_handle: Arc, + api: Arc, +} + +impl Ubisync { + pub async fn new(config: &Config) -> anyhow::Result { + let state = State::new().await?; + let comm_handle = Arc::new(CommHandle::new(CommState::new(state.clone()), config)?); + state.set_comm_handle(comm_handle.clone()); + + let api = Arc::new( + ApiBuilder::from(config.api_config.clone()) + .build(ApiState::new(state.clone(), &config.jwt_secret)) + .await, + ); + + comm_handle.run().await; + Ok(Ubisync { + comm_handle: comm_handle, + state_handle: state, + api: api, + }) + } + + pub fn api(&self) -> Arc { + self.api.clone() + } + + pub fn add_peer(&self, p: impl TryInto) -> anyhow::Result<()> { + match p.try_into() { + Ok(peer) => self.state_handle.set_peer(&peer), + Err(e) => bail!(e), + } + } + + pub fn add_peer_from_id(&self, id: PeerId) -> anyhow::Result<()> { + // TODO: resolve peer's name before setting + self.state_handle.set_peer(&Peer::new(id, "".to_string())) + } + + pub fn get_peers(&self) -> Vec { + self.state_handle.get_peers().unwrap_or(vec![]) + } + + pub fn get_destination(&self) -> anyhow::Result { + self.comm_handle.i2p_address() + } +} \ No newline at end of file diff --git a/ubisync/src/main.rs b/ubisync/src/main.rs index 3e9b348..806a05d 100644 --- a/ubisync/src/main.rs +++ b/ubisync/src/main.rs @@ -1,5 +1,9 @@ +use ubisync::{Ubisync, config::Config}; -pub fn main() { - -} \ No newline at end of file + +#[tokio::main] +async fn main() { + let _node = Ubisync::new(&Config::default()).await.unwrap(); +} + diff --git a/ubisync-lib/src/state/api_state.rs b/ubisync/src/state/api_state.rs similarity index 94% rename from ubisync-lib/src/state/api_state.rs rename to ubisync/src/state/api_state.rs index 0ad06b1..5dbaac9 100644 --- a/ubisync-lib/src/state/api_state.rs +++ b/ubisync/src/state/api_state.rs @@ -4,17 +4,11 @@ use chrono::Utc; use cozo::DbInstance; use jsonwebtoken::{DecodingKey, EncodingKey, Validation}; use tracing::debug; +use ubisync_lib::{types::{ElementContent, ElementId, Element}, messages::MessageContent}; -use crate::{ - api::v0::app::{AppDescription, AppId}, - comm::messages::MessageContent, - state::{queries, types::ElementId}, -}; +use crate::{api::v0::app::{AppDescription, AppId}, state::queries}; -use super::{ - types::{Element, ElementContent}, - State, -}; +use super::State; pub struct ApiState { state: Arc, @@ -108,8 +102,11 @@ impl ApiState { #[cfg(test)] mod tests { + use ubisync_lib::types::ElementContent; + + use crate::state::State; + use super::ApiState; - use crate::state::{types::ElementContent, State}; #[tokio::test] #[serial_test::serial] diff --git a/ubisync-lib/src/state/comm_state.rs b/ubisync/src/state/comm_state.rs similarity index 93% rename from ubisync-lib/src/state/comm_state.rs rename to ubisync/src/state/comm_state.rs index 227fa24..f91c436 100644 --- a/ubisync-lib/src/state/comm_state.rs +++ b/ubisync/src/state/comm_state.rs @@ -3,12 +3,11 @@ use std::sync::Arc; use cozo::DbInstance; use tracing::debug; -use crate::{comm::Peer, state::queries}; +use ubisync_lib::{types::{Element, ElementContent, ElementId, MessageId, PeerId}, peer::Peer}; -use super::{ - types::{Element, ElementContent, ElementId, MessageId, PeerId}, - State, -}; +use crate::state::queries; + +use super::State; //TODO: Notify API about changes pub struct CommState { @@ -81,11 +80,11 @@ impl CommState { #[cfg(test)] mod tests { + use crate::state::State; + use super::CommState; - use crate::state::{ - types::{ElementContent, ElementId, MessageId}, - State, - }; + + use ubisync_lib::types::{ElementContent, ElementId, MessageId}; #[tokio::test] #[serial_test::serial] diff --git a/ubisync-lib/src/state/mod.rs b/ubisync/src/state/mod.rs similarity index 94% rename from ubisync-lib/src/state/mod.rs rename to ubisync/src/state/mod.rs index bd3231f..a31428b 100644 --- a/ubisync-lib/src/state/mod.rs +++ b/ubisync/src/state/mod.rs @@ -4,14 +4,6 @@ use anyhow::Error; use cozo::DbInstance; use tracing::{debug, error}; -use crate::comm::{ - messages::{Message, MessageContent}, - CommHandle, Peer, -}; - -use self::types::{Element, ElementContent, ElementId, Tag}; - -pub mod types; mod api_state; mod comm_state; @@ -20,6 +12,9 @@ mod schema; pub use api_state::ApiState; pub use comm_state::CommState; +use ubisync_lib::{types::{ElementId, ElementContent, Element, Tag}, messages::{Message, MessageContent}, peer::Peer}; + +use crate::comm::CommHandle; pub struct State { db: DbInstance, diff --git a/ubisync-lib/src/state/queries/apps.rs b/ubisync/src/state/queries/apps.rs similarity index 100% rename from ubisync-lib/src/state/queries/apps.rs rename to ubisync/src/state/queries/apps.rs diff --git a/ubisync-lib/src/state/queries/elements.rs b/ubisync/src/state/queries/elements.rs similarity index 99% rename from ubisync-lib/src/state/queries/elements.rs rename to ubisync/src/state/queries/elements.rs index 7323ee7..5e1f719 100644 --- a/ubisync-lib/src/state/queries/elements.rs +++ b/ubisync/src/state/queries/elements.rs @@ -8,11 +8,12 @@ use tracing::{debug, error}; use crate::{ run_query, state::{ - types::{MessageId, Tag}, Element, ElementContent, ElementId, }, }; +use ubisync_lib::types::{MessageId, Tag}; + pub fn add( db: &DbInstance, id: &ElementId, diff --git a/ubisync-lib/src/state/queries/mod.rs b/ubisync/src/state/queries/mod.rs similarity index 100% rename from ubisync-lib/src/state/queries/mod.rs rename to ubisync/src/state/queries/mod.rs diff --git a/ubisync-lib/src/state/queries/peers.rs b/ubisync/src/state/queries/peers.rs similarity index 95% rename from ubisync-lib/src/state/queries/peers.rs rename to ubisync/src/state/queries/peers.rs index 2499a44..1230cb0 100644 --- a/ubisync-lib/src/state/queries/peers.rs +++ b/ubisync/src/state/queries/peers.rs @@ -1,7 +1,8 @@ use anyhow::Error; use cozo::{DataValue, DbInstance, ScriptMutability}; +use ubisync_lib::{types::PeerId, peer::Peer}; -use crate::{comm::Peer, run_query, state::types::PeerId}; +use crate::run_query; pub fn put(db: &DbInstance, id: &PeerId, name: &str) -> anyhow::Result<()> { let params = vec![ diff --git a/ubisync-lib/src/state/schema.rs b/ubisync/src/state/schema.rs similarity index 100% rename from ubisync-lib/src/state/schema.rs rename to ubisync/src/state/schema.rs diff --git a/ubisync-lib/tests/api.rs b/ubisync/tests/api.rs similarity index 94% rename from ubisync-lib/tests/api.rs rename to ubisync/tests/api.rs index c51a6c1..765930c 100644 --- a/ubisync-lib/tests/api.rs +++ b/ubisync/tests/api.rs @@ -1,12 +1,9 @@ use std::time::Duration; use tracing::{debug, Level}; -use ubisync_lib::{ - api::v0::app::AppDescription, - config::Config, - state::types::{Element, ElementContent, ElementId}, - Ubisync, -}; +use ubisync::{config::Config, Ubisync, api::v0::app::AppDescription}; +use ubisync_lib::types::{ElementContent, ElementId, Element}; + #[tokio::test(flavor = "multi_thread")] async fn two_nodes_element_creation() {