use crate::annotation::Annotation;
use crate::config::GarrisonConfig;
use crate::context::tenant::{HeaderTenantResolver, TenantResolver};
use crate::router::{DefaultGarrisonInterceptor, GarrisonInterceptor};
use std::collections::HashMap;
use std::sync::Arc;
use super::{GarrisonMiddleware, GarrisonRouter};
impl GarrisonRouter {
pub fn new(config: Arc<GarrisonConfig>) -> Self {
Self {
rules: HashMap::new(),
interceptor: Arc::new(DefaultGarrisonInterceptor),
config,
tenant_resolver: None,
handler_timeout: None,
}
}
pub fn with_interceptor<I: GarrisonInterceptor + 'static>(mut self, interceptor: I) -> Self {
self.interceptor = Arc::new(interceptor);
self
}
pub fn with_handler_timeout(mut self, timeout: std::time::Duration) -> Self {
self.handler_timeout = Some(timeout);
self
}
pub fn with_tenant_resolver<T: TenantResolver + 'static>(mut self, resolver: T) -> Self {
self.tenant_resolver = Some(Arc::new(resolver));
self
}
pub fn with_header_tenant(mut self) -> Self {
self.tenant_resolver = Some(Arc::new(HeaderTenantResolver));
self
}
pub fn route_protected(mut self, path: &str, annotation: Annotation) -> Self {
if path.is_empty() || !path.starts_with('/') {
tracing::error!(
path = %path,
"{}",
crate::i18n::translate_detail("logs-router-invalid-path-protected", &[])
);
return self;
}
if self.rules.contains_key(path) {
tracing::warn!(
path = %path,
"{}",
crate::i18n::translate_detail("logs-router-duplicate-path-override", &[])
);
}
self.rules.insert(path.to_string(), annotation);
self
}
pub fn into_middleware(self) -> GarrisonMiddleware {
GarrisonMiddleware {
rules: Arc::new(self.rules),
interceptor: self.interceptor,
config: self.config,
tenant_resolver: self.tenant_resolver,
handler_timeout: self.handler_timeout,
}
}
}
impl Default for GarrisonRouter {
fn default() -> Self {
Self::new(Arc::new(GarrisonConfig::default_config()))
}
}