Skip to content

lassejlv/rust-axum-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Server

A simple HTTP server built with Rust using the Axum web framework.

Features

  • Basic HTTP server with routing
  • Async/await support with Tokio
  • JSON serialization with Serde
  • Logging with Tracing
  • GET and POST endpoints
  • JSON request/response handling
  • Docker support

Getting Started

Prerequisites

  • Rust (latest stable version)
  • Cargo

Running the Server

  1. Clone or navigate to the project directory
  2. Run the server:
cargo run

The server will start on http://0.0.0.0:3000

Available Endpoints

  • GET / - Returns "Hello, World!" (plain text)
  • GET /json - Returns JSON response with message
  • POST /post - Echo JSON endpoint (returns the same JSON body sent)
  • POST /post-validate - Validates and echoes structured JSON (name: string, age: number)

Project Structure

src/
├── main.rs           # Application entry point
└── routers/
    ├── mod.rs        # Router module declarations
    └── root/
        ├── mod.rs    # Root module declarations
        ├── get.rs    # GET route handlers
        └── post.rs   # POST route handlers

Dependencies

  • axum - Web framework
  • tokio - Async runtime
  • tracing - Logging framework
  • serde - Serialization framework

Docker

Build and run with Docker:

docker build -t http-server .
docker run -p 3000:3000 http-server

Development

To run in development mode with hot reloading, you can use cargo watch:

cargo install cargo-watch
cargo watch -x run

Example Usage

Test the endpoints:

# GET endpoints
curl http://localhost:3000/
curl http://localhost:3000/json

# POST endpoints
curl -X POST http://localhost:3000/post \
  -H "Content-Type: application/json" \
  -d '{"test": "data"}'

curl -X POST http://localhost:3000/post-validate \
  -H "Content-Type: application/json" \
  -d '{"name": "John", "age": 30}'

About

A simple HTTP server built with Rust using the Axum web framework.

Resources

Stars

Watchers

Forks