-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_server.rs
More file actions
264 lines (237 loc) · 8.89 KB
/
Copy pathhttp_server.rs
File metadata and controls
264 lines (237 loc) · 8.89 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Basic HTTP IPC server example
//!
//! This example demonstrates how to create a simple HTTP-style IPC server
//! that can handle various types of requests with routing and JSON responses.
use kode_bridge::ipc_http_server::{HttpResponse, IpcHttpServer, Router};
use kode_bridge::{Result, ServerConfig};
use serde_json::json;
use std::env;
use std::time::Duration;
use tokio::signal;
use tracing::info;
#[derive(serde::Serialize, serde::Deserialize)]
struct ApiVersion {
name: String,
version: String,
build: String,
}
#[derive(serde::Serialize, serde::Deserialize)]
struct UserData {
id: u64,
name: String,
email: String,
}
#[derive(serde::Serialize, serde::Deserialize)]
struct CreateUserRequest {
name: String,
email: String,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
println!("🚀 HTTP IPC Server Example");
println!("==========================");
// Get IPC path from environment or use default
#[cfg(unix)]
let ipc_path = env::var("CUSTOM_SOCK").unwrap_or_else(|_| "/tmp/http_server.sock".to_string());
#[cfg(windows)]
let ipc_path = env::var("CUSTOM_PIPE").unwrap_or_else(|_| r"\\.\pipe\http_server".to_string());
println!("📡 Server will listen on: {}", ipc_path);
// Configure server
let config = ServerConfig {
max_connections: 50,
read_timeout: Duration::from_secs(30),
write_timeout: Duration::from_secs(30),
max_request_size: 1024 * 1024, // 1MB
enable_logging: true,
shutdown_timeout: Duration::from_secs(5),
..Default::default()
};
// Create router with various endpoints
let router = Router::new()
// GET /version - Return API version information
.get("/version", |_ctx| async move {
let version = ApiVersion {
name: "HTTP IPC Server Example".to_string(),
version: "1.0.0".to_string(),
build: "example-build".to_string(),
};
let headers = _ctx.headers;
println!("Headers received: {:?}", headers);
HttpResponse::json(&version)
})
// GET /health - Health check endpoint
.get("/health", |_ctx| async move {
HttpResponse::json(&json!({
"status": "healthy",
"timestamp": chrono::Utc::now().to_rfc3339(),
"uptime": "available"
}))
})
// GET /users - List users (mock data)
.get("/users", |_ctx| async move {
let users = vec![
UserData {
id: 1,
name: "Alice".to_string(),
email: "alice@example.com".to_string(),
},
UserData {
id: 2,
name: "Bob".to_string(),
email: "bob@example.com".to_string(),
},
];
HttpResponse::json(&users)
})
// GET /users/{id} - Get user by ID (simplified routing)
.get("/user/1", |_ctx| async move {
let user = UserData {
id: 1,
name: "Alice".to_string(),
email: "alice@example.com".to_string(),
};
HttpResponse::json(&user)
})
// POST /users - Create a new user
.post("/users", |ctx| async move {
match ctx.json::<CreateUserRequest>() {
Ok(request) => {
// Validate request
if request.name.is_empty() || request.email.is_empty() {
return Ok(HttpResponse::builder()
.status(http::StatusCode::BAD_REQUEST)
.json(&json!({
"error": "Name and email are required"
}))?
.build());
}
// Create user (mock)
let user = UserData {
id: 999, // Mock ID
name: request.name,
email: request.email,
};
Ok(HttpResponse::builder()
.status(http::StatusCode::CREATED)
.json(&user)?
.build())
}
Err(e) => Ok(HttpResponse::builder()
.status(http::StatusCode::BAD_REQUEST)
.json(&json!({
"error": format!("Invalid JSON: {}", e)
}))?
.build()),
}
})
// POST /echo - Echo back the request body
.post("/echo", |ctx| async move {
let response_data = json!({
"method": ctx.method.to_string(),
"uri": ctx.uri.to_string(),
"headers": ctx.headers.len(),
"body_size": ctx.body.len(),
"body_text": ctx.text().unwrap_or_else(|_| "<binary data>".to_string()),
"timestamp": chrono::Utc::now().to_rfc3339()
});
HttpResponse::json(&response_data)
})
// PUT /config - Update configuration
.put("/config", |ctx| async move {
match ctx.json::<serde_json::Value>() {
Ok(config_data) => {
info!("Received config update: {}", config_data);
HttpResponse::json(&json!({
"status": "updated",
"config": config_data
}))
}
Err(e) => Ok(HttpResponse::builder()
.status(http::StatusCode::BAD_REQUEST)
.json(&json!({
"error": format!("Invalid config: {}", e)
}))?
.build()),
}
})
// DELETE /cache - Clear cache
.delete("/cache", |_ctx| async move {
Ok(HttpResponse::builder()
.status(http::StatusCode::NO_CONTENT)
.build())
});
// Create and configure server
#[cfg(unix)]
let mut server = IpcHttpServer::with_config(&ipc_path, config)?
.with_listener_mode(0o666) // Allow read/write for everyone
.router(router);
#[cfg(windows)]
let mut server = IpcHttpServer::with_config(&ipc_path, config)?
.with_listener_security_descriptor("D:(A;;GA;;;WD)") // Allow Everyone access
.router(router);
println!("🌟 Server configured with endpoints:");
println!(" GET /version - API version information");
println!(" GET /health - Health check");
println!(" GET /users - List all users");
println!(" GET /user/1 - Get user by ID");
println!(" POST /users - Create new user");
println!(" POST /echo - Echo request data");
println!(" PUT /config - Update configuration");
println!(" DELETE /cache - Clear cache");
println!();
// Start the server
let server_handle = tokio::spawn(async move {
if let Err(e) = server.serve().await {
eprintln!("Server error: {}", e);
}
});
println!("✅ Server started successfully!");
println!("📊 Use the following commands to test:");
println!();
#[cfg(unix)]
{
println!("# Test basic endpoints:");
println!("CUSTOM_SOCK={} cargo run --features=client --example request", ipc_path);
println!();
println!("# Using curl-like tools with HTTP over IPC:");
println!("# (These would work if you had an HTTP-to-IPC bridge)");
}
#[cfg(windows)]
{
println!("# Test basic endpoints:");
println!("set CUSTOM_PIPE={}", ipc_path);
println!("cargo run --features=client --example request");
println!();
}
println!("# Example client requests you can make:");
println!("GET /version - Get server version");
println!("GET /health - Check server health");
println!("GET /users - List users");
println!("POST /echo - Echo request data");
println!();
// Wait for shutdown signal
println!("🎯 Server is running. Press Ctrl+C to shutdown...");
match signal::ctrl_c().await {
Ok(()) => {
println!("🛑 Shutdown signal received");
}
Err(err) => {
eprintln!("Unable to listen for shutdown signal: {}", err);
}
}
// Attempt graceful shutdown
println!("🔄 Shutting down server...");
server_handle.abort();
// Give some time for cleanup
tokio::time::sleep(Duration::from_millis(500)).await;
println!("✅ Server stopped");
Ok(())
}
// Helper function to demonstrate server statistics
#[allow(dead_code)]
fn print_server_stats() {
// This would be called periodically to show server stats
// For now, it's just a placeholder
info!("Server statistics would be displayed here");
}