adk_server/
config.rs

1use std::{sync::Arc, time::Duration};
2
3/// Security configuration for the ADK server.
4#[derive(Clone, Debug)]
5pub struct SecurityConfig {
6    /// Allowed origins for CORS (empty = allow all, which is NOT recommended for production)
7    pub allowed_origins: Vec<String>,
8    /// Maximum request body size in bytes (default: 10MB)
9    pub max_body_size: usize,
10    /// Request timeout duration (default: 30 seconds)
11    pub request_timeout: Duration,
12    /// Whether to include detailed error messages in responses (default: false for production)
13    pub expose_error_details: bool,
14}
15
16impl Default for SecurityConfig {
17    fn default() -> Self {
18        Self {
19            allowed_origins: Vec::new(), // Empty = permissive (for dev), should be configured for prod
20            max_body_size: 10 * 1024 * 1024, // 10MB
21            request_timeout: Duration::from_secs(30),
22            expose_error_details: false,
23        }
24    }
25}
26
27impl SecurityConfig {
28    /// Create a development configuration (permissive CORS, detailed errors)
29    pub fn development() -> Self {
30        Self {
31            allowed_origins: Vec::new(),
32            max_body_size: 10 * 1024 * 1024,
33            request_timeout: Duration::from_secs(60),
34            expose_error_details: true,
35        }
36    }
37
38    /// Create a production configuration with specific allowed origins
39    pub fn production(allowed_origins: Vec<String>) -> Self {
40        Self {
41            allowed_origins,
42            max_body_size: 10 * 1024 * 1024,
43            request_timeout: Duration::from_secs(30),
44            expose_error_details: false,
45        }
46    }
47}
48
49/// Configuration for the ADK server.
50#[derive(Clone)]
51pub struct ServerConfig {
52    pub agent_loader: Arc<dyn adk_core::AgentLoader>,
53    pub session_service: Arc<dyn adk_session::SessionService>,
54    pub artifact_service: Option<Arc<dyn adk_artifact::ArtifactService>>,
55    pub span_exporter: Option<Arc<adk_telemetry::AdkSpanExporter>>,
56    pub backend_url: Option<String>,
57    pub security: SecurityConfig,
58}
59
60impl ServerConfig {
61    pub fn new(
62        agent_loader: Arc<dyn adk_core::AgentLoader>,
63        session_service: Arc<dyn adk_session::SessionService>,
64    ) -> Self {
65        Self {
66            agent_loader,
67            session_service,
68            artifact_service: None,
69            span_exporter: None,
70            backend_url: None,
71            security: SecurityConfig::default(),
72        }
73    }
74
75    pub fn with_artifact_service(
76        mut self,
77        artifact_service: Arc<dyn adk_artifact::ArtifactService>,
78    ) -> Self {
79        self.artifact_service = Some(artifact_service);
80        self
81    }
82
83    pub fn with_artifact_service_opt(
84        mut self,
85        artifact_service: Option<Arc<dyn adk_artifact::ArtifactService>>,
86    ) -> Self {
87        self.artifact_service = artifact_service;
88        self
89    }
90
91    pub fn with_backend_url(mut self, backend_url: impl Into<String>) -> Self {
92        self.backend_url = Some(backend_url.into());
93        self
94    }
95
96    pub fn with_security(mut self, security: SecurityConfig) -> Self {
97        self.security = security;
98        self
99    }
100
101    pub fn with_span_exporter(
102        mut self,
103        span_exporter: Arc<adk_telemetry::AdkSpanExporter>,
104    ) -> Self {
105        self.span_exporter = Some(span_exporter);
106        self
107    }
108
109    /// Configure allowed CORS origins
110    pub fn with_allowed_origins(mut self, origins: Vec<String>) -> Self {
111        self.security.allowed_origins = origins;
112        self
113    }
114
115    /// Configure maximum request body size
116    pub fn with_max_body_size(mut self, size: usize) -> Self {
117        self.security.max_body_size = size;
118        self
119    }
120
121    /// Configure request timeout
122    pub fn with_request_timeout(mut self, timeout: Duration) -> Self {
123        self.security.request_timeout = timeout;
124        self
125    }
126
127    /// Enable detailed error messages (for development only)
128    pub fn with_error_details(mut self, expose: bool) -> Self {
129        self.security.expose_error_details = expose;
130        self
131    }
132}