diff --git a/pubky-homeserver/README.md b/pubky-homeserver/README.md index 8aa2c26..cc7b443 100644 --- a/pubky-homeserver/README.md +++ b/pubky-homeserver/README.md @@ -1,9 +1,31 @@ # Pubky Homeserver -A pubky-core homeserver that acts as users' agent on the Internet, providing data availability and more.more.more.more. +A pubky-core homeserver that acts as users' agent on the Internet, providing data availability and more. ## Usage +### Library + +You can use the Homeserver as a library in other crates/binaries or for testing purposes. + +```rust + +#[tokio::main] +async fn main() -> Result<()> { + Homeserver::builder().run().await? + + tokio::signal::ctrl_c().await?; + + tracing::info!("Shutting down Homeserver"); + + server.shutdown(); + + Ok(()) +} +``` + +### Binary + Use `cargo run` ```bash diff --git a/pubky-homeserver/src/core/mod.rs b/pubky-homeserver/src/core/mod.rs index 472cac0..ac3b119 100644 --- a/pubky-homeserver/src/core/mod.rs +++ b/pubky-homeserver/src/core/mod.rs @@ -1,19 +1,8 @@ use std::path::PathBuf; use anyhow::Result; -use axum::{ - body::Body, - extract::Request, - http::{header, Method}, - response::Response, - Router, -}; -use pkarr::Keypair; -use pubky_common::{ - auth::{AuthToken, AuthVerifier}, - capabilities::Capability, -}; -use tower::ServiceExt; +use axum::Router; +use pubky_common::auth::AuthVerifier; pub mod database; mod error; @@ -36,7 +25,6 @@ pub(crate) struct AppState { #[derive(Debug, Clone)] /// A side-effect-free Core of the [crate::Homeserver]. pub struct HomeserverCore { - config: CoreConfig, pub(crate) router: Router, } @@ -56,48 +44,61 @@ impl HomeserverCore { let router = routes::create_app(state.clone()); - Ok(Self { router, config }) + Ok(Self { router }) } +} - /// Test version of [HomeserverCore::new], using an ephemeral small storage. - pub fn test() -> Result { - unsafe { HomeserverCore::new(CoreConfig::test()) } - } +#[cfg(test)] +mod tests { - // === Getters === + use anyhow::Result; + use axum::{ + body::Body, + extract::Request, + http::{header, Method}, + response::Response, + }; + use pkarr::Keypair; + use pubky_common::{auth::AuthToken, capabilities::Capability}; + use tower::ServiceExt; - pub fn config(&self) -> &CoreConfig { - &self.config - } + use super::*; - // === Public Methods === + impl HomeserverCore { + /// Test version of [HomeserverCore::new], using an ephemeral small storage. + pub fn test() -> Result { + unsafe { HomeserverCore::new(CoreConfig::test()) } + } - pub async fn create_root_user(&mut self, keypair: &Keypair) -> Result { - let auth_token = AuthToken::sign(keypair, vec![Capability::root()]); + // === Public Methods === - let response = self - .call( - Request::builder() - .uri("/signup") - .header("host", keypair.public_key().to_string()) - .method(Method::POST) - .body(Body::from(auth_token.serialize())) - .unwrap(), - ) - .await?; + pub async fn create_root_user(&mut self, keypair: &Keypair) -> Result { + let auth_token = AuthToken::sign(keypair, vec![Capability::root()]); - let header_value = response - .headers() - .get(header::SET_COOKIE) - .and_then(|h| h.to_str().ok()) - .expect("should return a set-cookie header") - .to_string(); + let response = self + .call( + Request::builder() + .uri("/signup") + .header("host", keypair.public_key().to_string()) + .method(Method::POST) + .body(Body::from(auth_token.serialize())) + .unwrap(), + ) + .await?; - Ok(header_value) - } + let header_value = response + .headers() + .get(header::SET_COOKIE) + .and_then(|h| h.to_str().ok()) + .expect("should return a set-cookie header") + .to_string(); - pub async fn call(&self, request: Request) -> Result { - Ok(self.router.clone().oneshot(request).await?) + Ok(header_value) + } + + pub async fn call(&self, request: Request) -> Result { + Ok(self.router.clone().oneshot(request).await?) + } } } diff --git a/pubky-homeserver/src/io/mod.rs b/pubky-homeserver/src/io/mod.rs index 5a84ff9..a848f47 100644 --- a/pubky-homeserver/src/io/mod.rs +++ b/pubky-homeserver/src/io/mod.rs @@ -12,13 +12,14 @@ use tracing::info; use crate::{ config::{Config, DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT}, - HomeserverCore, + core::HomeserverCore, }; mod http; mod pkarr; #[derive(Debug, Default)] +/// Builder for [Homeserver]. pub struct HomeserverBuilder(Config); impl HomeserverBuilder { @@ -135,11 +136,12 @@ impl Homeserver { // === Getters === + /// Returns the public_key of this server. pub fn public_key(&self) -> PublicKey { self.keypair.public_key() } - /// Return the `https://` url + /// Returns the `https://` url pub fn url(&self) -> url::Url { url::Url::parse(&format!("https://{}", self.public_key())).expect("valid url") } diff --git a/pubky-homeserver/src/lib.rs b/pubky-homeserver/src/lib.rs index 85f86fe..83b27d3 100644 --- a/pubky-homeserver/src/lib.rs +++ b/pubky-homeserver/src/lib.rs @@ -1,7 +1,13 @@ +#![doc = include_str!("../README.md")] +//! + +#![deny(missing_docs)] +#![deny(rustdoc::broken_intra_doc_links)] +#![cfg_attr(any(), deny(clippy::unwrap_used))] + mod config; mod core; mod io; -pub use core::HomeserverCore; pub use io::Homeserver; pub use io::HomeserverBuilder;