chore(homeserver): prepare for release

This commit is contained in:
nazeh
2025-02-13 19:13:40 +03:00
parent 6b92c39af0
commit 1435fe8686
4 changed files with 81 additions and 50 deletions

View File

@@ -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

View File

@@ -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<Self> {
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<Self> {
unsafe { HomeserverCore::new(CoreConfig::test()) }
}
pub async fn create_root_user(&mut self, keypair: &Keypair) -> Result<String> {
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<String> {
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<Response> {
Ok(self.router.clone().oneshot(request).await?)
Ok(header_value)
}
pub async fn call(&self, request: Request) -> Result<Response> {
Ok(self.router.clone().oneshot(request).await?)
}
}
}

View File

@@ -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://<server public key>` url
/// Returns the `https://<server public key>` url
pub fn url(&self) -> url::Url {
url::Url::parse(&format!("https://{}", self.public_key())).expect("valid url")
}

View File

@@ -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;