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 backend_url: Option<String>,
56    pub security: SecurityConfig,
57}
58
59impl ServerConfig {
60    pub fn new(
61        agent_loader: Arc<dyn adk_core::AgentLoader>,
62        session_service: Arc<dyn adk_session::SessionService>,
63    ) -> Self {
64        Self {
65            agent_loader,
66            session_service,
67            artifact_service: None,
68            backend_url: None,
69            security: SecurityConfig::default(),
70        }
71    }
72
73    pub fn with_artifact_service(
74        mut self,
75        artifact_service: Arc<dyn adk_artifact::ArtifactService>,
76    ) -> Self {
77        self.artifact_service = Some(artifact_service);
78        self
79    }
80
81    pub fn with_artifact_service_opt(
82        mut self,
83        artifact_service: Option<Arc<dyn adk_artifact::ArtifactService>>,
84    ) -> Self {
85        self.artifact_service = artifact_service;
86        self
87    }
88
89    pub fn with_backend_url(mut self, backend_url: impl Into<String>) -> Self {
90        self.backend_url = Some(backend_url.into());
91        self
92    }
93
94    pub fn with_security(mut self, security: SecurityConfig) -> Self {
95        self.security = security;
96        self
97    }
98
99    /// Configure allowed CORS origins
100    pub fn with_allowed_origins(mut self, origins: Vec<String>) -> Self {
101        self.security.allowed_origins = origins;
102        self
103    }
104
105    /// Configure maximum request body size
106    pub fn with_max_body_size(mut self, size: usize) -> Self {
107        self.security.max_body_size = size;
108        self
109    }
110
111    /// Configure request timeout
112    pub fn with_request_timeout(mut self, timeout: Duration) -> Self {
113        self.security.request_timeout = timeout;
114        self
115    }
116
117    /// Enable detailed error messages (for development only)
118    pub fn with_error_details(mut self, expose: bool) -> Self {
119        self.security.expose_error_details = expose;
120        self
121    }
122}