#web-server #axum #json #handler #query #traits #country

bin+lib example-webserver-rs

Building Rust webserver application with Axum for fun

2 releases

new 0.1.1 Sep 10, 2024
0.1.0 Sep 6, 2024

#622 in HTTP server

Download history 108/week @ 2024-09-02

108 downloads per month

Apache-2.0

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

  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"

Dependencies

~8–20MB
~280K SLoC