noosphere_cli/native/commands/
serve.rs

1//! Concrete implementations of subcommands related to running a Noosphere
2//! gateway server
3
4use crate::native::workspace::Workspace;
5use anyhow::Result;
6use noosphere_core::context::HasSphereContext;
7use noosphere_gateway::{Gateway, SingleTenantGatewayManager};
8use std::net::{IpAddr, TcpListener};
9use url::Url;
10
11/// Start a Noosphere gateway server
12pub async fn serve(
13    interface: IpAddr,
14    port: u16,
15    ipfs_api: Url,
16    name_resolver_api: Url,
17    cors_origin: Option<Url>,
18    workspace: &mut Workspace,
19) -> Result<()> {
20    workspace.ensure_sphere_initialized()?;
21
22    let listener = TcpListener::bind((interface, port))?;
23    let counterpart = workspace.counterpart_identity().await?;
24    let sphere_context = workspace.sphere_context().await?;
25    let gateway_identity = sphere_context
26        .sphere_context()
27        .await?
28        .author()
29        .did()
30        .await?;
31    let manager = SingleTenantGatewayManager::new(sphere_context, counterpart.clone()).await?;
32
33    let gateway = Gateway::new(manager, ipfs_api, name_resolver_api, cors_origin)?;
34
35    info!(
36        r#"A geist is summoned to manage local sphere {}
37
38    It has bound a gateway to {:?}
39    It awaits updates from sphere {}..."#,
40        gateway_identity,
41        listener
42            .local_addr()
43            .expect("Unexpected missing listener address"),
44        counterpart
45    );
46
47    gateway.start(listener).await?;
48    Ok(())
49}