example-webserver-rs

Crates.ioexample-webserver-rs
lib.rsexample-webserver-rs
version0.1.1
sourcesrc
created_at2024-09-06 16:30:58.096251
updated_at2024-09-10 08:10:17.813068
descriptionBuilding Rust webserver application with Axum for fun
homepage
repositoryhttps://github.com/rpietrusinski/example-webserver-rs
max_upload_size
id1366133
size44,995
(rpietrusinski)

documentation

README

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

  1. GET /
curl -X GET 127.0.0.1:3000
  1. GET /json
curl -X GET 127.0.0.1:3000/json
  1. GET /json-counter
curl -X GET 127.0.0.1:3000/json-counter
  1. POST /append
curl \
    -X POST 127.0.0.1:3000/append \
    -H "Content-Type: application/json" \
    -d '{"data": "hello"}'
  1. GET /rnd
curl -X GET 127.0.0.1:3000/rnd
  1. GET /country
curl -X GET "127.0.0.1:3000/country?name=usa"
Commit count: 0

cargo fmt