Skip to main content

hashtree_cli/server/
mod.rs

1mod auth;
2mod blob_read;
3pub mod blossom;
4mod handlers;
5mod ingest_filter;
6mod mime;
7mod nostr_query;
8mod peer_status;
9mod request_paths;
10mod status_metrics;
11#[cfg(feature = "p2p")]
12pub mod stun;
13mod ui;
14pub mod ws_relay;
15
16use crate::nostr_relay::NostrRelay;
17use crate::socialgraph;
18use crate::storage::HashtreeStore;
19use crate::webrtc::WebRTCState;
20use anyhow::Result;
21use axum::{
22    body::Body,
23    extract::DefaultBodyLimit,
24    http::Request,
25    middleware,
26    response::Response,
27    routing::{get, post, put},
28    Router,
29};
30use futures::{future::poll_fn, pin_mut, FutureExt};
31use hyper::body::Incoming;
32use hyper_util::{
33    rt::{TokioExecutor, TokioIo, TokioTimer},
34    server::conn::auto::Builder as HyperBuilder,
35    service::TowerToHyperService,
36};
37use socket2::{SockRef, TcpKeepalive};
38use std::collections::{HashMap, HashSet};
39use std::convert::Infallible;
40use std::future;
41use std::io;
42use std::net::SocketAddr;
43use std::sync::{Arc, OnceLock, RwLock};
44use std::time::Duration;
45use tokio::sync::watch;
46use tower::{Service, ServiceExt as _};
47use tower_http::cors::CorsLayer;
48use tracing::{debug, error, trace};
49
50pub use auth::{new_lookup_cache, AppState, AuthCredentials, CachedTreeRootEntry};
51
52static VIRTUAL_TREE_HOSTS: OnceLock<RwLock<HashMap<String, String>>> = OnceLock::new();
53const DEFAULT_OPTIMISTIC_UPLOAD_QUEUE_BYTES: usize = 512 * 1024 * 1024;
54
55#[cfg(not(test))]
56const HTTP1_HEADER_READ_TIMEOUT: Duration = Duration::from_secs(30);
57#[cfg(test)]
58const HTTP1_HEADER_READ_TIMEOUT: Duration = Duration::from_millis(200);
59const HTTP2_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30);
60const HTTP2_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(10);
61const TCP_KEEPALIVE_TIME: Duration = Duration::from_secs(60);
62const TCP_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(15);
63
64fn virtual_tree_hosts() -> &'static RwLock<HashMap<String, String>> {
65    VIRTUAL_TREE_HOSTS.get_or_init(|| RwLock::new(HashMap::new()))
66}
67
68fn normalize_virtual_tree_host(host: &str) -> Option<String> {
69    let trimmed = host.trim().trim_end_matches('.').to_ascii_lowercase();
70    if trimmed.is_empty() {
71        return None;
72    }
73
74    if let Some(stripped) = trimmed
75        .strip_prefix('[')
76        .and_then(|value| value.split_once(']'))
77    {
78        let host_only = stripped.0.trim();
79        if host_only.is_empty() {
80            return None;
81        }
82        return Some(host_only.to_string());
83    }
84
85    if let Some((host_only, port)) = trimmed.rsplit_once(':') {
86        if !host_only.is_empty() && !port.is_empty() && port.chars().all(|ch| ch.is_ascii_digit()) {
87            return Some(host_only.to_string());
88        }
89    }
90
91    Some(trimmed)
92}
93
94pub fn register_virtual_tree_host(host: &str, internal_root: &str) {
95    let Some(normalized_host) = normalize_virtual_tree_host(host) else {
96        return;
97    };
98
99    let normalized_root = internal_root.trim().trim_end_matches('/');
100    if normalized_root.is_empty() {
101        return;
102    }
103
104    if let Ok(mut hosts) = virtual_tree_hosts().write() {
105        hosts.insert(normalized_host, normalized_root.to_string());
106    }
107}
108
109pub fn resolve_virtual_tree_host(host: &str) -> Option<String> {
110    let normalized_host = normalize_virtual_tree_host(host)?;
111    virtual_tree_hosts()
112        .read()
113        .ok()
114        .and_then(|hosts| hosts.get(&normalized_host).cloned())
115}
116
117#[cfg(test)]
118pub fn clear_virtual_tree_hosts_for_test() {
119    if let Ok(mut hosts) = virtual_tree_hosts().write() {
120        hosts.clear();
121    }
122}
123
124pub struct HashtreeServer {
125    state: AppState,
126    addr: String,
127    extra_routes: Option<Router<AppState>>,
128    cors: Option<CorsLayer>,
129}
130
131impl HashtreeServer {
132    pub fn new(store: Arc<HashtreeStore>, addr: String) -> Self {
133        Self {
134            state: AppState {
135                store,
136                auth: None,
137                daemon_started_at: current_unix_secs(),
138                peer_mode: crate::config::ServerMode::Normal,
139                hash_get_enabled: true,
140                http_webrtc_fetch: true,
141                webrtc_peers: None,
142                fips_transport: None,
143                fetch_from_fips_peers: true,
144                ws_relay: Arc::new(auth::WsRelayState::new()),
145                max_upload_bytes: 5 * 1024 * 1024, // 5 MB default
146                public_writes: true,               // Allow anyone with valid Nostr auth by default
147                require_random_untrusted_ingest: true,
148                optimistic_blossom_uploads: false,
149                optimistic_upload_queue_bytes: DEFAULT_OPTIMISTIC_UPLOAD_QUEUE_BYTES,
150                optimistic_upload_queue: Arc::new(tokio::sync::Semaphore::new(
151                    DEFAULT_OPTIMISTIC_UPLOAD_QUEUE_BYTES,
152                )),
153                allowed_pubkeys: HashSet::new(), // No pubkeys allowed by default (use public_writes)
154                upstream_blossom: Vec::new(),
155                social_graph: None,
156                social_graph_store: None,
157                social_graph_root: None,
158                socialgraph_snapshot_public: false,
159                nostr_relay: None,
160                nostr_relay_urls: Vec::new(),
161                tree_root_cache: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
162                inflight_blob_fetches: Arc::new(tokio::sync::Mutex::new(
163                    std::collections::HashMap::new(),
164                )),
165                inflight_blob_reads: Arc::new(tokio::sync::Mutex::new(
166                    std::collections::HashMap::new(),
167                )),
168                blob_cache: Arc::new(crate::blob_cache::BlobCache::from_env()),
169                directory_listing_cache: Arc::new(std::sync::Mutex::new(new_lookup_cache())),
170                resolved_path_cache: Arc::new(std::sync::Mutex::new(new_lookup_cache())),
171                thumbnail_path_cache: Arc::new(std::sync::Mutex::new(new_lookup_cache())),
172                cid_size_cache: Arc::new(std::sync::Mutex::new(new_lookup_cache())),
173            },
174            addr,
175            extra_routes: None,
176            cors: None,
177        }
178    }
179
180    /// Set maximum upload size for Blossom uploads
181    pub fn with_max_upload_bytes(mut self, bytes: usize) -> Self {
182        self.state.max_upload_bytes = bytes;
183        self
184    }
185
186    /// Set whether to allow public writes (anyone with valid Nostr auth)
187    /// When false, only social graph members can write
188    pub fn with_public_writes(mut self, public: bool) -> Self {
189        self.state.public_writes = public;
190        self
191    }
192
193    pub fn with_require_random_untrusted_ingest(mut self, require: bool) -> Self {
194        self.state.require_random_untrusted_ingest = require;
195        self
196    }
197
198    pub fn with_optimistic_blossom_uploads(mut self, enabled: bool) -> Self {
199        self.state.optimistic_blossom_uploads = enabled;
200        self
201    }
202
203    pub fn with_server_mode(mut self, mode: crate::config::ServerMode) -> Self {
204        self.state.peer_mode = mode;
205        self
206    }
207
208    pub fn with_hash_get_enabled(mut self, enabled: bool) -> Self {
209        self.state.hash_get_enabled = enabled;
210        self
211    }
212
213    pub fn with_http_webrtc_fetch(mut self, enabled: bool) -> Self {
214        self.state.http_webrtc_fetch = enabled;
215        self
216    }
217
218    pub fn with_fetch_from_fips_peers(mut self, enabled: bool) -> Self {
219        self.state.fetch_from_fips_peers = enabled;
220        self
221    }
222
223    pub fn with_fips_transport(
224        mut self,
225        transport: Arc<crate::fips_transport::DaemonFipsTransport>,
226    ) -> Self {
227        self.state.fips_transport = Some(transport);
228        self
229    }
230
231    /// Set WebRTC state for P2P peer queries
232    pub fn with_webrtc_peers(mut self, webrtc_state: Arc<WebRTCState>) -> Self {
233        self.state.webrtc_peers = Some(webrtc_state);
234        self
235    }
236
237    pub fn with_auth(mut self, username: String, password: String) -> Self {
238        self.state.auth = Some(AuthCredentials { username, password });
239        self
240    }
241
242    /// Set allowed pubkeys for blossom write access (hex format)
243    pub fn with_allowed_pubkeys(mut self, pubkeys: HashSet<String>) -> Self {
244        self.state.allowed_pubkeys = pubkeys;
245        self
246    }
247
248    /// Set upstream Blossom servers for cascade fetching
249    pub fn with_upstream_blossom(mut self, servers: Vec<String>) -> Self {
250        self.state.upstream_blossom = servers;
251        self
252    }
253
254    /// Set social graph access control
255    pub fn with_social_graph(mut self, sg: Arc<socialgraph::SocialGraphAccessControl>) -> Self {
256        self.state.social_graph = Some(sg);
257        self
258    }
259
260    /// Configure social graph snapshot export (store handle + root)
261    pub fn with_socialgraph_snapshot(
262        mut self,
263        store: Arc<dyn socialgraph::SocialGraphBackend>,
264        root: [u8; 32],
265        public: bool,
266    ) -> Self {
267        self.state.social_graph_store = Some(store);
268        self.state.social_graph_root = Some(root);
269        self.state.socialgraph_snapshot_public = public;
270        self
271    }
272
273    /// Set Nostr relay state (shared for /ws and WebRTC)
274    pub fn with_nostr_relay(mut self, relay: Arc<NostrRelay>) -> Self {
275        self.state.nostr_relay = Some(relay);
276        self
277    }
278
279    /// Set active upstream Nostr relays for HTTP resolver operations.
280    pub fn with_nostr_relay_urls(mut self, relays: Vec<String>) -> Self {
281        self.state.nostr_relay_urls = relays;
282        self
283    }
284
285    /// Merge extra routes into the daemon router (e.g. Tauri embeds /nip07).
286    pub fn with_extra_routes(mut self, routes: Router<AppState>) -> Self {
287        self.extra_routes = Some(routes);
288        self
289    }
290
291    /// Apply a CORS layer to all routes (used by embedded clients like Tauri).
292    pub fn with_cors(mut self, cors: CorsLayer) -> Self {
293        self.cors = Some(cors);
294        self
295    }
296
297    pub async fn run(self) -> Result<()> {
298        let listener = tokio::net::TcpListener::bind(&self.addr).await?;
299        let _ = self.run_with_listener(listener).await?;
300        Ok(())
301    }
302
303    pub async fn run_with_listener(self, listener: tokio::net::TcpListener) -> Result<u16> {
304        self.run_with_listener_until(listener, future::pending::<()>())
305            .await
306    }
307
308    pub async fn run_with_listener_until<F>(
309        self,
310        listener: tokio::net::TcpListener,
311        shutdown: F,
312    ) -> Result<u16>
313    where
314        F: std::future::Future<Output = ()> + Send + 'static,
315    {
316        let local_addr = listener.local_addr()?;
317
318        // Public endpoints (no auth required)
319        // Note: /:id serves both CID and blossom SHA256 hash lookups
320        // The handler differentiates based on hash format (64 char hex = blossom)
321        let state = self.state.clone();
322        let public_routes = Router::new()
323            .route("/", get(handlers::serve_root_or_virtual_host))
324            .route("/ws", get(ws_relay::ws_data))
325            .route("/ws/", get(ws_relay::ws_data))
326            .route(
327                "/htree/test",
328                get(handlers::htree_test).head(handlers::htree_test),
329            )
330            // /htree/nhash1...[/path] - content-addressed (immutable)
331            .route("/htree/nhash1:nhash", get(handlers::htree_nhash))
332            .route("/htree/nhash1:nhash/", get(handlers::htree_nhash))
333            .route("/htree/nhash1:nhash/*path", get(handlers::htree_nhash_path))
334            // /htree/npub1.../tree[/path] - mutable (resolver-backed)
335            .route("/htree/npub1:npub/:treename", get(handlers::htree_npub))
336            .route("/htree/npub1:npub/:treename/", get(handlers::htree_npub))
337            .route(
338                "/htree/npub1:npub/:treename/*path",
339                get(handlers::htree_npub_path),
340            )
341            // Nostr resolver endpoints - resolve npub/treename to content
342            .route("/n/:pubkey/:treename", get(handlers::resolve_and_serve))
343            // Direct npub route (clients should parse nhash and request by hex hash)
344            .route("/npub1:rest", get(handlers::serve_npub))
345            // Blossom endpoints (BUD-01, BUD-02)
346            .route(
347                "/:id",
348                get(handlers::serve_content_or_blob)
349                    .head(blossom::head_blob)
350                    .delete(blossom::delete_blob)
351                    .options(blossom::cors_preflight),
352            )
353            .route(
354                "/upload",
355                put(blossom::upload_blob).options(blossom::cors_preflight),
356            )
357            .route(
358                "/upload/batch",
359                post(blossom::upload_blob_batch).options(blossom::cors_preflight),
360            )
361            .route(
362                "/upload/check",
363                post(blossom::upload_check).options(blossom::cors_preflight),
364            )
365            .route(
366                "/list/:pubkey",
367                get(blossom::list_blobs).options(blossom::cors_preflight),
368            )
369            // Hashtree API endpoints
370            .route("/health", get(handlers::health_check))
371            .route("/api/pins", get(handlers::list_pins))
372            .route("/api/stats", get(handlers::storage_stats))
373            .route("/api/peers", get(handlers::webrtc_peers))
374            .route("/api/status", get(handlers::daemon_status))
375            .route("/api/p2p/signal", post(handlers::p2p_signal))
376            .route("/api/socialgraph", get(handlers::socialgraph_stats))
377            .route(
378                "/api/socialgraph/snapshot",
379                get(handlers::socialgraph_snapshot),
380            )
381            .route(
382                "/api/socialgraph/distance/:pubkey",
383                get(handlers::follow_distance),
384            )
385            // Resolver API endpoints
386            .route(
387                "/api/resolve/:pubkey/:treename",
388                get(handlers::resolve_to_hash),
389            )
390            .route(
391                "/api/nostr/resolve/:pubkey/:treename",
392                get(handlers::resolve_to_hash),
393            )
394            .route("/api/nostr/profile/:pubkey", get(handlers::nostr_profile))
395            .route("/api/cache-tree-root", post(handlers::cache_tree_root))
396            .route(
397                "/api/clear-tree-root-cache",
398                post(handlers::clear_tree_root_cache),
399            )
400            .route("/api/trees/:pubkey", get(handlers::list_trees))
401            .fallback(get(handlers::serve_virtual_host_fallback))
402            .with_state(state.clone());
403
404        // Protected endpoints (require auth if enabled)
405        let protected_routes = Router::new()
406            .route("/upload", post(handlers::upload_file))
407            .route("/api/pin/:cid", post(handlers::pin_cid))
408            .route("/api/unpin/:cid", post(handlers::unpin_cid))
409            .route("/api/gc", post(handlers::garbage_collect))
410            .layer(middleware::from_fn_with_state(
411                state.clone(),
412                auth::auth_middleware,
413            ))
414            .with_state(state.clone());
415
416        let mut app = public_routes
417            .merge(protected_routes)
418            .layer(DefaultBodyLimit::max(10 * 1024 * 1024 * 1024)) // 10GB limit
419            .layer(middleware::from_fn(status_metrics::record_http_status));
420
421        if let Some(extra) = self.extra_routes {
422            app = app.merge(extra.with_state(state));
423        }
424
425        if let Some(cors) = self.cors {
426            app = app.layer(cors);
427        }
428
429        let make_service = app.into_make_service_with_connect_info::<std::net::SocketAddr>();
430        serve_with_connection_limits(listener, make_service, shutdown).await?;
431
432        Ok(local_addr.port())
433    }
434
435    pub fn addr(&self) -> &str {
436        &self.addr
437    }
438}
439
440async fn serve_with_connection_limits<M, S, F>(
441    listener: tokio::net::TcpListener,
442    mut make_service: M,
443    shutdown: F,
444) -> io::Result<()>
445where
446    M: Service<SocketAddr, Error = Infallible, Response = S> + Send + 'static,
447    M::Future: Send,
448    S: Service<Request<Body>, Response = Response, Error = Infallible> + Clone + Send + 'static,
449    S::Future: Send,
450    F: std::future::Future<Output = ()> + Send + 'static,
451{
452    let (signal_tx, signal_rx) = watch::channel(());
453    let signal_tx = Arc::new(signal_tx);
454    tokio::spawn(async move {
455        shutdown.await;
456        trace!("received graceful shutdown signal; stopping daemon listener");
457        drop(signal_rx);
458    });
459
460    let (close_tx, close_rx) = watch::channel(());
461
462    loop {
463        let (tcp_stream, remote_addr) = tokio::select! {
464            accepted = accept_tcp(&listener) => {
465                match accepted {
466                    Some(connection) => connection,
467                    None => continue,
468                }
469            }
470            _ = signal_tx.closed() => {
471                trace!("shutdown signal received; no longer accepting daemon connections");
472                break;
473            }
474        };
475
476        configure_tcp_stream(&tcp_stream);
477        let tcp_stream = TokioIo::new(tcp_stream);
478
479        poll_fn(|cx| make_service.poll_ready(cx))
480            .await
481            .unwrap_or_else(|err| match err {});
482
483        let tower_service = make_service
484            .call(remote_addr)
485            .await
486            .unwrap_or_else(|err| match err {})
487            .map_request(|req: Request<Incoming>| req.map(Body::new));
488        let hyper_service = TowerToHyperService::new(tower_service);
489
490        let signal_tx = Arc::clone(&signal_tx);
491        let close_rx = close_rx.clone();
492
493        tokio::spawn(async move {
494            let mut builder = HyperBuilder::new(TokioExecutor::new());
495            builder
496                .http1()
497                .timer(TokioTimer::new())
498                .header_read_timeout(HTTP1_HEADER_READ_TIMEOUT);
499            builder
500                .http2()
501                .timer(TokioTimer::new())
502                .keep_alive_interval(Some(HTTP2_KEEPALIVE_INTERVAL))
503                .keep_alive_timeout(HTTP2_KEEPALIVE_TIMEOUT);
504
505            let conn = builder.serve_connection_with_upgrades(tcp_stream, hyper_service);
506            pin_mut!(conn);
507
508            let signal_closed = signal_tx.closed().fuse();
509            pin_mut!(signal_closed);
510
511            loop {
512                tokio::select! {
513                    result = conn.as_mut() => {
514                        if let Err(err) = result {
515                            trace!("daemon connection closed with error: {err:#}");
516                        }
517                        break;
518                    }
519                    _ = &mut signal_closed => {
520                        trace!("shutdown signal received by connection task");
521                        conn.as_mut().graceful_shutdown();
522                    }
523                }
524            }
525
526            drop(close_rx);
527        });
528    }
529
530    drop(close_rx);
531    drop(listener);
532    close_tx.closed().await;
533
534    Ok(())
535}
536
537fn configure_tcp_stream(tcp_stream: &tokio::net::TcpStream) {
538    if let Err(err) = tcp_stream.set_nodelay(true) {
539        debug!("failed to set TCP_NODELAY on daemon connection: {err:#}");
540    }
541
542    let socket = SockRef::from(tcp_stream);
543    if let Err(err) = socket.set_tcp_keepalive(
544        &TcpKeepalive::new()
545            .with_time(TCP_KEEPALIVE_TIME)
546            .with_interval(TCP_KEEPALIVE_INTERVAL),
547    ) {
548        debug!("failed to set TCP keepalive on daemon connection: {err:#}");
549    }
550}
551
552async fn accept_tcp(
553    listener: &tokio::net::TcpListener,
554) -> Option<(tokio::net::TcpStream, SocketAddr)> {
555    match listener.accept().await {
556        Ok(connection) => Some(connection),
557        Err(err) => {
558            if is_connection_error(&err) {
559                return None;
560            }
561            error!("daemon accept error: {err}");
562            tokio::time::sleep(Duration::from_secs(1)).await;
563            None
564        }
565    }
566}
567
568fn is_connection_error(err: &io::Error) -> bool {
569    matches!(
570        err.kind(),
571        io::ErrorKind::ConnectionRefused
572            | io::ErrorKind::ConnectionAborted
573            | io::ErrorKind::ConnectionReset
574    )
575}
576
577fn current_unix_secs() -> u64 {
578    std::time::SystemTime::now()
579        .duration_since(std::time::UNIX_EPOCH)
580        .unwrap_or(std::time::Duration::ZERO)
581        .as_secs()
582}
583
584#[cfg(test)]
585mod tests {
586    use super::*;
587    use crate::nostr_relay::{NostrRelay, NostrRelayConfig};
588    use crate::storage::HashtreeStore;
589    use hashtree_core::{from_hex, nhash_encode, DirEntry, HashTree, HashTreeConfig, LinkType};
590    use nostr::{EventBuilder, Keys, Kind, Timestamp};
591    use serde_json::json;
592    use tempfile::TempDir;
593
594    #[tokio::test]
595    async fn test_server_serve_file() -> Result<()> {
596        let temp_dir = TempDir::new()?;
597        let store = Arc::new(HashtreeStore::new(temp_dir.path().join("db"))?);
598
599        // Create and upload a test file
600        let test_file = temp_dir.path().join("test.txt");
601        std::fs::write(&test_file, b"Hello, Hashtree!")?;
602
603        let cid = store.upload_file(&test_file)?;
604        let hash = from_hex(&cid)?;
605
606        // Verify we can get it
607        let content = store.get_file(&hash)?;
608        assert!(content.is_some());
609        assert_eq!(content.unwrap(), b"Hello, Hashtree!");
610
611        Ok(())
612    }
613
614    #[tokio::test]
615    async fn test_server_list_pins() -> Result<()> {
616        let temp_dir = TempDir::new()?;
617        let store = Arc::new(HashtreeStore::new(temp_dir.path().join("db"))?);
618
619        let test_file = temp_dir.path().join("test.txt");
620        std::fs::write(&test_file, b"Test")?;
621
622        let cid = store.upload_file(&test_file)?;
623        let hash = from_hex(&cid)?;
624
625        let pins = store.list_pins_raw()?;
626        assert_eq!(pins.len(), 1);
627        assert_eq!(pins[0], hash);
628
629        Ok(())
630    }
631
632    async fn spawn_test_server(
633        store: Arc<HashtreeStore>,
634    ) -> Result<(u16, tokio::task::JoinHandle<Result<()>>)> {
635        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
636        let port = listener.local_addr()?.port();
637        let server = HashtreeServer::new(store, "127.0.0.1:0".to_string());
638        let handle =
639            tokio::spawn(async move { server.run_with_listener(listener).await.map(|_| ()) });
640        Ok((port, handle))
641    }
642
643    async fn spawn_test_server_with_nostr_relay(
644        store: Arc<HashtreeStore>,
645        relay: Arc<NostrRelay>,
646    ) -> Result<(u16, tokio::task::JoinHandle<Result<()>>)> {
647        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
648        let port = listener.local_addr()?.port();
649        let server = HashtreeServer::new(store, "127.0.0.1:0".to_string()).with_nostr_relay(relay);
650        let handle =
651            tokio::spawn(async move { server.run_with_listener(listener).await.map(|_| ()) });
652        Ok((port, handle))
653    }
654
655    #[tokio::test]
656    async fn virtual_tree_hosts_serve_root_assets_and_spa_fallbacks() -> Result<()> {
657        clear_virtual_tree_hosts_for_test();
658
659        let temp_dir = TempDir::new()?;
660        let store = Arc::new(HashtreeStore::new(temp_dir.path().join("db"))?);
661        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()).public());
662
663        let (index_cid, _) = tree
664            .put(b"<!doctype html><title>Virtual host ok</title>")
665            .await?;
666        let (favicon_cid, _) = tree.put(b"ico").await?;
667        let (main_js_cid, _) = tree.put(b"console.log('ok');").await?;
668        let assets_dir = tree
669            .put_directory(vec![
670                DirEntry::from_cid("main.js", &main_js_cid).with_link_type(LinkType::File)
671            ])
672            .await?;
673        let root_cid = tree
674            .put_directory(vec![
675                DirEntry::from_cid("index.html", &index_cid).with_link_type(LinkType::File),
676                DirEntry::from_cid("favicon.ico", &favicon_cid).with_link_type(LinkType::File),
677                DirEntry::from_cid("assets", &assets_dir).with_link_type(LinkType::Dir),
678            ])
679            .await?;
680        let nhash = nhash_encode(&root_cid.hash)?;
681        let host = "tree-test.htree.localhost";
682        register_virtual_tree_host(host, &format!("/htree/{nhash}"));
683
684        let (port, handle) = spawn_test_server(store).await?;
685        let base_url = format!("http://127.0.0.1:{port}");
686        let host_header = format!("{host}:{port}");
687        let client = reqwest::Client::new();
688
689        let root_response = client
690            .get(format!("{base_url}/"))
691            .header("Host", &host_header)
692            .header("Accept", "text/html")
693            .send()
694            .await?;
695        assert_eq!(root_response.status(), reqwest::StatusCode::OK);
696        assert_eq!(
697            root_response.bytes().await?.as_ref(),
698            b"<!doctype html><title>Virtual host ok</title>"
699        );
700
701        let favicon_response = client
702            .get(format!("{base_url}/favicon.ico"))
703            .header("Host", &host_header)
704            .send()
705            .await?;
706        assert_eq!(favicon_response.status(), reqwest::StatusCode::OK);
707        assert_eq!(favicon_response.bytes().await?.as_ref(), b"ico");
708
709        let js_response = client
710            .get(format!("{base_url}/assets/main.js"))
711            .header("Host", &host_header)
712            .send()
713            .await?;
714        assert_eq!(js_response.status(), reqwest::StatusCode::OK);
715        assert_eq!(js_response.bytes().await?.as_ref(), b"console.log('ok');");
716
717        let profile_response = client
718            .get(format!("{base_url}/users/npub1example"))
719            .header("Host", &host_header)
720            .header("Accept", "text/html")
721            .send()
722            .await?;
723        assert_eq!(profile_response.status(), reqwest::StatusCode::OK);
724        assert_eq!(
725            profile_response.bytes().await?.as_ref(),
726            b"<!doctype html><title>Virtual host ok</title>"
727        );
728
729        handle.abort();
730        clear_virtual_tree_hosts_for_test();
731
732        Ok(())
733    }
734
735    #[tokio::test]
736    async fn nostr_profile_route_returns_latest_metadata_event() -> Result<()> {
737        let temp_dir = TempDir::new()?;
738        let store = Arc::new(HashtreeStore::new(temp_dir.path().join("db"))?);
739        let graph_store = {
740            let _guard = crate::socialgraph::test_lock();
741            crate::socialgraph::open_social_graph_store_with_mapsize(
742                &temp_dir.path().join("relay-db"),
743                Some(128 * 1024 * 1024),
744            )?
745        };
746        let backend: Arc<dyn crate::socialgraph::SocialGraphBackend> = graph_store;
747        let relay = Arc::new(NostrRelay::new(
748            backend,
749            temp_dir.path().to_path_buf(),
750            HashSet::new(),
751            None,
752            NostrRelayConfig {
753                spambox_db_max_bytes: 0,
754                ..Default::default()
755            },
756        )?);
757
758        let author = Keys::generate();
759        let older = EventBuilder::new(
760            Kind::Metadata,
761            json!({ "name": "older", "about": "before" }).to_string(),
762            [],
763        )
764        .custom_created_at(Timestamp::from_secs(10))
765        .to_event(&author)?;
766        let newer = EventBuilder::new(
767            Kind::Metadata,
768            json!({ "name": "newer", "about": "after" }).to_string(),
769            [],
770        )
771        .custom_created_at(Timestamp::from_secs(20))
772        .to_event(&author)?;
773
774        relay.ingest_trusted_event(older).await?;
775        relay.ingest_trusted_event(newer.clone()).await?;
776
777        let (port, handle) = spawn_test_server_with_nostr_relay(store, relay).await?;
778        let response = reqwest::get(format!(
779            "http://127.0.0.1:{port}/api/nostr/profile/{}",
780            author.public_key().to_hex()
781        ))
782        .await?;
783
784        assert_eq!(response.status(), reqwest::StatusCode::OK);
785        let payload: serde_json::Value = response.json().await?;
786        assert_eq!(payload["profile"]["name"].as_str(), Some("newer"),);
787        assert_eq!(payload["profile"]["about"].as_str(), Some("after"));
788        assert_eq!(payload["created_at"].as_u64(), Some(20));
789        let expected_event_id = newer.id.to_hex();
790        assert_eq!(
791            payload["event_id"].as_str(),
792            Some(expected_event_id.as_str())
793        );
794
795        handle.abort();
796        Ok(())
797    }
798}