wip: startup flow

This commit is contained in:
2024-11-21 08:43:42 +07:00
parent fb71f02ef3
commit c60a37a245
15 changed files with 910 additions and 82 deletions

10
crates/client/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "client"
version = "0.1.0"
edition = "2021"
[dependencies]
gpui.workspace = true
nostr-sdk.workspace = true
dirs.workspace = true
tokio.workspace = true

20
crates/client/src/lib.rs Normal file
View File

@@ -0,0 +1,20 @@
use gpui::Global;
use nostr_sdk::prelude::*;
use state::get_client;
pub mod state;
pub struct NostrClient {
pub client: &'static Client,
}
impl Global for NostrClient {}
impl NostrClient {
pub async fn init() -> Self {
// Initialize nostr client
let client = get_client().await;
Self { client }
}
}

View File

@@ -0,0 +1,37 @@
use dirs::config_dir;
use nostr_sdk::prelude::*;
use std::fs;
use tokio::sync::OnceCell;
pub static CLIENT: OnceCell<Client> = OnceCell::const_new();
pub async fn get_client() -> &'static Client {
CLIENT
.get_or_init(|| async {
// Setup app data folder
let config_dir = config_dir().unwrap();
let _ = fs::create_dir_all(config_dir.join("Coop/"));
// Setup database
let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr"))
.expect("Database is NOT initialized");
// Setup Nostr Client
let client = ClientBuilder::default().database(lmdb).build();
// Add some bootstrap relays
let _ = client.add_relay("wss://relay.damus.io").await;
let _ = client.add_relay("wss://relay.primal.net").await;
let _ = client.add_relay("wss://nostr.fmt.wiz.biz").await;
let _ = client.add_relay("wss://directory.yabu.me").await;
let _ = client.add_relay("wss://purplepag.es").await;
let _ = client.add_relay("wss://user.kindpag.es/").await;
// Connect to all relays
client.connect().await;
// Return client
client
})
.await
}