1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{
errors::{ConfigError, VetisError},
server::Protocol,
VetisVirtualHosts,
};
use serde::Deserialize;
use std::{future::Future, pin::Pin};
/// A pinned future that resolves to a result of type T or a VetisError
pub type ListenerResult<'a, T> =
Pin<Box<dyn Future<Output = Result<T, VetisError>> + Send + Sync + 'a>>;
/// A trait for defining server listeners that can handle HTTP requests
pub trait Listener {
/// The type of virtual host that this listener can handle
type VirtualHost;
/// Creates a new listener with the given configuration
fn new(config: ListenerConfig) -> Self
where
Self: Sized;
/// Sets the virtual hosts for this listener
fn set_virtual_hosts(&mut self, virtual_hosts: VetisVirtualHosts<Self::VirtualHost>);
/// Starts the listener and begins accepting connections
fn listen(&mut self) -> ListenerResult<'_, ()>;
/// Stops the listener and closes all connections
fn stop(&mut self) -> ListenerResult<'_, ()>;
}
/// Builder for creating `ListenerConfig` instances.
///
/// Provides a fluent API for configuring server listeners.
///
/// # Examples
///
/// ```rust,no_run
/// use vetis::{listener::ListenerConfig, server::Protocol};
///
/// let config = ListenerConfig::builder()
/// .port(8080)
/// .protocol(Protocol::Http1)
/// .interface("127.0.0.1")
/// .build();
/// ```
#[derive(Clone)]
pub struct ListenerConfigBuilder {
port: u16,
protocol: Protocol,
interface: String,
}
impl ListenerConfigBuilder {
/// Sets the port number for the listener.
///
/// # Examples
///
/// ```rust,no_run
/// use vetis::listener::ListenerConfig;
///
/// let config = ListenerConfig::builder()
/// .port(8443)
/// .build();
/// ```
pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
}
/// Sets the network interface to bind to.
///
/// Common values:
/// - "0.0.0.0" - All interfaces
/// - "127.0.0.1" - Localhost only
/// - "::1" - IPv6 localhost
///
/// # Examples
///
/// ```rust,no_run
/// use vetis::listener::ListenerConfig;
///
/// let config = ListenerConfig::builder()
/// .interface("127.0.0.1")
/// .build();
/// ```
pub fn interface(mut self, interface: &str) -> Self {
self.interface = interface.to_string();
self
}
/// Sets the HTTP protocol for this listener.
///
/// # Examples
///
/// ```rust,no_run
/// use vetis::{listener::ListenerConfig, server::Protocol};
///
/// #[cfg(feature = "http1")]
/// let config = ListenerConfig::builder()
/// .protocol(Protocol::HTTP1)
/// .build();
/// ```
pub fn protocol(mut self, protocol: Protocol) -> Self {
self.protocol = protocol;
self
}
/// Creates the `ListenerConfig` with the configured settings.
pub fn build(self) -> Result<ListenerConfig, ConfigError> {
if self.port == 0 {
return Err(ConfigError::Listener("Port cannot be 0".to_string()));
}
if self
.interface
.is_empty()
{
return Err(ConfigError::Listener("Interface cannot be empty".to_string()));
}
Ok(ListenerConfig { port: self.port, protocol: self.protocol, interface: self.interface })
}
}
/// Configuration for a server listener.
///
/// Defines how the server should listen for incoming connections,
/// including the port, protocol, interface, and SSL settings.
///
/// # Examples
///
/// ```rust,no_run
/// use vetis::{listener::ListenerConfig, server::Protocol};
///
/// let config = ListenerConfig::builder()
/// .port(8443)
/// .protocol(Protocol::Http1)
/// .interface("0.0.0.0")
/// .build()
/// .unwrap();
///
/// println!("Listening on port {}", config.port());
/// ```
#[derive(Clone, Deserialize)]
pub struct ListenerConfig {
port: u16,
protocol: Protocol,
interface: String,
}
impl ListenerConfig {
/// Creates a new `ListenerConfigBuilder` with default settings.
///
/// Default values:
/// - port: 80
/// - ssl: false
/// - protocol: HTTP1 (if available)
/// - interface: "0.0.0.0"
///
/// # Examples
///
/// ```rust,no_run
/// use vetis::listener::ListenerConfig;
///
/// let builder = ListenerConfig::builder();
/// let config = builder.port(8080).build();
/// ```
pub fn builder() -> ListenerConfigBuilder {
ListenerConfigBuilder { port: 80, protocol: Protocol::Http1, interface: "0.0.0.0".into() }
}
/// Returns the port number.
pub fn port(&self) -> u16 {
self.port
}
/// Returns the HTTP protocol.
pub fn protocol(&self) -> &Protocol {
&self.protocol
}
/// Returns the network interface.
pub fn interface(&self) -> &str {
&self.interface
}
}