1use crate::types::*;
7use async_trait::async_trait;
8use ferrum_types::{InferenceRequest, Result};
9use std::time::Duration;
10
11#[async_trait]
13pub trait HttpServer: Send + Sync {
14 async fn start(&self, config: &ServerConfig) -> Result<()>;
16
17 async fn stop(&self, timeout: Duration) -> Result<()>;
19
20 fn is_running(&self) -> bool;
22
23 fn address(&self) -> Option<std::net::SocketAddr>;
25
26 fn register_handler(
28 &mut self,
29 path: &str,
30 method: HttpMethod,
31 handler: Box<dyn RequestHandler>,
32 );
33
34 fn register_middleware(&mut self, middleware: Box<dyn Middleware>);
36
37 fn get_metrics(&self) -> ServerMetrics;
39
40 async fn health_check(&self) -> HealthStatus;
42}
43
44#[async_trait]
46pub trait RequestHandler: Send + Sync {
47 async fn handle(&self, request: HttpRequest, context: RequestContext) -> Result<HttpResponse>;
49
50 fn name(&self) -> &str;
52
53 fn can_handle(&self, request: &HttpRequest) -> bool;
55}
56
57pub trait ResponseBuilder: Send + Sync {
59 fn ok(&self, body: serde_json::Value) -> HttpResponse;
61
62 fn error(&self, code: StatusCode, message: &str) -> HttpResponse;
64
65 fn streaming(&self, content_type: &str) -> HttpResponse;
67
68 fn with_headers(&self, body: serde_json::Value, headers: Headers) -> HttpResponse;
70
71 fn redirect(&self, location: &str, permanent: bool) -> HttpResponse;
73}
74
75#[async_trait]
77pub trait StreamingHandler: Send + Sync {
78 async fn handle_stream(
80 &self,
81 request: InferenceRequest,
82 sender: Box<dyn StreamSender>,
83 ) -> Result<()>;
84
85 fn stream_config(&self) -> &StreamConfig;
87}
88
89#[async_trait]
91pub trait StreamSender: Send + Sync {
92 async fn send_chunk(&self, chunk: &str) -> Result<()>;
94
95 async fn send_json(&self, data: &serde_json::Value) -> Result<()>;
97
98 async fn close(&self) -> Result<()>;
100
101 fn is_closed(&self) -> bool;
103}
104
105#[async_trait]
107pub trait Middleware: Send + Sync {
108 async fn before_request(
110 &self,
111 request: &mut HttpRequest,
112 context: &mut RequestContext,
113 ) -> Result<()>;
114
115 async fn after_response(
117 &self,
118 request: &HttpRequest,
119 response: &mut HttpResponse,
120 context: &RequestContext,
121 ) -> Result<()>;
122
123 async fn on_error(
125 &self,
126 error: &ferrum_types::FerrumError,
127 context: &RequestContext,
128 ) -> Option<HttpResponse>;
129
130 fn name(&self) -> &str;
132
133 fn priority(&self) -> i32;
135}
136
137pub trait MiddlewareStack: Send + Sync {
139 fn add(&mut self, middleware: Box<dyn Middleware>);
141
142 fn remove(&mut self, name: &str) -> bool;
144
145 fn get(&self, name: &str) -> Option<&dyn Middleware>;
147
148 fn clear(&mut self);
150
151 fn len(&self) -> usize;
153}
154
155#[async_trait]
157pub trait AuthProvider: Send + Sync {
158 async fn authenticate(&self, request: &HttpRequest) -> Result<AuthResult>;
160
161 async fn validate_api_key(&self, api_key: &str) -> Result<ClientInfo>;
163
164 async fn validate_jwt(&self, token: &str) -> Result<TokenClaims>;
166
167 fn scheme(&self) -> AuthScheme;
169}
170
171#[async_trait]
173pub trait RateLimiter: Send + Sync {
174 async fn check_limit(&self, client_id: &str, endpoint: &str) -> Result<RateLimitResult>;
176
177 async fn record_request(&self, client_id: &str, endpoint: &str) -> Result<()>;
179
180 async fn get_status(&self, client_id: &str) -> Result<RateLimitStatus>;
182
183 async fn reset_limits(&self, client_id: &str) -> Result<()>;
185}
186
187pub trait RequestValidator: Send + Sync {
189 fn validate_inference_request(&self, request: &InferenceRequest) -> Result<()>;
191
192 fn validate_chat_request(&self, request: &crate::openai::ChatCompletionsRequest) -> Result<()>;
194
195 fn validate_parameters(&self, params: &serde_json::Value) -> Result<()>;
197
198 fn get_rules(&self) -> &ValidationRules;
200}
201
202#[async_trait]
204pub trait HealthChecker: Send + Sync {
205 async fn check_health(&self) -> HealthStatus;
207
208 async fn check_component(&self, component: &str) -> ComponentHealth;
210
211 fn config(&self) -> &HealthCheckConfig;
213}
214
215#[async_trait]
217pub trait MetricsCollector: Send + Sync {
218 async fn record_request(
220 &self,
221 request: &HttpRequest,
222 response: &HttpResponse,
223 duration: Duration,
224 );
225
226 async fn record_error(&self, error: &ferrum_types::FerrumError, endpoint: &str);
228
229 fn get_metrics(&self) -> ServerMetrics;
231
232 async fn reset_metrics(&self) -> Result<()>;
234}
235
236#[async_trait]
238pub trait ServerLifecycle: Send + Sync {
239 async fn initialize(&self) -> Result<()>;
241
242 async fn start_services(&self) -> Result<()>;
244
245 async fn stop_services(&self, timeout: Duration) -> Result<()>;
247
248 async fn graceful_shutdown(&self, signal: ShutdownSignal) -> Result<()>;
250
251 fn get_state(&self) -> LifecycleState;
253}