#activity-pub #federation #activity-stream #async #http-client

apub-background-jobs

构建activitypub服务器的实用工具

2个不稳定版本

0.2.0 2021年12月5日
0.1.0 2021年11月30日

#2 in #activitystreams


apub中使用

AGPL-3.0

62KB
2K SLoC

基于background_jobs的客户端实现

use apub_core::session::SessionFactory;
use apub_background_jobs::{JobClient, ClientFactory, DeliverJob};
use apub_openssl::OpenSsl;
use apub_reqwest::{ReqwestClient, SignatureConfig};
use background_jobs::{memory_storage::Storage, WorkerConfig};
use openssl::{pkey::PKey, rsa::Rsa};
use url::Url;

#[derive(Clone)]
struct State {
    config: SignatureConfig,
    client: reqwest::Client,
}

impl ClientFactory for State {
    type Crypto = OpenSsl;
    type Client = ReqwestClient<OpenSsl>;

    fn build_client(&self, crypto: &Self::Crypto) -> Self::Client {
        ReqwestClient::new(self.client.clone().into(), self.config.clone(), crypto.clone())
    }
}

impl SessionFactory for State {
    type Session = ();

    fn build_session(&self) -> Self::Session {
        ()
    }
}

#[actix_rt::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = SignatureConfig::default();

    let http = reqwest::Client::new();

    let manager = WorkerConfig::new_managed(Storage::new(), move |_| State {
        config: config.clone(),
        client: http.clone(),
    })
    .register::<DeliverJob<State, OpenSsl>>()
    .start();

    let private_key = PKey::from_rsa(Rsa::generate(1024)?)?;
    let crypto = OpenSsl::new("key-id".to_string(), private_key);

    let inbox: Url = "https://masto.asonix.dog/inbox".parse()?;
    // let activity = /* ... */;
    // JobClient::<State>::new(crypto, queue_handle.clone()).enqueue(inbox, &activity)?;

    drop(manager);

    Ok(())
}

依赖项

~6–17MB
~224K SLoC