2 releases
0.1.1 | Sep 10, 2024 |
---|---|
0.1.0 | Sep 6, 2024 |
#821 in HTTP server
14KB
122 lines
Example-webserver-rs
Project aims for setting up basic webserver functionality written in Rust and providing a few ready-to-use endpoints. It serves for learning purposes and figuring out Rust libraries ecosystem. Implemented with Axum framework.
Implemented
From this project one can learn the following concepts:
- How to create HTTP listener and handle incoming traffic
- How to create endpoints executing GET/POST requests
- How to parse JSON payloads and process them
- How to share state between endpoints (either with Clone Trait or Atomic Reference Counting)
- How to parse query parameters
- What are requirements for Handler functions (FromRequest/IntoResponse traits, Extractors)
- How to call another external REST API from inside our endpoint, parse the results and return to user
Run
Command cargo run
will build the project and start the webserver on port 3000
. Below are the commands you can
execute against webserver.
Examples
- GET /
curl -X GET 127.0.0.1:3000
- GET /json
curl -X GET 127.0.0.1:3000/json
- GET /json-counter
curl -X GET 127.0.0.1:3000/json-counter
- POST /append
curl \
-X POST 127.0.0.1:3000/append \
-H "Content-Type: application/json" \
-d '{"data": "hello"}'
- GET /rnd
curl -X GET 127.0.0.1:3000/rnd
- GET /country
curl -X GET "127.0.0.1:3000/country?name=usa"
Dependencies
~9–20MB
~269K SLoC