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
//! # HTTP Server Middleware
//!
//! This module provides middleware configurations and utilities for Axum HTTP servers
//! using StreamWeave. It includes common middleware patterns like CORS, authentication,
//! logging, and rate limiting.
//!
//! ## Example
//!
//! ```rust,no_run
//! use crate::http_server::middleware;
//! use axum::Router;
//!
//! let app = Router::new()
//! .layer(middleware::cors_layer())
//! .layer(middleware::logging_layer());
//! ```
use HeaderValue;
use ;
/// Creates a CORS middleware layer with permissive defaults.
///
/// This middleware allows all origins, methods, and headers by default.
/// For production use, you should configure it more restrictively.
///
/// ## Example
///
/// ```rust,no_run
/// use crate::http_server::middleware;
/// use axum::Router;
///
/// let app = Router::new()
/// .layer(middleware::cors_layer());
/// ```
/// Creates a CORS middleware layer with specific allowed origins.
///
/// ## Example
///
/// ```rust,no_run
/// use crate::http_server::middleware;
/// use axum::Router;
///
/// let app = Router::new()
/// .layer(middleware::cors_layer_with_origins(vec![
/// "http://localhost:3000".parse().unwrap(),
/// "https://example.com".parse().unwrap(),
/// ]));
/// ```
/// Creates a logging middleware layer using tracing.
///
/// This middleware logs all incoming requests and responses using the `tracing` crate.
/// It includes request method, path, status code, and duration.
///
/// ## Example
///
/// ```rust,no_run
/// use crate::http_server::middleware;
/// use axum::Router;
///
/// let app = Router::new()
/// .layer(middleware::logging_layer());
/// ```
/// Example authentication middleware function that extracts Bearer tokens.
///
/// This is a basic example. For production use, you should implement proper
/// token validation and user authentication.
///
/// ## Example
///
/// ```rust,no_run
/// use crate::http_server::middleware;
/// use axum::{Router, extract::Request, middleware::Next, response::Response};
/// use axum::body::Body;
///
/// async fn auth_middleware(request: Request, next: Next) -> Response<Body> {
/// // Extract and validate token
/// if let Some(auth_header) = request.headers().get("authorization") {
/// if let Ok(auth_str) = auth_header.to_str() {
/// if auth_str.starts_with("Bearer ") {
/// let token = &auth_str[7..];
/// // Validate token here
/// return next.run(request).await;
/// }
/// }
/// }
///
/// // Return 401 if no valid token
/// axum::response::Response::builder()
/// .status(axum::http::StatusCode::UNAUTHORIZED)
/// .body(Body::from("Unauthorized"))
/// .unwrap()
/// }
///
/// let app = Router::new()
/// .route_layer(axum::middleware::from_fn(auth_middleware));
/// ```
pub async
/// Creates a rate limiting middleware layer.
///
/// This is a basic example. For production use, you should use a proper
/// rate limiting library like `tower-governor` or `tower-ratelimit`.
///
/// ## Example
///
/// ```rust,no_run
/// use crate::http_server::middleware;
/// use axum::Router;
///
/// // Note: This is a placeholder - implement actual rate limiting
/// // using a library like tower-governor
/// ```
/// Creates a middleware stack with common middleware applied.
///
/// This applies CORS, logging, and other common middleware in the correct order.
///
/// ## Example
///
/// ```rust,no_run
/// use crate::http_server::middleware;
/// use axum::Router;
///
/// let app = Router::new()
/// .layer(middleware::common_middleware_stack());
/// ```
+ Clone + Send + Sync + 'static