Compare commits
37 commits
feature/di
...
main
Author | SHA1 | Date | |
---|---|---|---|
64aa5f2353 | |||
4561cc66f9 | |||
ff5256eadd | |||
347ef56d56 | |||
1d6ccc5d1b | |||
6741c2e0a7 | |||
0fce680e1e | |||
ab05bd10cd | |||
f495af68fc | |||
be896aaae3 | |||
5120005128 | |||
2cb1f50f57 | |||
e24ff770a5 | |||
18e6d9239b | |||
8f04832397 | |||
29ddd61eec | |||
69e047cf5a | |||
f7e67759de | |||
c39a28388d | |||
25ea7ed652 | |||
9c960a9d59 | |||
1adebe17e4 | |||
8b3aebb4e9 | |||
387c043367 | |||
2b70b130f2 | |||
72c9710283 | |||
0e1227da85 | |||
ebbe3d82d6 | |||
8fbf8a16ec | |||
3356684530 | |||
3a2f53dd46 | |||
eb428eb537 | |||
a80b9bcdf1 | |||
66be78dabf | |||
1854e84949 | |||
1a5ea87780 | |||
70107257c2 |
133 changed files with 3655 additions and 1172 deletions
50
Cargo.toml
50
Cargo.toml
|
@ -1,5 +1,32 @@
|
|||
[package]
|
||||
name = "caretta-sync"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[features]
|
||||
default = ["macros"]
|
||||
bevy = ["dep:caretta-sync-bevy"]
|
||||
mobile = ["dep:caretta-sync-mobile"]
|
||||
cli = ["dep:caretta-sync-cli"]
|
||||
desktop = ["cli", "bevy"]
|
||||
macros = ["dep:caretta-sync-macros"]
|
||||
test = ["caretta-sync-core/test"]
|
||||
|
||||
[dependencies]
|
||||
caretta-sync-bevy = { path = "bevy", optional = true }
|
||||
caretta-sync-core.workspace = true
|
||||
caretta-sync-cli = { path="cli", optional = true }
|
||||
caretta-sync-mobile = { path = "mobile", optional = true }
|
||||
caretta-sync-macros = { path="macros", optional = true}
|
||||
|
||||
[dev-dependencies]
|
||||
caretta-sync-core = {workspace = true, features = ["test"]}
|
||||
|
||||
[workspace]
|
||||
members = [ "lazy-supplements-*", "examples/*" ]
|
||||
members = [ ".", "core", "macros", "cli", "mobile", "examples/*" , "bevy"]
|
||||
resolver = "3"
|
||||
|
||||
[workspace.package]
|
||||
|
@ -10,10 +37,27 @@ license = "MIT OR Apache-2.0"
|
|||
repository = "https://forgejo.fireturlte.net/lazy-supplements"
|
||||
|
||||
[workspace.dependencies]
|
||||
dioxus = { version = "0.6.0", features = [] }
|
||||
lazy-supplements-core.path = "lazy-supplements-core"
|
||||
bevy = { git = "https://github.com/bevyengine/bevy.git", rev="16ffdaea0daec11e4347d965f56c9c8e1122a488" }
|
||||
chrono = "0.4.41"
|
||||
ciborium = "0.2.2"
|
||||
clap = { version = "4.5.38", features = ["derive"] }
|
||||
caretta-sync-core.path = "core"
|
||||
futures = { version = "0.3.31", features = ["executor"] }
|
||||
libp2p = { version = "0.55.0", features = ["macros", "mdns", "noise", "ping", "tcp", "tokio", "yamux" ] }
|
||||
sea-orm = { version = "1.1.11", features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros", "with-chrono", "with-uuid"] }
|
||||
sea-orm-migration = { version = "1.1.0", features = ["runtime-tokio-rustls", "sqlx-postgres"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
thiserror = "2.0.12"
|
||||
tokio = { version = "1.45.0", features = ["macros", "rt", "rt-multi-thread"] }
|
||||
tonic = "0.14.0"
|
||||
uuid = { version = "1.17.0", features = ["v7"] }
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 1
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
lto = "thin"
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
# Lazy Supplements Framework
|
||||
# Caretta Framework
|
||||
|
||||
A local-first application framework for lazy person
|
||||
A local-first application framework.
|
||||
|
||||
## Features
|
||||
- Local first
|
||||
- Decentralized data synchronization with libp2p
|
||||
- Device management
|
15
bevy/Cargo.toml
Normal file
15
bevy/Cargo.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "caretta-sync-bevy"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
bevy.workspace = true
|
||||
caretta-sync-core.workspace = true
|
||||
futures.workspace = true
|
||||
sea-orm.workspace = true
|
||||
tokio.workspace = true
|
||||
tonic.workspace = true
|
4
bevy/src/global.rs
Normal file
4
bevy/src/global.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
use bevy::{asset::uuid::Uuid, ecs::component::Component};
|
||||
|
||||
#[derive(Component)]
|
||||
struct Id(Uuid);
|
2
bevy/src/lib.rs
Normal file
2
bevy/src/lib.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod global;
|
||||
pub mod peer;
|
47
bevy/src/peer.rs
Normal file
47
bevy/src/peer.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use bevy::{app::{App, Plugin, Startup, Update}, ecs::{component::Component, query::With, system::{Commands, Query}}, tasks::TaskPool};
|
||||
use caretta_sync_core::{cache::entity::{CachedPeerEntity, CachedPeerModel}, global::{CONFIG, DATABASE_CONNECTIONS}};
|
||||
use caretta_sync_core::{
|
||||
proto::*,
|
||||
};
|
||||
use sea_orm::EntityTrait;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Peer;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct PeerId(String);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct PeerAddress(String);
|
||||
|
||||
#[tokio::main]
|
||||
async fn add_cached_peers(mut commands: Commands) {
|
||||
let config = CONFIG.get_unchecked();
|
||||
let path = String::from("unix://") + config.rpc.socket_path.as_os_str().to_str().expect("Invalid string");
|
||||
let mut client = caretta_sync_core::proto::cached_peer_service_client::CachedPeerServiceClient::connect(path).await.expect("Unix socket should be accessible");
|
||||
let request = tonic::Request::new(CachedPeerListRequest {});
|
||||
let response = client.list(request).await.expect("Faild to request/response");
|
||||
let peers = response.into_inner().peers;
|
||||
for model in peers.into_iter() {
|
||||
commands.spawn((Peer, PeerId(model.peer_id.to_string())));
|
||||
}
|
||||
}
|
||||
|
||||
fn print_peer(query: Query<&PeerId, With<Peer>>) {
|
||||
for peer_id in &query {
|
||||
println!("Hello {}!", peer_id.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn hello_world() {
|
||||
println!("hello world!");
|
||||
}
|
||||
|
||||
pub struct PeerPlugin;
|
||||
|
||||
impl Plugin for PeerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, add_cached_peers);
|
||||
app.add_systems(Update, (hello_world, print_peer));
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "lazy-supplements-desktop"
|
||||
name = "caretta-sync-cli"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
|
@ -8,16 +8,21 @@ repository.workspace = true
|
|||
|
||||
[features]
|
||||
default = []
|
||||
test = ["lazy-supplements-core/test"]
|
||||
test = ["caretta-sync-core/test"]
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.38", features = ["derive"] }
|
||||
ciborium.workspace = true
|
||||
clap.workspace = true
|
||||
dirs = "6.0.0"
|
||||
lazy-supplements-core.workspace = true
|
||||
caretta-sync-core = { workspace = true, features = ["cli"] }
|
||||
libp2p.workspace = true
|
||||
sea-orm.workspace = true
|
||||
serde.workspace = true
|
||||
thiserror.workspace = true
|
||||
tokio.workspace = true
|
||||
tonic.workspace = true
|
||||
|
||||
uuid.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
lazy-supplements-core = {workspace = true, features = ["test"]}
|
||||
caretta-sync-core = {workspace = true, features = ["test"]}
|
69
cli/src/cli/args/config.rs
Normal file
69
cli/src/cli/args/config.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use std::{net::IpAddr, path::PathBuf, sync::LazyLock};
|
||||
|
||||
use clap::Args;
|
||||
use caretta_sync_core::{
|
||||
config::{Config, ConfigError, PartialConfig, PartialP2pConfig, PartialStorageConfig},
|
||||
utils::{emptiable::Emptiable, mergeable::Mergeable}
|
||||
};
|
||||
|
||||
use libp2p::identity::Keypair;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct ConfigArgs {
|
||||
#[arg(short = 'c', long = "config")]
|
||||
pub file_path: Option<PathBuf>,
|
||||
#[arg(skip)]
|
||||
pub file_content: OnceCell<PartialConfig>,
|
||||
#[command(flatten)]
|
||||
pub args: PartialConfig,
|
||||
}
|
||||
|
||||
|
||||
impl ConfigArgs {
|
||||
fn get_file_path_or_default(&self, app_name: &'static str) -> PathBuf {
|
||||
self.file_path.clone().unwrap_or(
|
||||
dirs::config_local_dir()
|
||||
.expect("Config user directory should be set")
|
||||
.join(app_name)
|
||||
.join("config.toml")
|
||||
)
|
||||
}
|
||||
async fn get_or_read_file_content(&self, app_name: &'static str) -> PartialConfig {
|
||||
self.file_content.get_or_init(|| async {
|
||||
PartialConfig::read_from(self.get_file_path_or_default(app_name)).await.expect("Config file should be invalid!")
|
||||
}).await.clone()
|
||||
}
|
||||
pub async fn to_partial_config_with_default(&self, app_name: &'static str) -> PartialConfig {
|
||||
let mut default = PartialConfig::default_desktop(app_name);
|
||||
default.merge(self.to_partial_config_without_default(app_name).await);
|
||||
default
|
||||
}
|
||||
pub async fn to_partial_config_without_default(&self, app_name: &'static str) -> PartialConfig {
|
||||
let mut file_content = self.get_or_read_file_content(app_name).await;
|
||||
let args = self.args.clone();
|
||||
file_content.merge(args);
|
||||
file_content
|
||||
}
|
||||
async fn has_p2p_private_key(&self, app_name: &'static str) -> bool {
|
||||
let merged = self.to_partial_config_with_default(app_name).await;
|
||||
match merged.p2p {
|
||||
Some(p2p) => p2p.private_key.is_some(),
|
||||
None => false
|
||||
}
|
||||
}
|
||||
pub async fn into_config(mut self, app_name: &'static str) -> Config {
|
||||
if !self.has_p2p_private_key(app_name).await {
|
||||
let path = self.get_file_path_or_default(app_name);
|
||||
let mut content = self.file_content.get_mut().unwrap();
|
||||
if let Some(p2p) = content.p2p.as_mut() {
|
||||
p2p.init_private_key();
|
||||
} else {
|
||||
content.p2p.insert(PartialP2pConfig::empty().with_new_private_key());
|
||||
}
|
||||
content.write_to(path).await.expect("Config file should be writable first time to initialize secret");
|
||||
}
|
||||
self.to_partial_config_with_default(app_name).await.try_into().expect("Some configurations are missing!")
|
||||
}
|
||||
}
|
12
cli/src/cli/args/device.rs
Normal file
12
cli/src/cli/args/device.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use clap::Args;
|
||||
use libp2p::{Multiaddr, PeerId};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
#[group(multiple = false, required = true)]
|
||||
pub struct DeviceArgs {
|
||||
device_number: Option<u32>,
|
||||
device_id: Option<Uuid>,
|
||||
peer_id: Option<PeerId>,
|
||||
multiaddr: Option<Multiaddr>,
|
||||
}
|
7
cli/src/cli/args/mod.rs
Normal file
7
cli/src/cli/args/mod.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
mod config;
|
||||
mod device;
|
||||
mod peer;
|
||||
|
||||
pub use config::ConfigArgs;
|
||||
pub use device::DeviceArgs;
|
||||
pub use peer::PeerArgs;
|
10
cli/src/cli/args/peer.rs
Normal file
10
cli/src/cli/args/peer.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use clap::Args;
|
||||
use libp2p::{Multiaddr, PeerId};
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
#[group(multiple = false, required = true)]
|
||||
pub struct PeerArgs {
|
||||
cache_number: Option<u32>,
|
||||
peer_id: Option<PeerId>,
|
||||
multiaddr: Option<Multiaddr>,
|
||||
}
|
17
cli/src/cli/config/check.rs
Normal file
17
cli/src/cli/config/check.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::ConfigArgs;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ConfigCheckCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for ConfigCheckCommandArgs {
|
||||
#[tokio::main]
|
||||
async fn run(self, app_name: &'static str) {
|
||||
let _ = self.config.into_config(app_name).await;
|
||||
println!("Ok");
|
||||
}
|
||||
}
|
24
cli/src/cli/config/list.rs
Normal file
24
cli/src/cli/config/list.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::{config::PartialConfig, utils::runnable::Runnable};
|
||||
use crate::cli::ConfigArgs;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ConfigListCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs,
|
||||
#[arg(short,long)]
|
||||
all: bool
|
||||
}
|
||||
|
||||
impl Runnable for ConfigListCommandArgs {
|
||||
#[tokio::main]
|
||||
async fn run(self, app_name: &'static str) {
|
||||
let config: PartialConfig = if self.all {
|
||||
self.config.into_config(app_name).await.into()
|
||||
} else {
|
||||
self.config.to_partial_config_without_default(app_name).await
|
||||
};
|
||||
println!("{}", config.into_toml().unwrap())
|
||||
|
||||
}
|
||||
}
|
38
cli/src/cli/config/mod.rs
Normal file
38
cli/src/cli/config/mod.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
mod check;
|
||||
mod list;
|
||||
|
||||
pub use check::*;
|
||||
pub use list::*;
|
||||
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use clap::{Args, Subcommand};
|
||||
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ConfigCommandArgs {
|
||||
#[command(subcommand)]
|
||||
pub command: ConfigSubcommand
|
||||
}
|
||||
|
||||
impl Runnable for ConfigCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
self.command.run(app_name)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum ConfigSubcommand {
|
||||
Check(ConfigCheckCommandArgs),
|
||||
List(ConfigListCommandArgs),
|
||||
}
|
||||
|
||||
impl Runnable for ConfigSubcommand {
|
||||
fn run(self, app_name: &'static str) {
|
||||
match self {
|
||||
Self::Check(x) => x.run(app_name),
|
||||
Self::List(x) => x.run(app_name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
24
cli/src/cli/device/add.rs
Normal file
24
cli/src/cli/device/add.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
|
||||
use crate::cli::ConfigArgs;
|
||||
|
||||
use crate::cli::PeerArgs;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct DeviceAddCommandArgs {
|
||||
#[command(flatten)]
|
||||
peer: PeerArgs,
|
||||
#[arg(short, long)]
|
||||
passcode: Option<String>,
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for DeviceAddCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
|
15
cli/src/cli/device/list.rs
Normal file
15
cli/src/cli/device/list.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::ConfigArgs;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct DeviceListCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for DeviceListCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
50
cli/src/cli/device/mod.rs
Normal file
50
cli/src/cli/device/mod.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
mod add;
|
||||
mod list;
|
||||
mod ping;
|
||||
mod remove;
|
||||
mod scan;
|
||||
|
||||
pub use add::DeviceAddCommandArgs;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use libp2p::{Multiaddr, PeerId};
|
||||
pub use list::DeviceListCommandArgs;
|
||||
pub use ping::DevicePingCommandArgs;
|
||||
pub use remove::DeviceRemoveCommandArgs;
|
||||
pub use scan::DeviceScanCommandArgs;
|
||||
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct DeviceCommandArgs {
|
||||
#[command(subcommand)]
|
||||
pub command: DeviceSubcommand
|
||||
}
|
||||
|
||||
impl Runnable for DeviceCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
self.command.run(app_name)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum DeviceSubcommand {
|
||||
Add(DeviceAddCommandArgs),
|
||||
List(DeviceListCommandArgs),
|
||||
Ping(DevicePingCommandArgs),
|
||||
Remove(DeviceRemoveCommandArgs),
|
||||
Scan(DeviceScanCommandArgs),
|
||||
}
|
||||
|
||||
impl Runnable for DeviceSubcommand {
|
||||
fn run(self, app_name: &'static str) {
|
||||
match self {
|
||||
Self::Add(x) => x.run(app_name),
|
||||
Self::List(x) => x.run(app_name),
|
||||
Self::Ping(x) => x.run(app_name),
|
||||
Self::Remove(x) => x.run(app_name),
|
||||
Self::Scan(x) => x.run(app_name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
cli/src/cli/device/ping.rs
Normal file
17
cli/src/cli/device/ping.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::{ConfigArgs, PeerArgs};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct DevicePingCommandArgs{
|
||||
#[command(flatten)]
|
||||
peer: PeerArgs,
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for DevicePingCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
17
cli/src/cli/device/remove.rs
Normal file
17
cli/src/cli/device/remove.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::{ConfigArgs, DeviceArgs};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct DeviceRemoveCommandArgs{
|
||||
#[command(flatten)]
|
||||
device: DeviceArgs,
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for DeviceRemoveCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
15
cli/src/cli/device/scan.rs
Normal file
15
cli/src/cli/device/scan.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::ConfigArgs;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct DeviceScanCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for DeviceScanCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
13
cli/src/cli/mod.rs
Normal file
13
cli/src/cli/mod.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
mod args;
|
||||
mod config;
|
||||
mod device;
|
||||
mod peer;
|
||||
mod serve;
|
||||
|
||||
pub use args::*;
|
||||
pub use config::*;
|
||||
pub use device::*;
|
||||
pub use peer::*;
|
||||
pub use serve::*;
|
17
cli/src/cli/peer/info.rs
Normal file
17
cli/src/cli/peer/info.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::{ConfigArgs, PeerArgs};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct PeerInfoCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs,
|
||||
#[command(flatten)]
|
||||
peer: PeerArgs,
|
||||
}
|
||||
|
||||
impl Runnable for PeerInfoCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
24
cli/src/cli/peer/list.rs
Normal file
24
cli/src/cli/peer/list.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::{
|
||||
utils::runnable::Runnable,
|
||||
proto::*,
|
||||
};
|
||||
use crate::cli::ConfigArgs;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct PeerListCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs
|
||||
}
|
||||
|
||||
impl Runnable for PeerListCommandArgs {
|
||||
#[tokio::main]
|
||||
async fn run(self, app_name: &'static str) {
|
||||
let config = self.config.into_config(app_name).await;
|
||||
let path = String::from("unix://") + config.rpc.socket_path.as_os_str().to_str().expect("Invalid string");
|
||||
let mut client = caretta_sync_core::proto::cached_peer_service_client::CachedPeerServiceClient::connect(path).await.expect("Unix socket should be accessible");
|
||||
let request = tonic::Request::new(CachedPeerListRequest {});
|
||||
let response = client.list(request).await.expect("Faild to request/response");
|
||||
println!("{:?}", response);
|
||||
}
|
||||
}
|
42
cli/src/cli/peer/mod.rs
Normal file
42
cli/src/cli/peer/mod.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
mod info;
|
||||
mod list;
|
||||
mod ping;
|
||||
|
||||
pub use info::*;
|
||||
pub use list::*;
|
||||
pub use ping::*;
|
||||
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use clap::{Args, Subcommand};
|
||||
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct PeerCommandArgs {
|
||||
#[command(subcommand)]
|
||||
pub command: PeerSubcommand
|
||||
}
|
||||
|
||||
impl Runnable for PeerCommandArgs {
|
||||
fn run(self, app_name: &'static str) {
|
||||
self.command.run(app_name)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum PeerSubcommand {
|
||||
Info(PeerInfoCommandArgs),
|
||||
List(PeerListCommandArgs),
|
||||
Ping(PeerPingCommandArgs),
|
||||
}
|
||||
|
||||
impl Runnable for PeerSubcommand {
|
||||
fn run(self, app_name: &'static str) {
|
||||
match self {
|
||||
Self::Info(x) => x.run(app_name),
|
||||
Self::List(x) => x.run(app_name),
|
||||
Self::Ping(x) => x.run(app_name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
18
cli/src/cli/peer/ping.rs
Normal file
18
cli/src/cli/peer/ping.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use clap::Args;
|
||||
use caretta_sync_core::utils::runnable::Runnable;
|
||||
use crate::cli::{ConfigArgs, PeerArgs};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct PeerPingCommandArgs{
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs,
|
||||
#[command(flatten)]
|
||||
peer: PeerArgs,
|
||||
}
|
||||
|
||||
impl Runnable for PeerPingCommandArgs {
|
||||
#[tokio::main]
|
||||
async fn run(self, app_name: &'static str) {
|
||||
todo!()
|
||||
}
|
||||
}
|
29
cli/src/cli/serve.rs
Normal file
29
cli/src/cli/serve.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use clap::Args;
|
||||
use caretta_sync_core::{config::Config, data::migration::DataMigrator, global::{CONFIG, DATABASE_CONNECTIONS}, server::ServerTrait, utils::runnable::Runnable};
|
||||
use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
|
||||
|
||||
use super::ConfigArgs;
|
||||
|
||||
#[derive(Args, Debug)]
|
||||
pub struct ServeCommandArgs<T>
|
||||
where
|
||||
T: ServerTrait
|
||||
{
|
||||
#[arg(skip)]
|
||||
server: PhantomData<T>,
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs,
|
||||
}
|
||||
impl<T> Runnable for ServeCommandArgs<T>
|
||||
where
|
||||
T: ServerTrait
|
||||
{
|
||||
#[tokio::main]
|
||||
async fn run(self, app_name: &'static str) {
|
||||
let config = CONFIG.get_or_init::<Config>(self.config.into_config(app_name).await).await;
|
||||
let _ = DATABASE_CONNECTIONS.get_or_init_unchecked(&config, DataMigrator).await;
|
||||
T::serve_all(config).await.unwrap();
|
||||
}
|
||||
}
|
1
cli/src/lib.rs
Normal file
1
cli/src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod cli;
|
55
core/Cargo.toml
Normal file
55
core/Cargo.toml
Normal file
|
@ -0,0 +1,55 @@
|
|||
[package]
|
||||
name = "caretta-sync-core"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[features]
|
||||
default = []
|
||||
cli = ["dep:clap"]
|
||||
test = ["dep:tempfile", ]
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
chrono.workspace = true
|
||||
chrono-tz = "0.10.3"
|
||||
ciborium.workspace = true
|
||||
clap = {workspace = true, optional = true}
|
||||
dirs = "6.0.0"
|
||||
futures.workspace = true
|
||||
libp2p.workspace = true
|
||||
libp2p-core = { version = "0.43.0", features = ["serde"] }
|
||||
libp2p-identity = { version = "0.2.11", features = ["ed25519", "peerid", "rand", "serde"] }
|
||||
prost = "0.14.1"
|
||||
sea-orm.workspace = true
|
||||
sea-orm-migration.workspace = true
|
||||
serde.workspace = true
|
||||
tempfile = { version = "3.20.0", optional = true }
|
||||
thiserror.workspace = true
|
||||
tokio.workspace = true
|
||||
toml = "0.8.22"
|
||||
tonic.workspace = true
|
||||
tonic-prost = "0.14.0"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||
uuid.workspace = true
|
||||
prost-types = "0.14.1"
|
||||
sysinfo = "0.37.0"
|
||||
whoami = "1.6.1"
|
||||
|
||||
[target.'cfg(target_os="android")'.dependencies]
|
||||
jni = "0.21.1"
|
||||
ndk = "0.9.0"
|
||||
|
||||
[target.'cfg(target_os="ios")'.dependencies]
|
||||
objc = "0.2.7"
|
||||
objc-foundation = "0.1.1"
|
||||
objc_id = "0.1.1"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.20.0"
|
||||
|
||||
[build-dependencies]
|
||||
tonic-prost-build = "0.14.0"
|
4
core/build.rs
Normal file
4
core/build.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
tonic_prost_build::compile_protos("proto/caretta_sync.proto")?;
|
||||
Ok(())
|
||||
}
|
33
core/proto/caretta_sync.proto
Normal file
33
core/proto/caretta_sync.proto
Normal file
|
@ -0,0 +1,33 @@
|
|||
syntax = "proto3";
|
||||
package caretta_sync;
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
enum PeerListOrderBy {
|
||||
CREATED_AT = 0;
|
||||
UPDATED_AT = 1;
|
||||
PEER_ID = 2;
|
||||
}
|
||||
|
||||
service CachedPeerService {
|
||||
rpc List(CachedPeerListRequest) returns (CachedPeerListResponse);
|
||||
}
|
||||
|
||||
message CachedPeerListRequest {}
|
||||
|
||||
message CachedPeerMessage {
|
||||
uint32 number = 1;
|
||||
string peer_id = 2;
|
||||
google.protobuf.Timestamp created_at = 3;
|
||||
repeated CachedAddressMessage addresses = 4;
|
||||
}
|
||||
|
||||
message CachedAddressMessage {
|
||||
uint32 number = 1;
|
||||
google.protobuf.Timestamp created_at = 2;
|
||||
google.protobuf.Timestamp updated_at = 3;
|
||||
string multiaddress = 4;
|
||||
}
|
||||
|
||||
message CachedPeerListResponse {
|
||||
repeated CachedPeerMessage peers = 1;
|
||||
}
|
59
core/src/cache/entity/cached_address.rs
vendored
Normal file
59
core/src/cache/entity/cached_address.rs
vendored
Normal file
|
@ -0,0 +1,59 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use chrono::{Days, Local};
|
||||
use libp2p::{multiaddr, Multiaddr, PeerId};
|
||||
use prost_types::Timestamp;
|
||||
use sea_orm::{entity::{
|
||||
prelude::*, *
|
||||
}, sea_query};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{cache, data::value::{MultiaddrValue, PeerIdValue}, utils::utc_to_timestamp};
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
||||
#[sea_orm(table_name = "cached_address")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: u32,
|
||||
#[sea_orm(indexed)]
|
||||
pub created_at: DateTimeUtc,
|
||||
#[sea_orm(indexed)]
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(indexed)]
|
||||
pub cached_peer_id: u32,
|
||||
#[sea_orm(indexed)]
|
||||
pub multiaddress: MultiaddrValue,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::CachedPeerEntity",
|
||||
from = "Column::CachedPeerId",
|
||||
to = "super::CachedPeerColumn::Id"
|
||||
)]
|
||||
CachedPeer,
|
||||
}
|
||||
impl Related<super::CachedPeerEntity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::CachedPeer.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl ActiveModel {
|
||||
pub fn new(cached_peer_id: u32, multiaddr: Multiaddr) -> Self {
|
||||
let timestamp: DateTimeUtc = Local::now().to_utc();
|
||||
Self{
|
||||
cached_peer_id: Set(cached_peer_id),
|
||||
multiaddress: Set(MultiaddrValue::from(multiaddr)),
|
||||
created_at: Set(timestamp),
|
||||
updated_at: Set(timestamp),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
57
core/src/cache/entity/cached_peer.rs
vendored
Normal file
57
core/src/cache/entity/cached_peer.rs
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use chrono::{Days, Local};
|
||||
use libp2p::{multiaddr, Multiaddr, PeerId};
|
||||
use sea_orm::{entity::{
|
||||
prelude::*, *
|
||||
}, sea_query};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::data::value::{MultiaddrValue, PeerIdValue};
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
||||
#[sea_orm(table_name = "cached_peer")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: u32,
|
||||
#[sea_orm(indexed)]
|
||||
pub created_at: DateTimeUtc,
|
||||
#[sea_orm(indexed)]
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(indexed)]
|
||||
pub peer_id: PeerIdValue,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::CachedAddressEntity")]
|
||||
CachedAddress,
|
||||
}
|
||||
|
||||
impl Related<super::CachedAddressEntity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::CachedAddress.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl ActiveModel {
|
||||
pub fn new(peer_id: PeerId) -> Self {
|
||||
let timestamp: DateTimeUtc = Local::now().to_utc();
|
||||
Self{
|
||||
peer_id: Set(PeerIdValue::from(peer_id)),
|
||||
created_at: Set(timestamp),
|
||||
updated_at: Set(timestamp),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
pub fn find_by_peer_id(peer_id: PeerId) -> Select<Entity> {
|
||||
Self::find().filter(Column::PeerId.eq(PeerIdValue::from(peer_id)))
|
||||
}
|
||||
}
|
48
core/src/cache/entity/mod.rs
vendored
Normal file
48
core/src/cache/entity/mod.rs
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
mod cached_peer;
|
||||
mod cached_address;
|
||||
|
||||
pub use cached_peer::{
|
||||
ActiveModel as CachedPeerActiveModel,
|
||||
Column as CachedPeerColumn,
|
||||
Model as CachedPeerModel,
|
||||
Entity as CachedPeerEntity,
|
||||
};
|
||||
|
||||
pub use cached_address::{
|
||||
ActiveModel as CachedAddressActiveModel,
|
||||
Column as CachedAddressColumn,
|
||||
Model as CachedAddressModel,
|
||||
Entity as CachedAddressEntity,
|
||||
};
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
use crate::{cache::entity::cached_peer, data::migration::DataMigrator, global::{DATABASE_CONNECTIONS}, tests::TEST_CONFIG};
|
||||
|
||||
use super::*;
|
||||
|
||||
use libp2p::{identity::{self, Keypair}, multiaddr, swarm::handler::multi, Multiaddr, PeerId};
|
||||
use sea_orm::ActiveModelTrait;
|
||||
|
||||
|
||||
|
||||
#[tokio::test]
|
||||
async fn insert() {
|
||||
|
||||
let db = DATABASE_CONNECTIONS.get_or_init_unchecked(&*TEST_CONFIG, DataMigrator).await.cache;
|
||||
let peer_id = Keypair::generate_ed25519().public().to_peer_id();
|
||||
let multiaddr = Multiaddr::empty()
|
||||
.with(Ipv4Addr::new(127,0,0,1).into())
|
||||
.with(multiaddr::Protocol::Tcp(0));
|
||||
let inserted_cached_peer: CachedPeerModel = CachedPeerActiveModel::new(peer_id.clone())
|
||||
.insert(db).await.unwrap();
|
||||
let inserted_cached_address: CachedAddressModel = CachedAddressActiveModel::new(inserted_cached_peer.id, multiaddr.clone())
|
||||
.insert(db).await.unwrap();
|
||||
assert_eq!(PeerId::from(inserted_cached_peer.peer_id), peer_id);
|
||||
assert_eq!(Multiaddr::from(inserted_cached_address.multiaddress), multiaddr);
|
||||
}
|
||||
|
||||
}
|
148
core/src/cache/migration/m20220101_000001_create_cache_tables.rs
vendored
Normal file
148
core/src/cache/migration/m20220101_000001_create_cache_tables.rs
vendored
Normal file
|
@ -0,0 +1,148 @@
|
|||
use sea_orm_migration::{prelude::*, schema::*};
|
||||
|
||||
use crate::migration::TableMigration;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
CachedPeer::up(manager).await?;
|
||||
CachedAddress::up(manager).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
CachedAddress::down(manager).await?;
|
||||
CachedPeer::down(manager).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden, DeriveMigrationName)]
|
||||
enum CachedPeer {
|
||||
Table,
|
||||
Id,
|
||||
PeerId,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
}
|
||||
|
||||
static IDX_CACHED_PEER_PEER_ID: &str = "idx_cached_peer_peer_id";
|
||||
static IDX_CACHED_PEER_CREATED_AT: &str = "idx_cached_peer_created_at";
|
||||
static IDX_CACHED_PEER_UPDATED_AT: &str = "idx_cached_peer_updated_at";
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl TableMigration for CachedPeer {
|
||||
async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
|
||||
manager.create_table(
|
||||
Table::create()
|
||||
.table(Self::Table)
|
||||
.if_not_exists()
|
||||
.col(pk_auto(Self::Id))
|
||||
.col(string_len(Self::PeerId, 255))
|
||||
.col(timestamp(Self::CreatedAt))
|
||||
.col(timestamp(Self::UpdatedAt))
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_PEER_PEER_ID)
|
||||
.table(Self::Table)
|
||||
.col(Self::PeerId)
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_PEER_CREATED_AT)
|
||||
.table(Self::Table)
|
||||
.col(Self::CreatedAt)
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_PEER_UPDATED_AT)
|
||||
.table(Self::Table)
|
||||
.col(Self::UpdatedAt)
|
||||
.to_owned()
|
||||
).await?;
|
||||
Ok(())
|
||||
}
|
||||
async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>{
|
||||
manager.drop_table(Table::drop().table(Self::Table).to_owned()).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden, DeriveMigrationName)]
|
||||
enum CachedAddress {
|
||||
Table,
|
||||
Id,
|
||||
CachedPeerId,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
Multiaddress,
|
||||
}
|
||||
|
||||
static IDX_CACHED_ADDRESS_MULTIADDRESS: &str = "idx_cached_address_multiaddress";
|
||||
static IDX_CACHED_ADDRESS_CACHED_PEER_ID: &str = "idx_cached_address_cached_peer_id";
|
||||
static IDX_CACHED_ADDRESS_CREATED_AT: &str = "idx_cached_address_created_at";
|
||||
static IDX_CACHED_ADDRESS_UPDATED_AT: &str = "idx_cached_address_updated_at";
|
||||
static FK_CACHED_ADDRESS_CACHED_PEER: &str = "fk_cached_address_cached_peer";
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl TableMigration for CachedAddress {
|
||||
async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
|
||||
manager.create_table(
|
||||
Table::create()
|
||||
.table(Self::Table)
|
||||
.if_not_exists()
|
||||
.col(pk_auto(Self::Id))
|
||||
.col(integer(Self::CachedPeerId))
|
||||
.foreign_key(ForeignKey::create()
|
||||
.name(FK_CACHED_ADDRESS_CACHED_PEER)
|
||||
.from(Self::Table,Self::CachedPeerId)
|
||||
.to(CachedPeer::Table, CachedPeer::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.on_update(ForeignKeyAction::Cascade)
|
||||
)
|
||||
.col(timestamp(Self::CreatedAt))
|
||||
.col(timestamp(Self::UpdatedAt))
|
||||
.col(text_uniq(Self::Multiaddress))
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_ADDRESS_CACHED_PEER_ID)
|
||||
.table(Self::Table)
|
||||
.col(Self::CachedPeerId)
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_ADDRESS_MULTIADDRESS)
|
||||
.table(Self::Table)
|
||||
.col(Self::Multiaddress)
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_ADDRESS_CREATED_AT)
|
||||
.table(Self::Table)
|
||||
.col(Self::CreatedAt)
|
||||
.to_owned()
|
||||
).await?;
|
||||
manager.create_index(
|
||||
Index::create()
|
||||
.name(IDX_CACHED_ADDRESS_UPDATED_AT)
|
||||
.table(Self::Table)
|
||||
.col(Self::UpdatedAt)
|
||||
.to_owned()
|
||||
).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>{
|
||||
manager.drop_table(Table::drop().table(Self::Table).to_owned()).await
|
||||
}
|
||||
}
|
11
core/src/config/error.rs
Normal file
11
core/src/config/error.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum ConfigError {
|
||||
#[error("missing config: {0}")]
|
||||
MissingConfig(String),
|
||||
#[error("Io error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Toml Deserialization Error")]
|
||||
TomlDerialization(#[from] toml::de::Error),
|
||||
#[error("Toml Serialization Error")]
|
||||
TomlSerialization(#[from] toml::ser::Error),
|
||||
}
|
159
core/src/config/mod.rs
Normal file
159
core/src/config/mod.rs
Normal file
|
@ -0,0 +1,159 @@
|
|||
pub mod error;
|
||||
mod storage;
|
||||
mod p2p;
|
||||
mod rpc;
|
||||
|
||||
use std::{path::Path, default::Default};
|
||||
use crate::{utils::{emptiable::Emptiable, mergeable::Mergeable}};
|
||||
pub use error::ConfigError;
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
|
||||
use tokio::{fs::File, io::{AsyncReadExt, AsyncWriteExt}};
|
||||
pub use storage::{StorageConfig, PartialStorageConfig};
|
||||
pub use p2p::{P2pConfig, PartialP2pConfig};
|
||||
pub use rpc::*;
|
||||
|
||||
#[cfg(feature="cli")]
|
||||
use clap::Args;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Config {
|
||||
pub p2p: P2pConfig,
|
||||
pub storage: StorageConfig,
|
||||
pub rpc: RpcConfig,
|
||||
}
|
||||
|
||||
impl AsRef<StorageConfig> for Config {
|
||||
fn as_ref(&self) -> &StorageConfig {
|
||||
&self.storage
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<P2pConfig> for Config {
|
||||
fn as_ref(&self) -> &P2pConfig {
|
||||
&self.p2p
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<RpcConfig> for Config {
|
||||
fn as_ref(&self) -> &RpcConfig {
|
||||
&self.rpc
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PartialConfig> for Config {
|
||||
type Error = crate::error::Error;
|
||||
fn try_from(value: PartialConfig) -> Result<Self, Self::Error> {
|
||||
Ok(Self{
|
||||
rpc: value.rpc.ok_or(crate::error::Error::MissingConfig("rpc"))?.try_into()?,
|
||||
p2p: value.p2p.ok_or(crate::error::Error::MissingConfig("p2p"))?.try_into()?,
|
||||
storage: value.storage.ok_or(crate::error::Error::MissingConfig("storage"))?.try_into()?
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature="cli", derive(Args))]
|
||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||
pub struct PartialConfig {
|
||||
#[cfg_attr(feature="cli", command(flatten))]
|
||||
pub p2p: Option<PartialP2pConfig>,
|
||||
#[cfg_attr(feature="cli", command(flatten))]
|
||||
pub storage: Option<PartialStorageConfig>,
|
||||
#[cfg_attr(feature="cli", command(flatten))]
|
||||
pub rpc: Option<PartialRpcConfig>,
|
||||
}
|
||||
|
||||
impl PartialConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
p2p : Some(PartialP2pConfig::empty().with_new_private_key()),
|
||||
storage: Some(PartialStorageConfig::empty()),
|
||||
rpc: Some(PartialRpcConfig::empty()),
|
||||
}
|
||||
}
|
||||
pub fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
|
||||
toml::from_str(s)
|
||||
}
|
||||
pub fn into_toml(&self) -> Result<String, toml::ser::Error> {
|
||||
toml::to_string(self)
|
||||
}
|
||||
pub async fn read_or_create<T>(path: T) -> Result<Self, ConfigError>
|
||||
where
|
||||
T: AsRef<Path>
|
||||
{
|
||||
if !path.as_ref().exists() {
|
||||
Self::new().write_to(&path).await?;
|
||||
}
|
||||
Self::read_from(&path).await
|
||||
}
|
||||
pub async fn read_from<T>(path:T) -> Result<Self, ConfigError>
|
||||
where
|
||||
T: AsRef<Path>
|
||||
{
|
||||
if !path.as_ref().exists() {
|
||||
if let Some(x) = path.as_ref().parent() {
|
||||
std::fs::create_dir_all(x)?;
|
||||
};
|
||||
let _ = File::create(&path).await?;
|
||||
}
|
||||
let mut file = File::open(path.as_ref()).await?;
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content).await?;
|
||||
let config: Self = toml::from_str(&content)?;
|
||||
Ok(config)
|
||||
}
|
||||
pub async fn write_to<T>(&self, path:T) -> Result<(), ConfigError>
|
||||
where
|
||||
T: AsRef<Path>
|
||||
{
|
||||
if !path.as_ref().exists() {
|
||||
if let Some(x) = path.as_ref().parent() {
|
||||
std::fs::create_dir_all(x)?;
|
||||
};
|
||||
let _ = File::create(&path).await?;
|
||||
}
|
||||
let mut file = File::create(&path).await?;
|
||||
file.write_all(toml::to_string(self)?.as_bytes()).await?;
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(not(any(target_os="android", target_os="ios")))]
|
||||
pub fn default_desktop(app_name: &'static str) -> Self {
|
||||
Self {
|
||||
p2p: Some(PartialP2pConfig::default()),
|
||||
rpc: Some(PartialRpcConfig::default(app_name)),
|
||||
storage: Some(PartialStorageConfig::default(app_name)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Config> for PartialConfig {
|
||||
fn from(value: Config) -> Self {
|
||||
Self {
|
||||
p2p: Some(value.p2p.into()),
|
||||
storage: Some(value.storage.into()),
|
||||
rpc: Some(value.rpc.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Emptiable for PartialConfig {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
p2p: None,
|
||||
storage: None,
|
||||
rpc: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.p2p.is_empty() && self.rpc.is_empty() && self.storage.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Mergeable for PartialConfig {
|
||||
fn merge(&mut self, other: Self) {
|
||||
self.p2p.merge(other.p2p);
|
||||
self.rpc.merge(other.rpc);
|
||||
self.storage.merge(other.storage);
|
||||
}
|
||||
}
|
181
core/src/config/p2p.rs
Normal file
181
core/src/config/p2p.rs
Normal file
|
@ -0,0 +1,181 @@
|
|||
use std::{net::{IpAddr, Ipv4Addr}, ops, path::{Path, PathBuf}};
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
#[cfg(feature="cli")]
|
||||
use clap::Args;
|
||||
use futures::StreamExt;
|
||||
use libp2p::{identity::{self, DecodingError, Keypair}, noise, ping, swarm::SwarmEvent, tcp, yamux, Swarm};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::{fs::File, io::{AsyncReadExt, AsyncWriteExt}};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
|
||||
use crate::{
|
||||
config::PartialConfig,
|
||||
error::Error, p2p, utils::{emptiable::Emptiable, mergeable::Mergeable}
|
||||
};
|
||||
|
||||
static DEFAULT_P2P_LISTEN_IPS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))];
|
||||
static DEFAULT_P2P_PORT: u16 = 0;
|
||||
|
||||
fn keypair_to_base64(keypair: &Keypair) -> String {
|
||||
let vec = match keypair.to_protobuf_encoding() {
|
||||
Ok(x) => x,
|
||||
Err(_) => unreachable!(),
|
||||
};
|
||||
BASE64_STANDARD.encode(vec)
|
||||
}
|
||||
|
||||
fn base64_to_keypair(base64: &str) -> Result<Keypair, Error> {
|
||||
let vec = BASE64_STANDARD.decode(base64)?;
|
||||
Ok(Keypair::from_protobuf_encoding(&vec)?)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct P2pConfig {
|
||||
pub private_key: Keypair,
|
||||
pub listen_ips: Vec<IpAddr>,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl P2pConfig {
|
||||
async fn try_into_swarm (self) -> Result<Swarm<p2p::Behaviour>, Error> {
|
||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.private_key)
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|keypair| p2p::Behaviour::try_from(keypair).unwrap())?
|
||||
.build();
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
Ok(swarm)
|
||||
}
|
||||
pub async fn launch_swarm(self) -> Result<(), Error>{
|
||||
let mut swarm = self.try_into_swarm().await?;
|
||||
loop{
|
||||
let swarm_event = swarm.select_next_some().await;
|
||||
tokio::spawn(async move{
|
||||
match swarm_event {
|
||||
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
|
||||
SwarmEvent::Behaviour(event) => {
|
||||
println!("{event:?}");
|
||||
event.run().await;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PartialP2pConfig> for P2pConfig {
|
||||
type Error = crate::error::Error;
|
||||
fn try_from(raw: PartialP2pConfig) -> Result<P2pConfig, Self::Error> {
|
||||
Ok(P2pConfig {
|
||||
private_key: base64_to_keypair(&raw.private_key.ok_or(Error::MissingConfig("secret"))?)?,
|
||||
listen_ips: raw.listen_ips.ok_or(Error::MissingConfig("listen_ips"))?,
|
||||
port: raw.port.ok_or(Error::MissingConfig("port"))?
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature="cli",derive(Args))]
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||
pub struct PartialP2pConfig {
|
||||
#[cfg_attr(feature="cli",arg(long))]
|
||||
pub private_key: Option<String>,
|
||||
#[cfg_attr(feature="cli",arg(long))]
|
||||
pub listen_ips: Option<Vec<IpAddr>>,
|
||||
#[cfg_attr(feature="cli",arg(long))]
|
||||
pub port: Option<u16>,
|
||||
}
|
||||
impl PartialP2pConfig {
|
||||
pub fn with_new_private_key(mut self) -> Self {
|
||||
self.private_key = Some(keypair_to_base64(&Keypair::generate_ed25519()));
|
||||
self
|
||||
}
|
||||
pub fn init_private_key(&mut self) {
|
||||
let _ = self.private_key.insert(keypair_to_base64(&Keypair::generate_ed25519()));
|
||||
}
|
||||
}
|
||||
|
||||
impl From<P2pConfig> for PartialP2pConfig {
|
||||
fn from(config: P2pConfig) -> Self {
|
||||
Self {
|
||||
private_key: Some(keypair_to_base64(&config.private_key)),
|
||||
listen_ips: Some(config.listen_ips),
|
||||
port: Some(config.port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PartialP2pConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
private_key: None,
|
||||
listen_ips: Some(Vec::from(DEFAULT_P2P_LISTEN_IPS)),
|
||||
port: Some(DEFAULT_P2P_PORT),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Emptiable for PartialP2pConfig {
|
||||
fn empty() -> Self {
|
||||
Self{
|
||||
private_key: None,
|
||||
listen_ips: None,
|
||||
port: None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.private_key.is_none() && self.listen_ips.is_none() && self.port.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl Mergeable for PartialP2pConfig {
|
||||
fn merge(&mut self, mut other: Self) {
|
||||
if let Some(x) = other.private_key.take() {
|
||||
let _ = self.private_key.insert(x);
|
||||
};
|
||||
if let Some(x) = other.listen_ips.take() {
|
||||
let _ = self.listen_ips.insert(x);
|
||||
};
|
||||
if let Some(x) = other.port.take() {
|
||||
let _ = self.port.insert(x);
|
||||
};
|
||||
}
|
||||
}
|
||||
impl Mergeable for Option<PartialP2pConfig> {
|
||||
fn merge(&mut self, mut other: Self) {
|
||||
match other.take() {
|
||||
Some(x) => {
|
||||
if let Some(y) = self.as_mut() {
|
||||
y.merge(x);
|
||||
} else {
|
||||
let _ = self.insert(x);
|
||||
}
|
||||
},
|
||||
None => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use libp2p::identity;
|
||||
use super::*;
|
||||
|
||||
|
||||
#[tokio::test]
|
||||
async fn parse_keypair() {
|
||||
let keypair = identity::Keypair::generate_ed25519();
|
||||
let keypair2 = base64_to_keypair(&keypair_to_base64(&keypair)).unwrap();
|
||||
|
||||
assert_eq!(keypair.public(), keypair2.public());
|
||||
}
|
||||
|
||||
}
|
82
core/src/config/rpc.rs
Normal file
82
core/src/config/rpc.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
use std::{net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener}, path::PathBuf, str::FromStr};
|
||||
#[cfg(feature="cli")]
|
||||
use clap::Args;
|
||||
use crate::{config::PartialConfig, utils::{emptiable::Emptiable, mergeable::Mergeable}};
|
||||
use libp2p::mdns::Config;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::config::error::ConfigError;
|
||||
|
||||
#[cfg(unix)]
|
||||
static DEFAULT_SOCKET_PATH: &str = "caretta.sock";
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RpcConfig {
|
||||
pub socket_path: PathBuf,
|
||||
}
|
||||
|
||||
impl TryFrom<PartialRpcConfig> for RpcConfig {
|
||||
type Error = ConfigError;
|
||||
fn try_from(config: PartialRpcConfig) -> Result<Self, Self::Error> {
|
||||
Ok(Self{
|
||||
socket_path: config.socket_path.ok_or(ConfigError::MissingConfig("port".to_string()))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature="cli", derive(Args))]
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||
pub struct PartialRpcConfig {
|
||||
pub socket_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl PartialRpcConfig {
|
||||
pub fn default(app_name: &'static str) -> Self {
|
||||
let username = whoami::username();
|
||||
Self{
|
||||
socket_path: Some(std::env::temp_dir().join(username).join(String::from(app_name) + ".sock")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Emptiable for PartialRpcConfig {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
socket_path: None,
|
||||
}
|
||||
}
|
||||
fn is_empty(&self) -> bool {
|
||||
self.socket_path.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RpcConfig> for PartialRpcConfig {
|
||||
fn from(source: RpcConfig) -> Self {
|
||||
Self {
|
||||
socket_path: Some(source.socket_path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mergeable for PartialRpcConfig {
|
||||
fn merge(&mut self, other: Self) {
|
||||
if let Some(x) = other.socket_path {
|
||||
self.socket_path = Some(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mergeable for Option<PartialRpcConfig> {
|
||||
fn merge(&mut self, mut other: Self) {
|
||||
match other.take() {
|
||||
Some(x) => {
|
||||
if let Some(y) = self.as_mut() {
|
||||
y.merge(x);
|
||||
} else {
|
||||
let _ = self.insert(x);
|
||||
}
|
||||
},
|
||||
None => {}
|
||||
};
|
||||
}
|
||||
}
|
126
core/src/config/storage.rs
Normal file
126
core/src/config/storage.rs
Normal file
|
@ -0,0 +1,126 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(feature="cli")]
|
||||
use clap::Args;
|
||||
|
||||
#[cfg(any(test, feature="test"))]
|
||||
use tempfile::tempdir;
|
||||
use crate::{config::{ConfigError, PartialConfig}, utils::{emptiable::Emptiable, get_binary_name, mergeable::Mergeable}};
|
||||
use libp2p::mdns::Config;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageConfig {
|
||||
pub data_directory: PathBuf,
|
||||
pub cache_directory: PathBuf,
|
||||
}
|
||||
|
||||
impl TryFrom<PartialStorageConfig> for StorageConfig {
|
||||
type Error = ConfigError;
|
||||
|
||||
fn try_from(value: PartialStorageConfig) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
data_directory: value.data_directory.ok_or(ConfigError::MissingConfig("data_directory".to_string()))?,
|
||||
cache_directory: value.cache_directory.ok_or(ConfigError::MissingConfig("cache_directory".to_string()))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
#[cfg_attr(feature="cli", derive(Args))]
|
||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||
pub struct PartialStorageConfig {
|
||||
#[cfg_attr(feature="cli", arg(long))]
|
||||
pub data_directory: Option<PathBuf>,
|
||||
#[cfg_attr(feature="cli", arg(long))]
|
||||
pub cache_directory: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl PartialStorageConfig {
|
||||
#[cfg(not(any(target_os="android", target_os="ios")))]
|
||||
pub fn default(app_name: &'static str) -> Self {
|
||||
|
||||
let mut data_dir = dirs::data_local_dir().unwrap();
|
||||
data_dir.push(app_name);
|
||||
let mut cache_dir = dirs::cache_dir().unwrap();
|
||||
cache_dir.push(app_name);
|
||||
|
||||
Self {
|
||||
data_directory: Some(data_dir),
|
||||
cache_directory: Some(cache_dir)
|
||||
}
|
||||
}
|
||||
#[cfg(target_os="android")]
|
||||
fn default_android() -> Self{
|
||||
let ctx = ndk_context::android_context();
|
||||
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }?;
|
||||
let mut env = vm.attach_current_thread()?;
|
||||
let ctx = unsafe { jni::objects::JObject::from_raw(ctx.context().cast()) };
|
||||
let cache_dir = env
|
||||
.call_method(ctx, "getFilesDir", "()Ljava/io/File;", &[])?
|
||||
.l()?;
|
||||
let cache_dir: jni::objects::JString = env
|
||||
.call_method(&cache_dir, "toString", "()Ljava/lang/String;", &[])?
|
||||
.l()?
|
||||
.try_into()?;
|
||||
let cache_dir = env.get_string(&cache_dir)?;
|
||||
let cache_dir = cache_dir.to_str()?;
|
||||
Ok(cache_dir.to_string())
|
||||
|
||||
}
|
||||
#[cfg(false)]
|
||||
fn default_ios(){
|
||||
unsafe {
|
||||
let file_manager: *mut Object = msg_send![Class::get("NSFileManager").unwrap(), defaultManager];
|
||||
let paths: Id<Object> = msg_send![file_manager, URLsForDirectory:1 inDomains:1];
|
||||
let first_path: *mut Object = msg_send![paths, firstObject];
|
||||
let path: Id<NSString> = Id::from_ptr(msg_send![first_path, path]);
|
||||
Some(path.as_str().to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StorageConfig> for PartialStorageConfig {
|
||||
fn from(config: StorageConfig) -> PartialStorageConfig {
|
||||
Self {
|
||||
data_directory: Some(config.data_directory),
|
||||
cache_directory: Some(config.cache_directory),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Emptiable for PartialStorageConfig {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
data_directory: None,
|
||||
cache_directory: None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.data_directory.is_none() && self.cache_directory.is_none()
|
||||
}
|
||||
}
|
||||
impl Mergeable for PartialStorageConfig {
|
||||
fn merge(&mut self, mut other: Self) {
|
||||
if let Some(x) = other.data_directory.take() {
|
||||
let _ = self.data_directory.insert(x);
|
||||
};
|
||||
if let Some(x) = other.cache_directory.take() {
|
||||
let _ = self.cache_directory.insert(x);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Mergeable for Option<PartialStorageConfig> {
|
||||
fn merge(&mut self, mut other: Self) {
|
||||
match other.take() {
|
||||
Some(x) => {
|
||||
if let Some(y) = self.as_mut() {
|
||||
y.merge(x);
|
||||
} else {
|
||||
let _ = self.insert(x);
|
||||
}
|
||||
},
|
||||
None => {}
|
||||
};
|
||||
}
|
||||
}
|
35
core/src/data/entity/mod.rs
Normal file
35
core/src/data/entity/mod.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
mod trusted_node;
|
||||
mod record_deletion;
|
||||
|
||||
pub use trusted_node::{
|
||||
ActiveModel as TrustedNodeActiveModel,
|
||||
Column as TrustedNodeColumn,
|
||||
Entity as TrustedNodeEntity,
|
||||
Model as TrustedNodeModel,
|
||||
};
|
||||
|
||||
pub use record_deletion::{
|
||||
ActiveModel as RecordDeletionActiveModel,
|
||||
Column as RecordDeletionColumn,
|
||||
Entity as RecordDeletionEntity,
|
||||
Model as RecordDeletionModel,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{data::{migration::DataMigrator, value::PeerIdValue}, global::{generate_uuid, DATABASE_CONNECTIONS}, tests::TEST_CONFIG};
|
||||
|
||||
use super::*;
|
||||
|
||||
use libp2p::{identity, PeerId};
|
||||
use sea_orm::ActiveModelTrait;
|
||||
|
||||
#[tokio::test]
|
||||
async fn check_insert() {
|
||||
let db = DATABASE_CONNECTIONS.get_or_init_unchecked(&*TEST_CONFIG, DataMigrator).await.cache;
|
||||
|
||||
let node = TrustedNodeActiveModel::new(PeerId::random(), "test note".to_owned()).insert(db).await.unwrap();
|
||||
let _ = RecordDeletionActiveModel::new(node.id, "test_table".to_string(), generate_uuid()).insert(db).await.unwrap();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,18 +1,23 @@
|
|||
use chrono::Local;
|
||||
use sea_orm::entity::{
|
||||
*,
|
||||
prelude::*
|
||||
};
|
||||
use sea_orm::{entity::{
|
||||
prelude::*, *
|
||||
}, sea_query::table};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::data::syncable::*;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature="macros", derive(SyncableModel))]
|
||||
#[sea_orm(table_name = "record_deletion")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
#[cfg_attr(feature="macros", syncable(id))]
|
||||
pub id: Uuid,
|
||||
#[sea_orm(indexed)]
|
||||
#[cfg_attr(feature="macros", syncable(timestamp))]
|
||||
pub created_at: DateTimeUtc,
|
||||
#[cfg_attr(feature="macros", syncable(author_id))]
|
||||
pub created_by: Uuid,
|
||||
pub table_name: String,
|
||||
pub record_id: Uuid,
|
||||
}
|
||||
|
@ -23,32 +28,15 @@ pub enum Relation{}
|
|||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl ActiveModel {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(node_id: Uuid, table_name: String, record_id: Uuid) -> Self {
|
||||
let timestamp: DateTimeUtc = Local::now().to_utc();
|
||||
Self{
|
||||
id: Set(super::generate_uuid()),
|
||||
id: Set(crate::global::generate_uuid()),
|
||||
created_at: Set(timestamp),
|
||||
..Default::default()
|
||||
created_by: Set(node_id),
|
||||
table_name: Set(table_name),
|
||||
record_id: Set(record_id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use uuid::{Timestamp, Uuid};
|
||||
use crate::global::get_or_init_temporary_main_database;
|
||||
|
||||
#[tokio::test]
|
||||
async fn check_insert_record_deletion() {
|
||||
let db = get_or_init_temporary_main_database().await;
|
||||
|
||||
assert!(ActiveModel{
|
||||
table_name: Set("test_table".to_string()),
|
||||
record_id: Set(super::super::generate_uuid()),
|
||||
..ActiveModel::new()
|
||||
}.insert(db).await.is_ok());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,16 @@
|
|||
use chrono::Local;
|
||||
use libp2p::PeerId;
|
||||
use sea_orm::entity::{
|
||||
*,
|
||||
prelude::*
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::data::value::PeerIdValue;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "node")]
|
||||
#[sea_orm(table_name = "trusted_node")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
|
@ -18,9 +21,10 @@ pub struct Model {
|
|||
#[sea_orm(indexed)]
|
||||
pub synced_at: Option<DateTimeUtc>,
|
||||
#[sea_orm(indexed)]
|
||||
pub peer_id: String,
|
||||
pub peer_id: PeerIdValue,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub note: String,
|
||||
pub is_prefered: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)]
|
||||
|
@ -29,33 +33,17 @@ pub enum Relation {}
|
|||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl ActiveModel {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(peer_id: PeerId, note: String) -> Self {
|
||||
let timestamp: DateTimeUtc = Local::now().to_utc();
|
||||
Self{
|
||||
id: Set(super::generate_uuid()),
|
||||
id: Set(crate::global::generate_uuid()),
|
||||
peer_id: Set(PeerIdValue::from(peer_id)),
|
||||
created_at: Set(timestamp),
|
||||
updated_at: Set(timestamp),
|
||||
..Default::default()
|
||||
synced_at: Set(None),
|
||||
is_prefered: Set(false),
|
||||
note: Set(note),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use libp2p::identity;
|
||||
use crate::global::GLOBAL;
|
||||
|
||||
#[tokio::test]
|
||||
async fn check_insert_node() {
|
||||
let db = crate::global::get_or_init_temporary_main_database().await;
|
||||
|
||||
ActiveModel{
|
||||
peer_id: Set(identity::Keypair::generate_ed25519().public().to_peer_id().to_string()),
|
||||
note: Set("test note".to_owned()),
|
||||
..ActiveModel::new()
|
||||
}.insert(db).await.unwrap();
|
||||
}
|
||||
|
||||
}
|
|
@ -8,20 +8,20 @@ pub struct Migration;
|
|||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
Node::up(manager).await?;
|
||||
TrustedNode::up(manager).await?;
|
||||
RecordDeletion::up(manager).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
Node::down(manager).await?;
|
||||
TrustedNode::down(manager).await?;
|
||||
RecordDeletion::down(manager).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Node {
|
||||
enum TrustedNode {
|
||||
Table,
|
||||
Id,
|
||||
CreatedAt,
|
||||
|
@ -29,10 +29,11 @@ enum Node {
|
|||
SyncedAt,
|
||||
PeerId,
|
||||
Note,
|
||||
IsPrefered,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl TableMigration for Node {
|
||||
impl TableMigration for TrustedNode {
|
||||
async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
|
||||
manager.create_table(
|
||||
Table::create()
|
||||
|
@ -44,6 +45,7 @@ impl TableMigration for Node {
|
|||
.col(timestamp_null(Self::SyncedAt))
|
||||
.col(string_len(Self::PeerId, 255))
|
||||
.col(text(Self::Note))
|
||||
.col(boolean(Self::IsPrefered))
|
||||
.to_owned()
|
||||
).await?;
|
||||
Ok(())
|
||||
|
@ -60,10 +62,13 @@ enum RecordDeletion {
|
|||
Table,
|
||||
Id,
|
||||
CreatedAt,
|
||||
CreatedBy,
|
||||
TableName,
|
||||
RecordId,
|
||||
}
|
||||
|
||||
static FK_RECORD_DELETION_TRUSTED_NODE: &str = "fk_record_deletion_trusted_node";
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl TableMigration for RecordDeletion {
|
||||
async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
|
||||
|
@ -73,6 +78,14 @@ impl TableMigration for RecordDeletion {
|
|||
.if_not_exists()
|
||||
.col(pk_uuid(Self::Id))
|
||||
.col(timestamp_with_time_zone(Self::CreatedAt))
|
||||
.col(uuid(Self::CreatedBy))
|
||||
.foreign_key(ForeignKey::create()
|
||||
.name(FK_RECORD_DELETION_TRUSTED_NODE)
|
||||
.from(Self::Table,Self::CreatedBy)
|
||||
.to(TrustedNode::Table, TrustedNode::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.on_update(ForeignKeyAction::Cascade)
|
||||
)
|
||||
.col(string(Self::TableName))
|
||||
.col(uuid(Self::RecordId))
|
||||
.to_owned()
|
|
@ -3,11 +3,11 @@ use sea_orm_migration::prelude::*;
|
|||
pub mod m20220101_000001_create_main_tables;
|
||||
|
||||
#[cfg(any(test, feature="test"))]
|
||||
pub struct MainMigrator;
|
||||
pub struct DataMigrator;
|
||||
|
||||
#[cfg(any(test, feature="test"))]
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for MainMigrator {
|
||||
impl MigratorTrait for DataMigrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![Box::new(m20220101_000001_create_main_tables::Migration)]
|
||||
}
|
4
core/src/data/mod.rs
Normal file
4
core/src/data/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
pub mod entity;
|
||||
pub mod migration;
|
||||
pub mod syncable;
|
||||
pub mod value;
|
79
core/src/data/syncable.rs
Normal file
79
core/src/data/syncable.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
use sea_orm::{prelude::*, query::*, sea_query::SimpleExpr, *};
|
||||
#[cfg(feature="macros")]
|
||||
pub use caretta_sync_macros::SyncableModel;
|
||||
pub trait SyncableModel: ModelTrait<Entity = Self::SyncableEntity> {
|
||||
type SyncableEntity: SyncableEntity<SyncableModel = Self>;
|
||||
fn get_timestamp(&self) -> DateTimeUtc;
|
||||
fn get_id(&self) -> Uuid;
|
||||
fn get_author_id(&self) -> Uuid;
|
||||
}
|
||||
|
||||
pub trait SyncableEntity: EntityTrait<
|
||||
Model = Self::SyncableModel,
|
||||
ActiveModel = Self::SyncableActiveModel,
|
||||
Column = Self::SyncableColumn,
|
||||
>{
|
||||
type SyncableModel: SyncableModel<SyncableEntity = Self> + FromQueryResult;
|
||||
type SyncableActiveModel: SyncableActiveModel<SyncableEntity= Self>;
|
||||
type SyncableColumn: SyncableColumn;
|
||||
|
||||
async fn get_updated(from: DateTimeUtc, db: &DatabaseConnection) -> Result<Vec<<Self as EntityTrait>::Model>, SyncableError> {
|
||||
let result: Vec<Self::SyncableModel> = <Self as EntityTrait>::find()
|
||||
.filter(Self::SyncableColumn::timestamp_after(from))
|
||||
.all(db)
|
||||
.await.unwrap();
|
||||
Ok(result)
|
||||
}
|
||||
async fn get_updated_by(author: Uuid, from: DateTimeUtc, db: &DatabaseConnection) -> Result<Vec<<Self as EntityTrait>::Model>, SyncableError> {
|
||||
let result: Vec<Self::SyncableModel> = <Self as EntityTrait>::find()
|
||||
.filter(Self::SyncableColumn::timestamp_after(from))
|
||||
.filter(Self::SyncableColumn::author_id_eq(author))
|
||||
.all(db)
|
||||
.await.unwrap();
|
||||
Ok(result)
|
||||
}
|
||||
fn apply_updated(models: Vec<<Self as EntityTrait>::Model>, db: &DatabaseConnection) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SyncableActiveModel: ActiveModelTrait<Entity = Self::SyncableEntity> {
|
||||
|
||||
type SyncableEntity: SyncableEntity<SyncableActiveModel = Self>;
|
||||
fn get_id(&self) -> Option<Uuid>;
|
||||
fn get_timestamp(&self) -> Option<DateTimeUtc>;
|
||||
fn get_author_id(&self) -> Option<Uuid>;
|
||||
fn try_merge(&mut self, other: <Self::SyncableEntity as SyncableEntity>::SyncableModel) -> Result<(), SyncableError> {
|
||||
if self.get_id().ok_or(SyncableError::MissingField("uuid"))? != other.get_id() {
|
||||
return Err(SyncableError::MismatchUuid)
|
||||
}
|
||||
if self.get_timestamp().ok_or(SyncableError::MissingField("updated_at"))? < other.get_timestamp() {
|
||||
for column in <<<Self as ActiveModelTrait>::Entity as EntityTrait>::Column as Iterable>::iter() {
|
||||
if column.should_synced(){
|
||||
self.take(column).set_if_not_equals(other.get(column));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub trait SyncableColumn: ColumnTrait {
|
||||
fn is_id(&self) -> bool;
|
||||
fn is_timestamp(&self) -> bool;
|
||||
fn should_synced(&self) -> bool;
|
||||
fn timestamp_after(from: DateTimeUtc) -> SimpleExpr;
|
||||
fn author_id_eq(author_id: Uuid) -> SimpleExpr;
|
||||
fn is_author_id(&self) -> bool;
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SyncableError {
|
||||
#[error("Invalid UUID")]
|
||||
MismatchUuid,
|
||||
#[error("mandatory field {0} is missing")]
|
||||
MissingField(&'static str),
|
||||
|
||||
}
|
5
core/src/data/value/mod.rs
Normal file
5
core/src/data/value/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod multiaddr;
|
||||
mod peer_id;
|
||||
|
||||
pub use multiaddr::MultiaddrValue;
|
||||
pub use peer_id::PeerIdValue;
|
68
core/src/data/value/multiaddr.rs
Normal file
68
core/src/data/value/multiaddr.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use libp2p::Multiaddr;
|
||||
use sea_orm::{sea_query::ValueTypeErr, DbErr};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
pub struct MultiaddrValue(Multiaddr);
|
||||
|
||||
impl From<Multiaddr> for MultiaddrValue {
|
||||
fn from(source: Multiaddr) -> Self {
|
||||
Self(source)
|
||||
}
|
||||
}
|
||||
impl From<MultiaddrValue> for Multiaddr {
|
||||
fn from(source: MultiaddrValue) -> Self {
|
||||
source.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MultiaddrValue> for sea_orm::Value {
|
||||
fn from(value: MultiaddrValue) -> Self {
|
||||
Self::from(value.0.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::TryGetable for MultiaddrValue {
|
||||
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, idx: I)
|
||||
-> std::result::Result<Self, sea_orm::TryGetError> {
|
||||
match <String as sea_orm::TryGetable>::try_get_by(res, idx){
|
||||
Ok(x) => match Multiaddr::from_str(&x) {
|
||||
Ok(y) => Ok(Self(y)),
|
||||
Err(_) => Err(DbErr::Type("Multiaddr".to_string()).into()),
|
||||
},
|
||||
Err(x) => Err(x),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::sea_query::ValueType for MultiaddrValue {
|
||||
fn try_from(v: sea_orm::Value) -> std::result::Result<Self, sea_orm::sea_query::ValueTypeErr> {
|
||||
match <String as sea_orm::sea_query::ValueType>::try_from(v) {
|
||||
Ok(x) => match Multiaddr::from_str(&x) {
|
||||
Ok(y) => Ok(Self(y)),
|
||||
Err(_) => Err(ValueTypeErr{}),
|
||||
},
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
|
||||
fn type_name() -> std::string::String {
|
||||
stringify!(MultiaddrValue).to_owned()
|
||||
}
|
||||
|
||||
fn array_type() -> sea_orm::sea_query::ArrayType {
|
||||
sea_orm::sea_query::ArrayType::String
|
||||
}
|
||||
|
||||
fn column_type() -> sea_orm::sea_query::ColumnType {
|
||||
sea_orm::sea_query::ColumnType::Text
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::sea_query::Nullable for MultiaddrValue {
|
||||
fn null() -> sea_orm::Value {
|
||||
<String as sea_orm::sea_query::Nullable>::null()
|
||||
}
|
||||
}
|
101
core/src/data/value/peer_id.rs
Normal file
101
core/src/data/value/peer_id.rs
Normal file
|
@ -0,0 +1,101 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use libp2p::PeerId;
|
||||
use sea_orm::{sea_query::ValueTypeErr, DbErr};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PeerIdValue(PeerId);
|
||||
|
||||
impl<'de> Deserialize<'de> for PeerIdValue {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de> {
|
||||
Self::from_str(&String::deserialize(deserializer)?).or(Err(<D::Error as serde::de::Error>::custom("fail to parse PeerId")))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for PeerIdValue {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer {
|
||||
serializer.serialize_str(&self.0.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for PeerIdValue{
|
||||
type Err = libp2p::identity::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Self(PeerId::from_str(s)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for PeerIdValue {
|
||||
fn to_string(&self) -> String {
|
||||
self.0.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PeerId> for PeerIdValue {
|
||||
fn from(source: PeerId) -> Self {
|
||||
Self(source)
|
||||
}
|
||||
}
|
||||
impl From<PeerIdValue> for PeerId {
|
||||
fn from(source: PeerIdValue) -> Self {
|
||||
source.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PeerIdValue> for sea_orm::Value {
|
||||
fn from(value: PeerIdValue) -> Self {
|
||||
Self::from(value.0.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::TryGetable for PeerIdValue {
|
||||
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, idx: I)
|
||||
-> std::result::Result<Self, sea_orm::TryGetError> {
|
||||
match <String as sea_orm::TryGetable>::try_get_by(res, idx){
|
||||
Ok(x) => match PeerId::from_str(&x) {
|
||||
Ok(y) => Ok(Self(y)),
|
||||
Err(_) => Err(DbErr::Type("PeerId".to_string()).into()),
|
||||
},
|
||||
Err(x) => Err(x),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::sea_query::ValueType for PeerIdValue {
|
||||
fn try_from(v: sea_orm::Value) -> std::result::Result<Self, sea_orm::sea_query::ValueTypeErr> {
|
||||
match <String as sea_orm::sea_query::ValueType>::try_from(v) {
|
||||
Ok(x) => match PeerId::from_str(&x) {
|
||||
Ok(y) => Ok(Self(y)),
|
||||
Err(_) => Err(ValueTypeErr{}),
|
||||
},
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
|
||||
fn type_name() -> std::string::String {
|
||||
stringify!(PeerIdValue).to_owned()
|
||||
}
|
||||
|
||||
fn array_type() -> sea_orm::sea_query::ArrayType {
|
||||
sea_orm::sea_query::ArrayType::String
|
||||
}
|
||||
|
||||
fn column_type() -> sea_orm::sea_query::ColumnType {
|
||||
sea_orm::sea_query::ColumnType::Text
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::sea_query::Nullable for PeerIdValue {
|
||||
fn null() -> sea_orm::Value {
|
||||
<String as sea_orm::sea_query::Nullable>::null()
|
||||
}
|
||||
}
|
|
@ -1,7 +1,15 @@
|
|||
use std::ffi::OsString;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Base64 decode error: {0}")]
|
||||
Base64Decode(#[from] base64::DecodeError),
|
||||
#[error(transparent)]
|
||||
CiborDeserialize(#[from] ciborium::de::Error<std::io::Error>),
|
||||
#[error(transparent)]
|
||||
CiborSerialize(#[from] ciborium::ser::Error<std::io::Error>),
|
||||
#[error("Config error: {0}")]
|
||||
Config(#[from] crate::config::error::ConfigError),
|
||||
#[error("DB Error: {0}")]
|
||||
Db(#[from]sea_orm::DbErr),
|
||||
#[error("Dial Error: {0}")]
|
||||
|
@ -18,10 +26,21 @@ pub enum Error {
|
|||
Multiaddr(#[from] libp2p::multiaddr::Error),
|
||||
#[error("Noise error: {0}")]
|
||||
Noise(#[from] libp2p::noise::Error),
|
||||
#[error("Parse OsString error: {0:?}")]
|
||||
OsStringConvert(std::ffi::OsString),
|
||||
#[cfg(feature="cli")]
|
||||
#[error("Parse args error: {0}")]
|
||||
ParseCommand(#[from] clap::Error),
|
||||
#[error("toml deserialization error: {0}")]
|
||||
TomlDe(#[from] toml::de::Error),
|
||||
#[error("toml serialization error: {0}")]
|
||||
TomlSer(#[from] toml::ser::Error),
|
||||
TomlSer(#[from] toml::ser::Error),
|
||||
#[error("Transport error: {0}")]
|
||||
Transport(#[from]libp2p::TransportError<std::io::Error>)
|
||||
}
|
||||
|
||||
impl From<std::ffi::OsString> for Error {
|
||||
fn from(s: OsString) -> Error {
|
||||
Self::OsStringConvert(s)
|
||||
}
|
||||
}
|
38
core/src/global/config.rs
Normal file
38
core/src/global/config.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
#[cfg(any(test,feature="test"))]
|
||||
use tempfile::TempDir;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
use crate::{config::{Config, ConfigError, PartialP2pConfig, PartialRpcConfig, PartialStorageConfig, StorageConfig}, error::Error};
|
||||
|
||||
pub static CONFIG: GlobalConfig = GlobalConfig::const_new();
|
||||
pub struct GlobalConfig {
|
||||
inner: OnceCell<Config>
|
||||
}
|
||||
|
||||
impl GlobalConfig {
|
||||
pub const fn const_new() -> Self {
|
||||
Self{
|
||||
inner: OnceCell::const_new()
|
||||
}
|
||||
}
|
||||
pub async fn get_or_init<T>(&'static self, config: Config) -> &'static Config where
|
||||
T: Into<Config>{
|
||||
self.inner.get_or_init(|| async {
|
||||
config.into()
|
||||
}).await
|
||||
}
|
||||
pub async fn get_or_try_init<T, E>(&'static self, config: T) -> Result<&'static Config, <T as TryInto<Config>>::Error> where
|
||||
T: TryInto<Config>,
|
||||
{
|
||||
self.inner.get_or_try_init(|| async {
|
||||
config.try_into()
|
||||
}).await
|
||||
|
||||
}
|
||||
pub fn get(&'static self) -> Option<&'static Config> {
|
||||
self.inner.get()
|
||||
}
|
||||
pub fn get_unchecked(&'static self) -> &'static Config {
|
||||
self.get().expect("Config must be initialized before use!")
|
||||
}
|
||||
}
|
121
core/src/global/database_connection.rs
Normal file
121
core/src/global/database_connection.rs
Normal file
|
@ -0,0 +1,121 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use dirs::cache_dir;
|
||||
use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection};
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
use crate::{cache::migration::CacheMigrator, config::StorageConfig, error::Error};
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
pub static DATABASE_CONNECTIONS: GlobalDatabaseConnections = GlobalDatabaseConnections::const_new();
|
||||
|
||||
pub struct DatabaseConnections<'a> {
|
||||
pub data: &'a DatabaseConnection,
|
||||
pub cache: &'a DatabaseConnection
|
||||
}
|
||||
|
||||
pub struct GlobalDatabaseConnections {
|
||||
data: OnceCell<DatabaseConnection>,
|
||||
cache: OnceCell<DatabaseConnection>,
|
||||
}
|
||||
|
||||
impl GlobalDatabaseConnections {
|
||||
pub const fn const_new() -> Self {
|
||||
Self {
|
||||
data: OnceCell::const_new(),
|
||||
cache: OnceCell::const_new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_data(&'static self) -> Option<&'static DatabaseConnection> {
|
||||
self.data.get()
|
||||
}
|
||||
|
||||
pub fn get_data_unchecked(&'static self) -> &'static DatabaseConnection {
|
||||
self.get_data().expect("Global data database connection should initialized before access!")
|
||||
}
|
||||
|
||||
pub fn get_cache(&'static self) -> Option<&'static DatabaseConnection> {
|
||||
self.cache.get()
|
||||
}
|
||||
|
||||
pub fn get_cache_unchecked(&'static self) -> &'static DatabaseConnection {
|
||||
self.get_cache().expect("Global cache database connection should initialized before access!")
|
||||
}
|
||||
|
||||
fn get_data_file_path<T>(config: &T) -> PathBuf
|
||||
where
|
||||
T: AsRef<StorageConfig>
|
||||
{
|
||||
config.as_ref().data_directory.join("data.sqlite")
|
||||
}
|
||||
|
||||
fn get_cache_file_path<T>(config: &T) -> PathBuf
|
||||
where
|
||||
T: AsRef<StorageConfig>
|
||||
{
|
||||
config.as_ref().cache_directory.join("cache.sqlite")
|
||||
}
|
||||
|
||||
fn get_url_unchecked<T>(path: T) -> String
|
||||
where
|
||||
T: AsRef<Path>
|
||||
{
|
||||
"sqlite://".to_string() + path.as_ref().as_os_str().to_str().expect("Failed to convert path to string!") + "?mode=rwc"
|
||||
}
|
||||
|
||||
async fn get_or_init_database_connection_unchecked<T, U>(cell: &OnceCell<DatabaseConnection>, options: T, _: U ) -> &DatabaseConnection
|
||||
where
|
||||
T: Into<ConnectOptions>,
|
||||
U: MigratorTrait
|
||||
{
|
||||
cell.get_or_init(|| async {
|
||||
let db = Database::connect(options.into()).await.unwrap();
|
||||
U::up(&db, None).await.unwrap();
|
||||
db
|
||||
}).await
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_or_init_unchecked<T, U>(&'static self, config: T, _migrator: U) -> DatabaseConnections
|
||||
where
|
||||
T: AsRef<StorageConfig>,
|
||||
U: MigratorTrait,
|
||||
{
|
||||
let data_path = Self::get_data_file_path(&config);
|
||||
if let Some(x) = data_path.parent() {
|
||||
std::fs::create_dir_all(x).expect("Failed to create directory for data database");
|
||||
}
|
||||
let cache_path = Self::get_cache_file_path(&config);
|
||||
if let Some(x) = cache_path.parent() {
|
||||
std::fs::create_dir_all(x).expect("Failed to create directory for cache database");
|
||||
}
|
||||
DatabaseConnections{
|
||||
data: Self::get_or_init_database_connection_unchecked(
|
||||
&self.data,
|
||||
Self::get_url_unchecked(data_path),
|
||||
_migrator
|
||||
).await,
|
||||
cache: Self::get_or_init_database_connection_unchecked(
|
||||
&self.cache,
|
||||
Self::get_url_unchecked(cache_path),
|
||||
CacheMigrator
|
||||
).await,
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub use tests::*;
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{cache::migration::CacheMigrator, data::migration::DataMigrator, global::CONFIG, tests::{TEST_CONFIG}};
|
||||
|
||||
#[tokio::test]
|
||||
pub async fn get_or_init_database() {
|
||||
DATABASE_CONNECTIONS.get_or_init_unchecked(&*TEST_CONFIG, DataMigrator).await;
|
||||
}
|
||||
}
|
24
core/src/global/mod.rs
Normal file
24
core/src/global/mod.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use std::{any::type_name, collections::HashMap, net::{IpAddr, Ipv4Addr}, path::{Path, PathBuf}, sync::LazyLock};
|
||||
|
||||
use crate::{config::{P2pConfig, PartialP2pConfig, StorageConfig}, error::Error };
|
||||
use libp2p::{swarm::SwarmEvent, Multiaddr, PeerId};
|
||||
use sea_orm::{prelude::*, Database};
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
use tokio::sync::{OnceCell, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
mod config;
|
||||
pub use config::*;
|
||||
mod database_connection;
|
||||
pub use database_connection::*;
|
||||
use uuid::{ContextV7, Timestamp, Uuid};
|
||||
|
||||
pub fn generate_uuid() -> Uuid {
|
||||
Uuid::new_v7(Timestamp::now(ContextV7::new()))
|
||||
}
|
||||
|
||||
pub static DEFAULT_LISTEN_IPS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))];
|
||||
|
||||
|
||||
fn uninitialized_message<T>(var: T) -> String {
|
||||
format!("{} is uninitialized!", &stringify!(var))
|
||||
}
|
|
@ -3,7 +3,12 @@ pub mod config;
|
|||
pub mod data;
|
||||
pub mod error;
|
||||
pub mod global;
|
||||
pub mod message;
|
||||
pub mod migration;
|
||||
pub mod p2p;
|
||||
pub mod proto;
|
||||
pub mod rpc;
|
||||
#[cfg(any(test, feature="test"))]
|
||||
pub mod tests;
|
||||
pub mod utils;
|
||||
pub mod server;
|
52
core/src/message/mod.rs
Normal file
52
core/src/message/mod.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
mod node;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{utils::async_convert::{AsyncTryFrom, AsyncTryInto}, error::Error};
|
||||
|
||||
pub trait Message: DeserializeOwned + Sized + Serialize {
|
||||
fn into_writer<W: std::io::Write>(&self, writer: W) -> Result<(), ciborium::ser::Error<std::io::Error>> {
|
||||
ciborium::into_writer(self, writer)
|
||||
}
|
||||
fn into_vec_u8(&self) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>> {
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
self.into_writer(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
fn from_reader<R: std::io::Read>(reader: R) -> Result<Self, ciborium::de::Error<std::io::Error>> {
|
||||
ciborium::from_reader(reader)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Request<T>: Into<T> + From<T> + AsyncTryInto<Self::Response>
|
||||
where T: Message {
|
||||
type Response: Response<T, Request = Self>;
|
||||
async fn send_p2p(self) -> Result<Self::Response, Error>;
|
||||
}
|
||||
|
||||
pub trait Response<T>: Into<T> + From<T> + AsyncTryFrom<Self::Request>
|
||||
where T: Message{
|
||||
type Request: Request<T, Response = Self>;
|
||||
async fn from_request_with_local(req: Self::Request) -> Result<Self,Error>;
|
||||
async fn from_request_with_p2p(req: Self::Request) -> Result<Self, Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromDatabase {
|
||||
async fn from_storage();
|
||||
}
|
||||
|
||||
|
||||
pub trait P2pRequest<T>: Into<T> + From<T>
|
||||
where T: Message {
|
||||
type P2pResponse: P2pResponse<T, P2pRequest = Self>;
|
||||
async fn send_p2p(&self) -> Result<Self::P2pResponse, crate::p2p::error::P2pError>{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
pub trait P2pResponse<T>: Into<T> + From<T> + AsyncTryFrom<(Self::P2pRequest)>
|
||||
where T: Message {
|
||||
type P2pRequest: P2pRequest<T, P2pResponse = Self>;
|
||||
async fn try_from_p2p_request(source: Self::P2pRequest) -> Result<Self, crate::p2p::error::P2pError>;
|
||||
}
|
10
core/src/message/node.rs
Normal file
10
core/src/message/node.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ListTrustedNodeRequest;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ListTrustedNodeResponse {
|
||||
node: Vec<crate::data::entity::TrustedNodeModel>
|
||||
}
|
4
core/src/p2p/error.rs
Normal file
4
core/src/p2p/error.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum P2pError {
|
||||
|
||||
}
|
104
core/src/p2p/mod.rs
Normal file
104
core/src/p2p/mod.rs
Normal file
|
@ -0,0 +1,104 @@
|
|||
pub mod error;
|
||||
use chrono::Local;
|
||||
use libp2p::{ identity::Keypair, mdns, ping, swarm, Multiaddr, PeerId};
|
||||
use sea_orm::{prelude::DateTimeUtc, ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, ModelTrait, QueryFilter};
|
||||
use tracing::{event, Level};
|
||||
|
||||
use crate::{cache::entity::{CachedPeerActiveModel, CachedAddressActiveModel, CachedAddressColumn, CachedAddressEntity, CachedAddressModel, CachedPeerColumn, CachedPeerEntity, CachedPeerModel}, data::value::{MultiaddrValue, PeerIdValue}, error::Error, global::DATABASE_CONNECTIONS};
|
||||
|
||||
#[derive(swarm::NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "Event")]
|
||||
pub struct Behaviour {
|
||||
pub mdns: mdns::tokio::Behaviour,
|
||||
pub ping: ping::Behaviour,
|
||||
}
|
||||
|
||||
impl TryFrom<&Keypair> for Behaviour {
|
||||
type Error = Error;
|
||||
fn try_from(keypair: &Keypair) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
mdns: mdns::tokio::Behaviour::new(
|
||||
mdns::Config::default(),
|
||||
keypair.public().into(),
|
||||
)?,
|
||||
ping: libp2p::ping::Behaviour::new(ping::Config::new()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Event {
|
||||
Mdns(mdns::Event),
|
||||
Ping(ping::Event),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub async fn run(&self)
|
||||
{
|
||||
match self {
|
||||
Self::Mdns(x) => {
|
||||
match x {
|
||||
mdns::Event::Discovered(e) => {
|
||||
for peer in e.iter() {
|
||||
event!(Level::TRACE, "Peer discovered via mdns: {}, {}", &peer.0, &peer.1);
|
||||
match try_get_or_insert_cached_peer(&peer.0, &peer.1).await {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
event!(Level::WARN, "{:?}", e);
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<mdns::Event> for Event {
|
||||
fn from(event: mdns::Event) -> Self {
|
||||
Self::Mdns(event)
|
||||
}
|
||||
}
|
||||
impl From<ping::Event> for Event {
|
||||
fn from(event: ping::Event) -> Self {
|
||||
Self::Ping(event)
|
||||
}
|
||||
}
|
||||
|
||||
async fn try_get_or_insert_cached_peer(peer_id: &PeerId, peer_addr: &Multiaddr) -> Result<(CachedPeerModel, CachedAddressModel), Error> {
|
||||
match (
|
||||
CachedPeerEntity::find().filter(CachedPeerColumn::PeerId.eq(PeerIdValue::from(peer_id.clone()))).one(DATABASE_CONNECTIONS.get_cache_unchecked()).await?,
|
||||
CachedAddressEntity::find().filter(CachedAddressColumn::Multiaddress.eq(MultiaddrValue::from(peer_addr.clone()))).one(DATABASE_CONNECTIONS.get_cache_unchecked()).await?,
|
||||
) {
|
||||
(Some(x), Some(y) ) => {
|
||||
if x.id == y.cached_peer_id {
|
||||
event!(Level::TRACE, "Known peer: {}, {}", peer_id, peer_addr);
|
||||
let mut addr: CachedAddressActiveModel = y.into();
|
||||
addr.updated_at = Set(Local::now().to_utc());
|
||||
let updated = addr.update(DATABASE_CONNECTIONS.get_cache_unchecked()).await?;
|
||||
Ok((x, updated))
|
||||
} else {
|
||||
y.delete(DATABASE_CONNECTIONS.get_cache().expect("Cache database should initialized beforehand!")).await?;
|
||||
Ok((x.clone(), CachedAddressActiveModel::new(x.id, peer_addr.clone()).insert(DATABASE_CONNECTIONS.get_cache_unchecked()).await?))
|
||||
}
|
||||
}
|
||||
(Some(x), None) => {
|
||||
event!(Level::INFO, "New address {} for {}", peer_addr, peer_id);
|
||||
Ok((x.clone(),CachedAddressActiveModel::new(x.id, peer_addr.clone()).insert(DATABASE_CONNECTIONS.get_cache_unchecked()).await?))
|
||||
},
|
||||
(None, x) => {
|
||||
event!(Level::INFO, "Add new peer: {}", peer_id);
|
||||
let inserted = CachedPeerActiveModel::new(peer_id.clone()).insert(DATABASE_CONNECTIONS.get_cache_unchecked()).await?;
|
||||
if let Some(y) = x {
|
||||
event!(Level::INFO, "Remove {} from {}", peer_addr, peer_id);
|
||||
y.delete(DATABASE_CONNECTIONS.get_cache_unchecked()).await?;
|
||||
};
|
||||
event!(Level::INFO, "Add address {} to {}", peer_addr, peer_id);
|
||||
Ok((inserted.clone(), CachedAddressActiveModel::new(inserted.id, peer_addr.clone()).insert(DATABASE_CONNECTIONS.get_cache_unchecked()).await?))
|
||||
},
|
||||
|
||||
|
||||
}
|
||||
}
|
16
core/src/proto/cached_address.rs
Normal file
16
core/src/proto/cached_address.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
use libp2p::Multiaddr;
|
||||
|
||||
use crate::cache::entity::CachedAddressModel;
|
||||
use crate::utils::utc_to_timestamp;
|
||||
use crate::proto::CachedAddressMessage;
|
||||
|
||||
impl From<&CachedAddressModel> for CachedAddressMessage {
|
||||
fn from(a: &CachedAddressModel) -> Self {
|
||||
Self {
|
||||
number: a.id,
|
||||
created_at: Some(utc_to_timestamp(&a.created_at)),
|
||||
updated_at: Some(utc_to_timestamp(&a.updated_at)),
|
||||
multiaddress: Multiaddr::from(a.multiaddress.clone()).to_string(),
|
||||
}
|
||||
}
|
||||
}
|
14
core/src/proto/cached_peer.rs
Normal file
14
core/src/proto/cached_peer.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use crate::{cache::entity::{CachedAddressModel, CachedPeerModel}, proto::{CachedAddressMessage, CachedPeerMessage}, utils::utc_to_timestamp};
|
||||
|
||||
impl From<(&CachedPeerModel, &Vec<CachedAddressModel>)> for CachedPeerMessage {
|
||||
fn from(source: (&CachedPeerModel, &Vec<CachedAddressModel>)) -> Self {
|
||||
let (peer, addresses) = source;
|
||||
|
||||
Self {
|
||||
number: peer.id,
|
||||
peer_id: peer.peer_id.to_string(),
|
||||
created_at: Some(utc_to_timestamp(&peer.created_at)),
|
||||
addresses: addresses.iter().map(|x| CachedAddressMessage::from(x)).collect(),
|
||||
}
|
||||
}
|
||||
}
|
5
core/src/proto/mod.rs
Normal file
5
core/src/proto/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod cached_address;
|
||||
mod cached_peer;
|
||||
|
||||
tonic::include_proto!("caretta_sync");
|
||||
|
2
core/src/rpc/mod.rs
Normal file
2
core/src/rpc/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod service;
|
||||
|
30
core/src/rpc/service/cached_peer.rs
Normal file
30
core/src/rpc/service/cached_peer.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
use crate::{cache::entity::{CachedAddressEntity, CachedPeerEntity, CachedPeerModel}, global::{DATABASE_CONNECTIONS}, proto::CachedAddressMessage};
|
||||
use futures::future::join_all;
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
use crate::proto::{cached_peer_service_server::{CachedPeerServiceServer}, CachedPeerListRequest, CachedPeerListResponse, CachedPeerMessage};
|
||||
use sea_orm::prelude::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct CachedPeerService {}
|
||||
|
||||
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl crate::proto::cached_peer_service_server::CachedPeerService for CachedPeerService {
|
||||
async fn list(&self, request: Request<CachedPeerListRequest>) -> Result<Response<CachedPeerListResponse>, Status> {
|
||||
println!("Got a request: {:?}", request);
|
||||
|
||||
let reply = CachedPeerListResponse {
|
||||
peers: join_all( CachedPeerEntity::find().all(DATABASE_CONNECTIONS.get_cache_unchecked()).await.or_else(|e| Err(Status::from_error(Box::new(e))))?.iter().map(|x| async move {
|
||||
let addresses = CachedAddressEntity::find()
|
||||
.all(DATABASE_CONNECTIONS.get_cache_unchecked())
|
||||
.await
|
||||
.or_else(|e| Err(Status::from_error(Box::new(e))))?;
|
||||
Ok::<CachedPeerMessage, Status>(CachedPeerMessage::from((x, &addresses)))
|
||||
})).await.into_iter().collect::<Result<Vec<_>,_>>()?,
|
||||
};
|
||||
|
||||
Ok(Response::new(reply))
|
||||
}
|
||||
}
|
1
core/src/rpc/service/mod.rs
Normal file
1
core/src/rpc/service/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod cached_peer;
|
17
core/src/server.rs
Normal file
17
core/src/server.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use crate::{config::{Config, P2pConfig, RpcConfig}, error::Error};
|
||||
|
||||
pub trait ServerTrait {
|
||||
async fn serve_p2p<T>(config: &T) -> Result<(), Error>
|
||||
where T: AsRef<P2pConfig>;
|
||||
async fn serve_rpc<T>(config: &T) -> Result<(), Error>
|
||||
where T: AsRef<RpcConfig>;
|
||||
async fn serve_all<T>(config: &T) -> Result<(), Error>
|
||||
where
|
||||
T: AsRef<P2pConfig> + AsRef<RpcConfig> {
|
||||
tokio::try_join!(
|
||||
Self::serve_p2p(config),
|
||||
Self::serve_rpc(config)
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
26
core/src/tests.rs
Normal file
26
core/src/tests.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use std::{path::PathBuf, sync::LazyLock};
|
||||
|
||||
use sea_orm::{sea_query::{FromValueTuple, IntoValueTuple, ValueType}, ActiveModelBehavior, ActiveModelTrait, ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiveModel, ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, Value};
|
||||
use sea_orm::QueryFilter;
|
||||
use tempfile::TempDir;
|
||||
use crate::{ config::{Config, PartialConfig, PartialP2pConfig, PartialRpcConfig, RpcConfig, StorageConfig}, message::Message};
|
||||
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
|
||||
pub static TEST_CONFIG: LazyLock<Config> = LazyLock::new(|| {
|
||||
let test_dir = TempDir::new().unwrap().keep();
|
||||
let data_dir = test_dir.join("data");
|
||||
let cache_dir = test_dir.join("cache");
|
||||
|
||||
|
||||
Config {
|
||||
p2p: PartialP2pConfig::default().with_new_private_key().try_into().unwrap(),
|
||||
storage: StorageConfig {
|
||||
data_directory: data_dir,
|
||||
cache_directory: cache_dir,
|
||||
},
|
||||
rpc: RpcConfig{
|
||||
socket_path: test_dir.join("socket.sock"),
|
||||
},
|
||||
}
|
||||
});
|
29
core/src/utils/async_convert.rs
Normal file
29
core/src/utils/async_convert.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
pub trait AsyncFrom<T> {
|
||||
async fn async_from(source: T) -> Self;
|
||||
}
|
||||
pub trait AsyncInto<T> {
|
||||
async fn async_into(self) -> T;
|
||||
}
|
||||
impl<T, U> AsyncInto<T> for U
|
||||
where T: AsyncFrom<U> {
|
||||
async fn async_into(self) -> T {
|
||||
T::async_from(self).await
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsyncTryFrom<T>: Sized {
|
||||
type Error: Sized;
|
||||
async fn async_try_from(source: T) -> Result<Self, Self::Error>;
|
||||
}
|
||||
pub trait AsyncTryInto<T>: Sized{
|
||||
type Error: Sized;
|
||||
async fn async_try_into(self) -> Result<T, Self::Error>;
|
||||
}
|
||||
|
||||
impl<T, U> AsyncTryInto<T> for U
|
||||
where T: AsyncTryFrom<U> {
|
||||
type Error = <T as AsyncTryFrom<U>>::Error;
|
||||
async fn async_try_into(self) -> Result<T, Self::Error> {
|
||||
T::async_try_from(self).await
|
||||
}
|
||||
}
|
53
core/src/utils/emptiable.rs
Normal file
53
core/src/utils/emptiable.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
#[cfg(feature="macros")]
|
||||
pub use caretta_sync_macros::Emptiable;
|
||||
|
||||
pub trait Emptiable{
|
||||
fn empty() -> Self;
|
||||
fn is_empty(&self) -> bool;
|
||||
}
|
||||
|
||||
impl<T> Emptiable for Vec<T> {
|
||||
fn empty() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
fn is_empty(&self) -> bool {
|
||||
self.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Emptiable for Option<T> {
|
||||
fn empty() -> Self {
|
||||
None
|
||||
}
|
||||
fn is_empty(&self) -> bool {
|
||||
self.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl Emptiable for String {
|
||||
fn empty() -> Self {
|
||||
String::new()
|
||||
}
|
||||
fn is_empty(&self) -> bool {
|
||||
self.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> Emptiable for HashMap<T, U> {
|
||||
fn empty() -> Self {
|
||||
HashMap::new()
|
||||
}
|
||||
fn is_empty(&self) -> bool {
|
||||
self.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Emptiable for HashSet<T> {
|
||||
fn empty() -> Self {
|
||||
HashSet::new()
|
||||
}
|
||||
fn is_empty(&self) -> bool {
|
||||
self.is_empty()
|
||||
}
|
||||
}
|
5
core/src/utils/mergeable.rs
Normal file
5
core/src/utils/mergeable.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
#[cfg(feature="macros")]
|
||||
pub use caretta_sync_macros::Mergeable;
|
||||
pub trait Mergeable: Sized {
|
||||
fn merge(&mut self, other: Self);
|
||||
}
|
47
core/src/utils/mod.rs
Normal file
47
core/src/utils/mod.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use prost_types::Timestamp;
|
||||
use chrono::{DateTime, TimeZone, Timelike, Utc};
|
||||
pub mod async_convert;
|
||||
pub mod emptiable;
|
||||
pub mod mergeable;
|
||||
pub mod runnable;
|
||||
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// use chrono::Utc;
|
||||
/// use std::time::SystemTime;
|
||||
/// use prost_types::Timestamp;
|
||||
/// use caretta_sync_core::utils::utc_to_timestamp;
|
||||
///
|
||||
/// let now_utc = Utc::now();
|
||||
/// let now_timestamp = utc_to_timestamp(&now_utc);
|
||||
/// assert_eq!(SystemTime::try_from(now_utc).unwrap(), SystemTime::try_from(now_timestamp).unwrap());
|
||||
/// ```
|
||||
pub fn utc_to_timestamp(utc: &DateTime<Utc>) -> Timestamp {
|
||||
Timestamp{
|
||||
seconds: utc.timestamp(),
|
||||
nanos: i32::try_from(utc.nanosecond()).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// use std::time::SystemTime;
|
||||
/// use prost_types::Timestamp;
|
||||
/// use caretta_sync_core::utils::timestamp_to_utc;
|
||||
///
|
||||
/// let now_timestamp = Timestamp::from(SystemTime::now());
|
||||
/// let now_utc = timestamp_to_utc(&now_timestamp);
|
||||
/// assert_eq!(SystemTime::try_from(now_utc).unwrap(), SystemTime::try_from(now_timestamp).unwrap());
|
||||
/// ```
|
||||
pub fn timestamp_to_utc(t: &Timestamp) -> DateTime<Utc> {
|
||||
Utc.timestamp_opt(t.seconds, u32::try_from(t.nanos).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_binary_name() -> Option<String> {
|
||||
std::env::current_exe()
|
||||
.ok()?
|
||||
.file_name()?
|
||||
.to_str()?
|
||||
.to_owned()
|
||||
.into()
|
||||
}
|
3
core/src/utils/runnable.rs
Normal file
3
core/src/utils/runnable.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub trait Runnable {
|
||||
fn run(self, app_name: &'static str);
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "lazy-supplements-examples-core"
|
||||
name = "caretta-sync-example-core"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
|
@ -7,4 +7,9 @@ license.workspace = true
|
|||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
dioxus.workspace = true
|
||||
bevy.workspace = true
|
||||
caretta-sync = { path = "../..", features = ["bevy"] }
|
||||
libp2p.workspace = true
|
||||
tokio.workspace = true
|
||||
tokio-stream = { version = "0.1.17", features = ["net"] }
|
||||
tonic.workspace = true
|
||||
|
|
BIN
examples/core/assets/favicon.ico
(Stored with Git LFS)
BIN
examples/core/assets/favicon.ico
(Stored with Git LFS)
Binary file not shown.
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 23 KiB |
|
@ -1,46 +0,0 @@
|
|||
/* App-wide styling */
|
||||
body {
|
||||
background-color: #0f1116;
|
||||
color: #ffffff;
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
margin: 20px;
|
||||
}
|
||||
|
||||
#hero {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
#links {
|
||||
width: 400px;
|
||||
text-align: left;
|
||||
font-size: x-large;
|
||||
color: white;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#links a {
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
margin-top: 20px;
|
||||
margin: 10px 0px;
|
||||
border: white 1px solid;
|
||||
border-radius: 5px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
#links a:hover {
|
||||
background-color: #1f1f1f;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#header {
|
||||
max-width: 1200px;
|
||||
}
|
||||
|
||||
|
||||
|
1
examples/core/src/global.rs
Normal file
1
examples/core/src/global.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub const APP_NAME: &str = "caretta_sync_example";
|
13
examples/core/src/gui.rs
Normal file
13
examples/core/src/gui.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use caretta_sync::{bevy::peer::PeerPlugin, utils::Runnable};
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub struct Gui {}
|
||||
|
||||
impl Runnable for Gui {
|
||||
fn run(self, app_name: &'static str) {
|
||||
App::new()
|
||||
//.add_plugins(DefaultPlugins)
|
||||
.add_plugins(PeerPlugin)
|
||||
.run();
|
||||
}
|
||||
}
|
|
@ -1 +1,4 @@
|
|||
pub mod ui;
|
||||
pub mod global;
|
||||
pub mod gui;
|
||||
pub mod rpc;
|
||||
pub mod server;
|
||||
|
|
1
examples/core/src/rpc/mod.rs
Normal file
1
examples/core/src/rpc/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod server;
|
63
examples/core/src/server.rs
Normal file
63
examples/core/src/server.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use caretta_sync::{
|
||||
config::P2pConfig,
|
||||
proto::cached_peer_service_server::CachedPeerServiceServer,
|
||||
server::ServerTrait,
|
||||
rpc::service::cached_peer::CachedPeerService
|
||||
};
|
||||
use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux};
|
||||
use tokio::net::UnixListener;
|
||||
use tokio_stream::wrappers::UnixListenerStream;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Server{}
|
||||
|
||||
impl ServerTrait for Server {
|
||||
async fn serve_p2p<T>(config: &T) -> Result<(), caretta_sync::error::Error>
|
||||
where
|
||||
T: AsRef<P2pConfig>
|
||||
{
|
||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(config.as_ref().private_key.clone())
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|keypair| caretta_sync::p2p::Behaviour::try_from(keypair).unwrap())?
|
||||
.build();
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
loop{
|
||||
let swarm_event = swarm.select_next_some().await;
|
||||
tokio::spawn(async move{
|
||||
match swarm_event {
|
||||
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
|
||||
SwarmEvent::Behaviour(event) => {
|
||||
println!("{event:?}");
|
||||
event.run().await;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_rpc<T>(config: &T) -> Result<(), caretta_sync::error::Error>
|
||||
where T: AsRef<caretta_sync::config::RpcConfig> {
|
||||
let path = config.as_ref().socket_path.clone();
|
||||
if let Some(x) = path.parent() {
|
||||
if !x.exists() {
|
||||
std::fs::create_dir_all(x).expect("Failed to create directory for socket file!");
|
||||
}
|
||||
}
|
||||
if path.exists() {
|
||||
std::fs::remove_file(&path).expect("Failed to remove existing socket file!")
|
||||
}
|
||||
let uds = UnixListener::bind(path).unwrap();
|
||||
let uds_stream = UnixListenerStream::new(uds);
|
||||
tonic::transport::Server::builder()
|
||||
.add_service(CachedPeerServiceServer::new(CachedPeerService::default()))
|
||||
.serve_with_incoming(uds_stream)
|
||||
.await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
pub mod plain;
|
|
@ -1,33 +0,0 @@
|
|||
use dioxus::prelude::*;
|
||||
|
||||
const FAVICON: Asset = asset!("/assets/favicon.ico");
|
||||
const MAIN_CSS: Asset = asset!("/assets/main.css");
|
||||
const HEADER_SVG: Asset = asset!("/assets/header.svg");
|
||||
|
||||
#[component]
|
||||
pub fn App() -> Element {
|
||||
rsx! {
|
||||
document::Link { rel: "icon", href: FAVICON }
|
||||
document::Link { rel: "stylesheet", href: MAIN_CSS }
|
||||
Hero {}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Hero() -> Element {
|
||||
rsx! {
|
||||
div {
|
||||
id: "hero",
|
||||
img { src: HEADER_SVG, id: "header" }
|
||||
div { id: "links",
|
||||
a { href: "https://dioxuslabs.com/learn/0.6/", "📚 Learn Dioxus" }
|
||||
a { href: "https://dioxuslabs.com/awesome", "🚀 Awesome Dioxus" }
|
||||
a { href: "https://github.com/dioxus-community/", "📡 Community Libraries" }
|
||||
a { href: "https://github.com/DioxusLabs/sdk", "⚙️ Dioxus Development Kit" }
|
||||
a { href: "https://marketplace.visualstudio.com/items?itemName=DioxusLabs.dioxus", "💫 VSCode Extension" }
|
||||
a { href: "https://discord.gg/XgGxMSkvUM", "👋 Community Discord" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +1,14 @@
|
|||
[package]
|
||||
name = "lazy-supplements-examples-desktop"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
name = "caretta-sync-example-desktop"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
dioxus.workspace = true
|
||||
lazy-supplements-examples-core.path = "../core"
|
||||
|
||||
[features]
|
||||
default = ["desktop"]
|
||||
web = ["dioxus/web"]
|
||||
desktop = ["dioxus/desktop"]
|
||||
mobile = ["dioxus/mobile"]
|
||||
clap.workspace = true
|
||||
caretta-sync = { path = "../..", features = ["cli", "bevy", "test"] }
|
||||
caretta-sync-example-core.path = "../core"
|
||||
libp2p.workspace = true
|
||||
tokio.workspace = true
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
[application]
|
||||
|
||||
[web.app]
|
||||
|
||||
# HTML title tag content
|
||||
title = "desktop"
|
||||
|
||||
# include `assets` in web platform
|
||||
[web.resource]
|
||||
|
||||
# Additional CSS style files
|
||||
style = []
|
||||
|
||||
# Additional JavaScript files
|
||||
script = []
|
||||
|
||||
[web.resource.dev]
|
||||
|
||||
# Javascript code file
|
||||
# serve: [dev-server] only
|
||||
script = []
|
|
@ -1,25 +0,0 @@
|
|||
# Development
|
||||
|
||||
Your new bare-bones project includes minimal organization with a single `main.rs` file and a few assets.
|
||||
|
||||
```
|
||||
project/
|
||||
├─ assets/ # Any assets that are used by the app should be placed here
|
||||
├─ src/
|
||||
│ ├─ main.rs # main.rs is the entry point to your application and currently contains all components for the app
|
||||
├─ Cargo.toml # The Cargo.toml file defines the dependencies and feature flags for your project
|
||||
```
|
||||
|
||||
### Serving Your App
|
||||
|
||||
Run the following command in the root of your project to start developing with the default platform:
|
||||
|
||||
```bash
|
||||
dx serve
|
||||
```
|
||||
|
||||
To run for a different platform, use the `--platform platform` flag. E.g.
|
||||
```bash
|
||||
dx serve --platform desktop
|
||||
```
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
await-holding-invalid-types = [
|
||||
"generational_box::GenerationalRef",
|
||||
{ path = "generational_box::GenerationalRef", reason = "Reads should not be held over an await point. This will cause any writes to fail while the await is pending since the read borrow is still active." },
|
||||
"generational_box::GenerationalRefMut",
|
||||
{ path = "generational_box::GenerationalRefMut", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
|
||||
"dioxus_signals::Write",
|
||||
{ path = "dioxus_signals::Write", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
|
||||
]
|
39
examples/desktop/src/cli/mod.rs
Normal file
39
examples/desktop/src/cli/mod.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use caretta_sync_example_core::{gui::Gui, server::Server};
|
||||
use clap::{Parser, Subcommand};
|
||||
use caretta_sync::{cli::*, config::Config, data::migration::DataMigrator, global::{CONFIG, DATABASE_CONNECTIONS}, utils::Runnable};
|
||||
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Option<CliCommand>,
|
||||
#[command(flatten)]
|
||||
config: ConfigArgs,
|
||||
}
|
||||
|
||||
impl Runnable for Cli {
|
||||
fn run(self, app_name: &'static str) {
|
||||
if let Some(x) = self.command {
|
||||
x.run(app_name)
|
||||
} else {
|
||||
tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async {
|
||||
let config: caretta_sync::config::Config = self.config.into_config(app_name).await;
|
||||
let _ = CONFIG.get_or_init::<Config>(config).await;
|
||||
});
|
||||
//let _ = DATABASE_CONNECTIONS.get_or_init_unchecked(&config, DataMigrator).await;
|
||||
Gui{}.run(app_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand, Runnable)]
|
||||
pub enum CliCommand {
|
||||
Config(ConfigCommandArgs),
|
||||
Device(DeviceCommandArgs),
|
||||
Peer(PeerCommandArgs),
|
||||
Serve(ServeCommandArgs<Server>),
|
||||
}
|
|
@ -1,3 +1,10 @@
|
|||
use caretta_sync::utils::Runnable;
|
||||
use caretta_sync_example_core::global::APP_NAME;
|
||||
use clap::Parser;
|
||||
|
||||
use crate::cli::Cli;
|
||||
|
||||
fn main() {
|
||||
dioxus::launch(lazy_supplements_examples_core::ui::plain::App);
|
||||
let args = Cli::parse();
|
||||
args.run(APP_NAME);
|
||||
}
|
||||
|
|
7
examples/mobile/.gitignore
vendored
Normal file
7
examples/mobile/.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
/build
|
||||
.gradle
|
||||
.idea
|
||||
.DS_Store
|
||||
build
|
||||
.cxx
|
||||
local.properties
|
|
@ -1,17 +1,20 @@
|
|||
[package]
|
||||
name = "lazy-supplements-examples-mobile"
|
||||
version = "0.1.0"
|
||||
authors = ["fluo10 <fluo10.dev@fireturtle.net>"]
|
||||
edition = "2021"
|
||||
name = "caretta-sync-example-mobile"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
description.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[[bin]]
|
||||
name = "caretta_sync_example"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
name = "caretta_sync_example"
|
||||
crate-type = ["lib", "cdylib"]
|
||||
|
||||
[dependencies]
|
||||
dioxus.workspace = true
|
||||
lazy-supplements-examples-core.path = "../core"
|
||||
|
||||
[features]
|
||||
default = ["mobile"]
|
||||
web = ["dioxus/web"]
|
||||
desktop = ["dioxus/desktop"]
|
||||
mobile = ["dioxus/mobile"]
|
||||
bevy.workspace = true
|
||||
caretta-sync-example-core.path = "../core"
|
||||
caretta-sync.path = "../.."
|
||||
|
|
25
examples/mobile/ios/Makefile
Normal file
25
examples/mobile/ios/Makefile
Normal file
|
@ -0,0 +1,25 @@
|
|||
.PHONY: xcodebuild run install boot-sim generate clean
|
||||
|
||||
DEVICE = ${DEVICE_ID}
|
||||
ifndef DEVICE_ID
|
||||
DEVICE=$(shell xcrun simctl list devices 'iOS' | grep -v 'unavailable' | grep -v '^--' | grep -v '==' | head -n 1 | grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})")
|
||||
endif
|
||||
|
||||
run: install
|
||||
xcrun simctl launch --console $(DEVICE) net.fireturtle.caretta-sync-example
|
||||
|
||||
boot-sim:
|
||||
xcrun simctl boot $(DEVICE) || true
|
||||
|
||||
install: xcodebuild-simulator boot-sim
|
||||
xcrun simctl install $(DEVICE) build/Build/Products/Debug-iphonesimulator/caretta_sync_example.app
|
||||
|
||||
xcodebuild-simulator:
|
||||
IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme caretta_sync_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)"
|
||||
|
||||
xcodebuild-iphone:
|
||||
IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme caretta_sync_example -configuration Debug -derivedDataPath build -arch arm64
|
||||
|
||||
clean:
|
||||
rm -r build
|
||||
cargo clean
|
68
examples/mobile/ios/build_rust_deps.sh
Executable file
68
examples/mobile/ios/build_rust_deps.sh
Executable file
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# based on https://github.com/mozilla/glean/blob/main/build-scripts/xc-universal-binary.sh
|
||||
|
||||
set -eux
|
||||
|
||||
PATH=$PATH:$HOME/.cargo/bin
|
||||
|
||||
PROFILE=debug
|
||||
RELFLAG=
|
||||
if [[ "$CONFIGURATION" != "Debug" ]]; then
|
||||
PROFILE=release
|
||||
RELFLAG=--release
|
||||
fi
|
||||
|
||||
set -euvx
|
||||
|
||||
# add homebrew bin path, as it's the most commonly used package manager on macOS
|
||||
# this is needed for cmake on apple arm processors as it's not available by default
|
||||
export PATH="$PATH:/opt/homebrew/bin"
|
||||
|
||||
# Make Cargo output cache files in Xcode's directories
|
||||
export CARGO_TARGET_DIR="$DERIVED_FILE_DIR/cargo"
|
||||
|
||||
# Xcode places `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
|
||||
# at the front of the path, with makes the build fail with `ld: library 'System' not found`, upstream issue:
|
||||
# <https://github.com/rust-lang/rust/issues/80817>.
|
||||
#
|
||||
# Work around it by resetting the path, so that we use the system `cc`.
|
||||
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
|
||||
|
||||
IS_SIMULATOR=0
|
||||
if [ "${LLVM_TARGET_TRIPLE_SUFFIX-}" = "-simulator" ]; then
|
||||
IS_SIMULATOR=1
|
||||
fi
|
||||
|
||||
EXECUTABLES=
|
||||
for arch in $ARCHS; do
|
||||
case "$arch" in
|
||||
x86_64)
|
||||
if [ $IS_SIMULATOR -eq 0 ]; then
|
||||
echo "Building for x86_64, but not a simulator build. What's going on?" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# Intel iOS simulator
|
||||
export CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios"
|
||||
TARGET=x86_64-apple-ios
|
||||
;;
|
||||
|
||||
arm64)
|
||||
if [ $IS_SIMULATOR -eq 0 ]; then
|
||||
# Hardware iOS targets
|
||||
TARGET=aarch64-apple-ios
|
||||
else
|
||||
# M1 iOS simulator
|
||||
TARGET=aarch64-apple-ios-sim
|
||||
fi
|
||||
esac
|
||||
cd ..
|
||||
cargo build $RELFLAG --target $TARGET --bin caretta_sync_example
|
||||
cd -
|
||||
# Collect the executables
|
||||
EXECUTABLES="$EXECUTABLES $DERIVED_FILE_DIR/cargo/$TARGET/$PROFILE/caretta_sync_example"
|
||||
done
|
||||
|
||||
# Combine executables, and place them at the output path excepted by Xcode
|
||||
lipo -create -output "$TARGET_BUILD_DIR/$EXECUTABLE_PATH" $EXECUTABLES
|
|
@ -0,0 +1,307 @@
|
|||
// !$*UTF8*$!
|
||||
{
|
||||
archiveVersion = 1;
|
||||
classes = {
|
||||
};
|
||||
objectVersion = 77;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
B617AE7C2E5D5E5A0013202E /* caretta_sync_example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = caretta_sync_example.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
B698F93D2E5E743A00C7EE06 /* caretta_sync_example.app */ = {isa = PBXFileReference; lastKnownFileType = wrapper.application; name = caretta_sync_example.app; path = "build/Build/Products/Debug-iphonesimulator/caretta_sync_example.app"; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
B617AE732E5D5E5A0013202E = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
B617AE7D2E5D5E5A0013202E /* Products */,
|
||||
B698F93D2E5E743A00C7EE06 /* caretta_sync_example.app */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
B617AE7D2E5D5E5A0013202E /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
B617AE7C2E5D5E5A0013202E /* caretta_sync_example.app */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
B617AE7B2E5D5E5A0013202E /* caretta_sync_example */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = B617AE872E5D5E5B0013202E /* Build configuration list for PBXNativeTarget "caretta_sync_example" */;
|
||||
buildPhases = (
|
||||
B698F8CE2E5D609900C7EE06 /* ShellScript */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = caretta_sync_example;
|
||||
packageProductDependencies = (
|
||||
);
|
||||
productName = caretta_sync_example;
|
||||
productReference = B617AE7C2E5D5E5A0013202E /* caretta_sync_example.app */;
|
||||
productType = "com.apple.product-type.application";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
B617AE742E5D5E5A0013202E /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
BuildIndependentTargetsInParallel = NO;
|
||||
LastSwiftUpdateCheck = 1640;
|
||||
LastUpgradeCheck = 1640;
|
||||
TargetAttributes = {
|
||||
B617AE7B2E5D5E5A0013202E = {
|
||||
CreatedOnToolsVersion = 16.4;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = B617AE772E5D5E5A0013202E /* Build configuration list for PBXProject "caretta_sync_example" */;
|
||||
developmentRegion = en;
|
||||
hasScannedForEncodings = 0;
|
||||
knownRegions = (
|
||||
en,
|
||||
);
|
||||
mainGroup = B617AE732E5D5E5A0013202E;
|
||||
minimizedProjectReferenceProxies = 1;
|
||||
preferredProjectObjectVersion = 77;
|
||||
productRefGroup = B617AE7D2E5D5E5A0013202E /* Products */;
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
B617AE7B2E5D5E5A0013202E /* caretta_sync_example */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
||||
/* Begin PBXShellScriptBuildPhase section */
|
||||
B698F8CE2E5D609900C7EE06 /* ShellScript */ = {
|
||||
isa = PBXShellScriptBuildPhase;
|
||||
alwaysOutOfDate = 1;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
inputFileListPaths = (
|
||||
);
|
||||
inputPaths = (
|
||||
"$(SRCROOT)/build_rust_deps.sh",
|
||||
);
|
||||
outputFileListPaths = (
|
||||
);
|
||||
outputPaths = (
|
||||
$TARGET_BUILD_DIR/$EXECUTABLE_PATH,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
shellPath = /bin/sh;
|
||||
shellScript = "./build_rust_deps.sh\n";
|
||||
};
|
||||
/* End PBXShellScriptBuildPhase section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
B617AE852E5D5E5B0013202E /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
ENABLE_TESTABILITY = YES;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu17;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 18.5;
|
||||
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
|
||||
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
|
||||
MTL_FAST_MATH = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
B617AE862E5D5E5B0013202E /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu17;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 18.5;
|
||||
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
MTL_FAST_MATH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
VALIDATE_PRODUCT = YES;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
B617AE882E5D5E5B0013202E /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = NO;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "net.fireturtle.caretta-sync-example";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
B617AE892E5D5E5B0013202E /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = NO;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "net.fireturtle.caretta-sync-example";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
B617AE772E5D5E5A0013202E /* Build configuration list for PBXProject "caretta_sync_example" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
B617AE852E5D5E5B0013202E /* Debug */,
|
||||
B617AE862E5D5E5B0013202E /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Debug;
|
||||
};
|
||||
B617AE872E5D5E5B0013202E /* Build configuration list for PBXNativeTarget "caretta_sync_example" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
B617AE882E5D5E5B0013202E /* Debug */,
|
||||
B617AE892E5D5E5B0013202E /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Debug;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
rootObject = B617AE742E5D5E5A0013202E /* Project object */;
|
||||
}
|
7
examples/mobile/ios/caretta_sync_example.xcodeproj/project.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
examples/mobile/ios/caretta_sync_example.xcodeproj/project.xcworkspace/contents.xcworkspacedata
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:">
|
||||
</FileRef>
|
||||
</Workspace>
|
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue