Massive refactoring
This commit is contained in:
parent
31dc4fd4a3
commit
a75c115761
31 changed files with 160 additions and 140 deletions
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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<MessageId>,
|
||||
}
|
||||
|
||||
pub struct Ubisync {
|
||||
comm_handle: Arc<CommHandle>,
|
||||
state_handle: Arc<State>,
|
||||
api: Arc<Api>,
|
||||
}
|
||||
|
||||
impl Ubisync {
|
||||
pub async fn new(config: &Config) -> anyhow::Result<Self> {
|
||||
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<Api> {
|
||||
self.api.clone()
|
||||
}
|
||||
|
||||
pub fn add_peer(&self, p: impl TryInto<Peer, Error = anyhow::Error>) -> 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<Peer> {
|
||||
self.state_handle.get_peers().unwrap_or(vec![])
|
||||
}
|
||||
|
||||
pub fn get_destination(&self) -> anyhow::Result<I2pSocketAddr> {
|
||||
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;
|
||||
|
|
|
@ -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<MessageId>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum MessageContent {
|
||||
Hello {
|
|
@ -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 {
|
|
@ -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" ] }
|
|
@ -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<ElementId>, s: Extension<Arc<ApiState>>) -> Response {
|
||||
let element = s.get_element(&id);
|
|
@ -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);
|
|
@ -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<CommState>,
|
||||
|
@ -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
|
||||
)),
|
67
ubisync/src/lib.rs
Normal file
67
ubisync/src/lib.rs
Normal file
|
@ -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<CommHandle>,
|
||||
state_handle: Arc<State>,
|
||||
api: Arc<Api>,
|
||||
}
|
||||
|
||||
impl Ubisync {
|
||||
pub async fn new(config: &Config) -> anyhow::Result<Self> {
|
||||
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<Api> {
|
||||
self.api.clone()
|
||||
}
|
||||
|
||||
pub fn add_peer(&self, p: impl TryInto<Peer, Error = anyhow::Error>) -> 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<Peer> {
|
||||
self.state_handle.get_peers().unwrap_or(vec![])
|
||||
}
|
||||
|
||||
pub fn get_destination(&self) -> anyhow::Result<I2pSocketAddr> {
|
||||
self.comm_handle.i2p_address()
|
||||
}
|
||||
}
|
|
@ -1,5 +1,9 @@
|
|||
use ubisync::{Ubisync, config::Config};
|
||||
|
||||
|
||||
pub fn main() {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let _node = Ubisync::new(&Config::default()).await.unwrap();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<State>,
|
||||
|
@ -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]
|
|
@ -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]
|
|
@ -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,
|
|
@ -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,
|
|
@ -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![
|
|
@ -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() {
|
Loading…
Add table
Add a link
Reference in a new issue