Skip to main content

tap_http/
config.rs

1//! Configuration for the TAP HTTP server.
2
3use crate::event::EventLoggerConfig;
4use serde::{Deserialize, Serialize};
5use std::time::Duration;
6
7/// Configuration for the TAP HTTP server.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct TapHttpConfig {
10    /// The host address to bind to.
11    pub host: String,
12
13    /// The port to bind to.
14    pub port: u16,
15
16    /// The endpoint path for receiving DIDComm messages.
17    pub didcomm_endpoint: String,
18
19    /// Optional rate limiting configuration.
20    pub rate_limit: Option<RateLimitConfig>,
21
22    /// Optional TLS configuration.
23    pub tls: Option<TlsConfig>,
24
25    /// Default timeout for outbound HTTP requests in seconds.
26    pub request_timeout_secs: u64,
27
28    /// Optional event logger configuration.
29    /// If not provided, no event logging will be performed.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub event_logger: Option<EventLoggerConfig>,
32
33    /// Enable the `/.well-known/did.json` endpoint for did:web hosting.
34    /// When enabled, the server resolves the HTTP Host header to a `did:web`
35    /// DID and serves the corresponding DID document.
36    pub enable_web_did: bool,
37
38    /// Maximum number of agents that can be auto-created via the web DID endpoint.
39    /// Prevents denial-of-service via unbounded agent creation.
40    pub max_agents: usize,
41}
42
43/// Configuration for rate limiting.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct RateLimitConfig {
46    /// Maximum number of requests per window.
47    pub max_requests: u32,
48
49    /// Time window in seconds.
50    pub window_secs: u64,
51}
52
53/// Configuration for TLS.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct TlsConfig {
56    /// Path to the certificate file.
57    pub cert_path: String,
58
59    /// Path to the key file.
60    pub key_path: String,
61}
62
63impl Default for TapHttpConfig {
64    fn default() -> Self {
65        Self {
66            host: "127.0.0.1".to_string(),
67            port: 8000,
68            didcomm_endpoint: "/didcomm".to_string(),
69            rate_limit: None,
70            tls: None,
71            request_timeout_secs: 30,
72            event_logger: Some(EventLoggerConfig::default()),
73            enable_web_did: false,
74            max_agents: 100,
75        }
76    }
77}
78
79impl TapHttpConfig {
80    /// Returns the full server address as a string (e.g., "127.0.0.1:8000").
81    pub fn server_addr(&self) -> String {
82        format!("{}:{}", self.host, self.port)
83    }
84
85    /// Returns the full URL for the DIDComm endpoint.
86    pub fn didcomm_url(&self, secure: bool) -> String {
87        let protocol = if secure || self.tls.is_some() {
88            "https"
89        } else {
90            "http"
91        };
92        format!(
93            "{}://{}:{}{}",
94            protocol, self.host, self.port, self.didcomm_endpoint
95        )
96    }
97
98    /// Returns the request timeout as a Duration.
99    pub fn request_timeout(&self) -> Duration {
100        Duration::from_secs(self.request_timeout_secs)
101    }
102}