Skip to content

chai-rs/http-server-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Server in Rust

A simple, extensible HTTP server implemented in Rust using async/await (Tokio). This project demonstrates basic HTTP routing, file serving, and request handling, and is suitable for learning or lightweight custom server needs.


Features

  • Async TCP Server using Tokio
  • Routing with support for dynamic parameters
  • Echo endpoint (/echo/:msg)
  • File serving (GET/POST /files/:filename)
  • User-Agent endpoint
  • Health check endpoint
  • Configurable file directory via CLI flag
  • Structured logging

Getting Started

Prerequisites

Installation

# Clone the repository
$ git clone <repo-url>
$ cd http-server-rs

# Copy .env.example to .env if needed
$ cp .env.example .env

# Build the project
$ cargo build --release

Running the Server

$ cargo run --release -- --directory ./data
  • Default address: 127.0.0.1:4221
  • Change data directory with --directory flag

API Endpoints

Health Check

  • GET /health

Echo

  • GET /echo/:msg
    • Responds with plain text echo of :msg parameter

User-Agent

  • GET /user-agent
    • Returns the User-Agent header

File Operations

  • GET /files/:filename
    • Serves file content from the configured directory
  • POST /files/:filename
    • Saves request body to file in the configured directory

Example Usage

Echo Endpoint

curl http://127.0.0.1:4221/echo/hello

File Upload

curl -X POST --data 'Hello world' http://127.0.0.1:4221/files/test.txt

File Download

curl http://127.0.0.1:4221/files/test.txt

Project Structure

http-server-rs/
├── src/
│   ├── main.rs         # Entry point
│   ├── server.rs       # Server implementation
│   ├── flags.rs        # CLI flag parsing
│   ├── request.rs      # HTTP request parsing
│   ├── response.rs     # HTTP response building
│   ├── handler/        # Routing & handler logic
│   ├── routes/         # Endpoint implementations
│   └── http/           # HTTP primitives (method, status, version)
├── Cargo.toml          # Dependencies
├── .env.example        # Example environment config
└── README.md           # Project documentation

Dependencies

  • tokio
  • clap
  • dotenv
  • env_logger
  • log
  • anyhow
  • regex
  • serde / serde_json
  • thiserror

See Cargo.toml for full list and versions.


License

This project is licensed under the MIT License.


Author

[[email protected]]

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages