-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_server.rs
More file actions
67 lines (56 loc) · 1.86 KB
/
test_server.rs
File metadata and controls
67 lines (56 loc) · 1.86 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
use std::time::Duration;
use axum::{Json, Router, extract::Path, http::StatusCode, routing::get};
use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
async fn root() -> &'static str {
"OK"
}
async fn delay(Path(ms): Path<u64>) -> &'static str {
tokio::time::sleep(Duration::from_millis(ms)).await;
"OK"
}
async fn status(Path(code): Path<u16>) -> StatusCode {
StatusCode::from_u16(code).unwrap_or(StatusCode::BAD_REQUEST)
}
async fn get_user(Path(user_id): Path<u64>) -> Json<UserResponse> {
Json(UserResponse {
user_id,
message: "User retrieved".to_string(),
})
}
#[derive(Deserialize)]
struct CreateUserRequest {
user_id: u64,
worker: usize,
}
#[derive(Serialize)]
struct UserResponse {
user_id: u64,
message: String,
}
async fn create_user(Json(payload): Json<CreateUserRequest>) -> (StatusCode, Json<UserResponse>) {
(
StatusCode::CREATED,
Json(UserResponse {
user_id: payload.user_id,
message: format!("User created by worker {}", payload.worker),
}),
)
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(root))
.route("/delay/{ms}", get(delay))
.route("/status/{code}", get(status))
.route("/user/{user_id}", get(get_user).post(create_user));
let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
println!("Test server running on http://127.0.0.1:3000");
println!("Endpoints:");
println!(" GET / - Returns 'OK'");
println!(" GET /delay/:ms - Returns 'OK' after :ms milliseconds");
println!(" GET /status/:code - Returns the specified HTTP status code");
println!(" GET /user/:id - Returns user info as JSON");
println!(" POST /user/:id - Creates user with JSON body");
axum::serve(listener, app).await.unwrap();
}