Move background-jobs example to module docs

This commit is contained in:
Aode (lion) 2021-11-22 17:49:08 -06:00
parent 482d2a8cf0
commit 0aa9ed6211

View file

@ -1,4 +1,55 @@
//! An implementation of Client based on background_jobs
//! ```rust
//! use actix_rt::Arbiter;
//! use apub_background_jobs::{client, ClientFactory, DeliverJob};
//! use apub_core::deliver::Client;
//! use apub_openssl::OpenSsl;
//! use apub_reqwest::{ReqwestClient, SignatureConfig};
//! use background_jobs::{create_server_in_arbiter, memory_storage::Storage, WorkerConfig};
//! use openssl::{pkey::PKey, rsa::Rsa};
//! use url::Url;
//!
//! #[derive(Clone)]
//! struct State {
//! config: SignatureConfig,
//! client: reqwest::Client,
//! }
//!
//! impl<'a> ClientFactory<'a> for State {
//! type Crypto = OpenSsl;
//! type Client = ReqwestClient<'a, (), &'a OpenSsl>;
//!
//! fn client(&'a self, crypto: &'a Self::Crypto) -> Self::Client {
//! ReqwestClient::new(&self.client, (), &self.config, crypto)
//! }
//! }
//!
//! #[actix_rt::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = SignatureConfig::default();
//!
//! let arbiter = Arbiter::new();
//! let storage = Storage::new();
//! let queue_handle = create_server_in_arbiter(&arbiter, storage);
//! let client = reqwest::Client::new();
//!
//! WorkerConfig::new(move || State {
//! config: config.clone(),
//! client: client.clone(),
//! })
//! .register::<DeliverJob<State, OpenSsl>>()
//! .start_in_arbiter(&arbiter, queue_handle.clone());
//!
//! 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 = /* ... */;
//! // client::<State, _>(crypto, queue_handle.clone()).enqueue(inbox, activity)?;
//!
//! Ok(())
//! }
//! ```
#![deny(missing_docs)]
@ -12,58 +63,6 @@ use std::{
use url::Url;
/// A trait describing acquiring an HTTP Client type from Job State and crypto
///
/// ```rust
/// use actix_rt::Arbiter;
/// use apub_background_jobs::{client, ClientFactory, DeliverJob};
/// use apub_core::deliver::Client;
/// use apub_openssl::OpenSsl;
/// use apub_reqwest::{ReqwestClient, SignatureConfig};
/// use background_jobs::{create_server_in_arbiter, memory_storage::Storage, WorkerConfig};
/// use openssl::{pkey::PKey, rsa::Rsa};
/// use url::Url;
///
/// #[derive(Clone)]
/// struct State {
/// config: SignatureConfig,
/// client: reqwest::Client,
/// }
///
/// impl<'a> ClientFactory<'a> for State {
/// type Crypto = OpenSsl;
/// type Client = ReqwestClient<'a, (), &'a OpenSsl>;
///
/// fn client(&'a self, crypto: &'a Self::Crypto) -> Self::Client {
/// ReqwestClient::new(&self.client, (), &self.config, crypto)
/// }
/// }
///
/// #[actix_rt::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = SignatureConfig::default();
///
/// let arbiter = Arbiter::new();
/// let storage = Storage::new();
/// let queue_handle = create_server_in_arbiter(&arbiter, storage);
/// let client = reqwest::Client::new();
///
/// WorkerConfig::new(move || State {
/// config: config.clone(),
/// client: client.clone(),
/// })
/// .register::<DeliverJob<State, OpenSsl>>()
/// .start_in_arbiter(&arbiter, queue_handle.clone());
///
/// 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 = /* ... */;
/// // client::<State, _>(crypto, queue_handle.clone()).enqueue(inbox, activity)?;
///
/// Ok(())
/// }
/// ```
pub trait ClientFactory<'a> {
/// The cryptography implementation associated with this client
type Crypto;