feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)
This commit is contained in:
1 parent
82677455b2
commit
2e002b6176
64 files changed
+2295
-196
No files matched your search
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "eco02-web-backend-axum"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||
|
||||
[dev-dependencies]
|
||||
http-body-util = "0.1"
|
||||
tower = { version = "0.5", features = ["util"] }
|
||||
@@ -0,0 +1,18 @@
|
||||
# 02 — Web Backend z `axum` + `tokio` + `serde`
|
||||
|
||||
`axum` (oparty na `tokio` i `tower`) to najpopularniejszy nowoczesny framework backendowy w Ruście.
|
||||
|
||||
## Zakres
|
||||
* Routing HTTP (`Router::new().route(...)`)
|
||||
* Serializacja/deserializacja JSON (`axum::Json`, `serde::Serialize`, `serde::Deserialize`)
|
||||
* Kody statusu HTTP (`StatusCode`) i testowanie routera w pamięci bez otwierania portów (`tower::ServiceExt::oneshot`)
|
||||
|
||||
## Zadania (`src/lib.rs`)
|
||||
Zbuduj router w `build_router()`:
|
||||
1. `GET /health` -> zwraca `200 OK` i JSON `{"status": "ok"}`.
|
||||
2. `POST /echo` -> przyjmuje JSON `EchoRequest { message: String }`; jeśli `message.trim().is_empty()`, zwraca `400 BAD_REQUEST`, inaczej `200 OK` z `EchoResponse { echoed: message, len }`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=eco02-web-backend-axum
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct HealthResponse {
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct EchoRequest {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct EchoResponse {
|
||||
pub echoed: String,
|
||||
pub len: usize,
|
||||
}
|
||||
|
||||
async fn health_handler() -> Json<HealthResponse> {
|
||||
todo!("Zwróć Json(HealthResponse {{ status: \"ok\".into() }})")
|
||||
}
|
||||
|
||||
async fn echo_handler(Json(payload): Json<EchoRequest>) -> Result<Json<EchoResponse>, StatusCode> {
|
||||
todo!("Odrzuć pusty message jako Err(StatusCode::BAD_REQUEST), inaczej zwróć EchoResponse")
|
||||
}
|
||||
|
||||
pub fn build_router() -> Router {
|
||||
Router::new()
|
||||
.route("/health", get(health_handler))
|
||||
.route("/echo", post(echo_handler))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::{body::Body, http::Request};
|
||||
use http_body_util::BodyExt;
|
||||
use tower::ServiceExt;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_endpoint_returns_ok_json() {
|
||||
let app = build_router();
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/health")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let bytes = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let body: HealthResponse = serde_json::from_slice(&bytes).unwrap();
|
||||
assert_eq!(
|
||||
body,
|
||||
HealthResponse {
|
||||
status: String::from("ok")
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn echo_endpoint_validates_and_echoes() {
|
||||
let app = build_router();
|
||||
let ok_resp = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/echo")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(r#"{"message":"rust"}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(ok_resp.status(), StatusCode::OK);
|
||||
let bytes = ok_resp.into_body().collect().await.unwrap().to_bytes();
|
||||
let body: EchoResponse = serde_json::from_slice(&bytes).unwrap();
|
||||
assert_eq!(
|
||||
body,
|
||||
EchoResponse {
|
||||
echoed: String::from("rust"),
|
||||
len: 4
|
||||
}
|
||||
);
|
||||
|
||||
let bad_resp = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/echo")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(r#"{"message":" "}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(bad_resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user