routing/
routing.rs

1use spike::{
2    http::{Method, StatusCode},
3    response::IntoResponse,
4    routing::{get, put},
5    Router, Server,
6};
7
8fn main() -> std::io::Result<()> {
9    let router = Router::new()
10        .route("/hello", get(hello_world).post(hello_post))
11        .route("/hello", put(put_hello_world).any(any_hello))
12        .route("/hi", get(|| "Hi world"))
13        .route("/world", get(world));
14
15    Server::bind("0.0.0.0:4444").serve(router)
16}
17
18fn hello_world(method: Method, body: String) -> impl IntoResponse {
19    (StatusCode::OK, format!("Hello: {method} - {body}"))
20}
21
22fn put_hello_world(method: Method, body: String) -> impl IntoResponse {
23    (StatusCode::CREATED, format!("Hello: {method} - {body}"))
24}
25
26fn hello_post(body: String) -> impl IntoResponse {
27    (StatusCode::CREATED, format!("POST Hello: {body}"))
28}
29
30fn any_hello(method: Method, body: String) -> impl IntoResponse {
31    (StatusCode::CREATED, format!("Any Hello: {method} - {body}"))
32}
33
34fn world() -> impl IntoResponse {
35    (StatusCode::OK, format!("World"))
36}