Skip to content

phosae/rust-hands-on

Repository files navigation

Hands-On Rust HTTP Server Based on Hyper 1.0+

play with

cargo run

v1: write a stateless car server

copy & paste codes

  • arealesramirez/rust-rest-api-hyper for apis
    • GET /cars to fetch all cars
    • GET /cars/:id = to fetch a specific car
    • POST /cars = to create a new car
  • [email protected] examples
    • especially web_api,
    • learn some serde_json...
    • learn the type trick
      • type GenericError = Box<dyn std::error::Error + Send + Sync>;
      • type Result<T> = std::result::Result<T, GenericError>;
      • type BoxBody = http_body_util::combinators::BoxBody<Bytes, hyper::Error>;

v2: store cars in memory

  • POST /cars
  • PUT /cars/{id}
  • GET /cars, /cars/{id}
  • DELETE /cars, /cars/{id}
  • generate cars 🚀🚀🚀 hey -z 10s -cpus 4 -c 4 -d '{"brand":"Tesla", "model": "Y", "year": 2023}' -m POST http:https://127.0.0.1:9100/cars
  • see CHANGELOG-v2

v3

sqlite carstore

store cars in sqlite when run with env DB_TYPE=sqlite

ctl

wrap some qappctl command as HTTP service in path /ctl/**. Some thing just like qappctl-shim

HTTP router: mock Golang HTTP Handler interface

register routes like Golang with ibraheemdev/matchit and our Handler implementation

// Go
router := mux.NewRouter()
router.HandleFunc("/images", server.listImagesHandler).Methods("GET")
router.HandleFunc("/images", server.pushImageHandler).Methods("POST")
// Rust
let mut mux: HashMap<Method, matchit::Router<HandlerFn>> = Router::new();
add_route(&mut mux, "/ctl/images", Method::GET, BoxCloneHandler::new(handler_fn(Svc::list_images)));
add_route(&mut mux, "/ctl/images", Method::POST, BoxCloneHandler::new(handler_fn(Svc::push_image)));

See CHANGELOG-v3

v4

HTTP Service middleware

  • GET /test/sleep/:duration to trigger a timeout
  • add timeout middleware, curl -i 127.1:9100/test/sleep/3100
  • add log middleware
  • add auth middleware, curl -i 127.1:9100/test/sleep/3100 -H 'Bearer: zenx'
  • add MapResponse middleware
  • add error_handling middleware

See CHANGELOG-v4

WebAssembly

add a demo wasi/wasm32 HTTP server, car-demo, based on v2

v5 (doing)

  • replace qappctl command with docker, make path /ctl/** more common

[TODO]