#[cfg(feature = "actix")]
pub mod actix;
#[cfg(feature = "axum")]
pub mod axum;
use std::sync::{Arc, Mutex};
use apalis_rtui_types::CapabilityMap;
use crate::api::capability::SharedState;
#[cfg(feature = "events")]
use crate::api::sse::EventRing;
pub const DEFAULT_MAX_BODY_SIZE: usize = 1024 * 1024;
pub trait RegisterRoute<B, T> {
#[must_use]
fn register(self, backend: B) -> Self;
}
#[derive(Clone, Debug)]
pub struct ApiBuilder<R> {
router: R,
#[allow(unused)]
root: bool,
pub(crate) state: Arc<Mutex<SharedState>>,
#[cfg(feature = "events")]
pub(crate) ring: Arc<Mutex<EventRing>>,
pub(crate) max_body_size: usize,
pub(crate) require_csrf_token: bool,
}
impl<R> ApiBuilder<R> {
pub fn new(router: R) -> Self {
Self {
router,
root: true,
state: Arc::new(Mutex::new(SharedState::default())),
#[cfg(feature = "events")]
ring: EventRing::create(),
max_body_size: DEFAULT_MAX_BODY_SIZE,
require_csrf_token: false,
}
}
pub fn new_with_router(router: R, register_root: bool) -> Self {
Self {
router,
root: register_root,
state: Arc::new(Mutex::new(SharedState::default())),
#[cfg(feature = "events")]
ring: EventRing::create(),
max_body_size: DEFAULT_MAX_BODY_SIZE,
require_csrf_token: false,
}
}
pub fn build(self) -> R {
self.router
}
#[must_use]
pub fn capabilities(&self) -> CapabilityMap {
self.state
.lock()
.expect("invariant: ApiBuilder's capability state mutex is never held across a panic")
.capabilities
.clone()
}
#[must_use]
pub fn max_body_size(mut self, bytes: usize) -> Self {
self.max_body_size = bytes;
self
}
#[must_use]
pub fn require_csrf_token(mut self, enabled: bool) -> Self {
self.require_csrf_token = enabled;
self
}
}