This repository demonstrates a basic web server built using the Axum web framework in Rust. It includes routes with
different functionalities, structured logging with tracing
, and JSON handling.
- Simple web server setup with
tokio
andaxum
- Support for:
- Basic routes
- JSON requests and responses
- Query and path parameter extraction
- Structured logging using
tracing
- Configurable port using environment variables
.
├── Cargo.lock # Lock file for Rust dependencies
├── Cargo.toml # Project dependencies and metadata
└── src
└── main.rs # Main application code
- Rust (Ensure you have
cargo
installed) - A terminal for running the application
-
Clone the repository:
git clone https://github.com/nawodyaishan/axum-starter cd axum-starter
-
Build the project:
cargo build
-
Run the server:
cargo run
-
Access the application in your browser or via
curl
:- Root route: http://127.0.0.1:3000/
- Hello World: http://127.0.0.1:3000/hello-world
- Demo HTML: http://127.0.0.1:3000/demo.html
- Demo JSON: http://127.0.0.1:3000/demo.json
The server port can be configured using the PORT
environment variable. By default, the application runs on port
3000
.
To set a custom port:
-
On Linux/macOS:
PORT=8080 cargo run
-
On Windows (Command Prompt):
set PORT=8080 && cargo run
Route | HTTP Method | Description |
---|---|---|
/ |
GET | Displays a welcome message |
/hello-world |
GET | Returns a Hello World message |
/demo.html |
GET | Serves a simple HTML response |
/demo.json |
GET | Returns a JSON response |
/demo.json |
PUT | Accepts and logs JSON data from the request |
/items |
GET | Displays query parameters |
/items/:id |
GET | Displays the provided path parameter |
curl http://127.0.0.1:3000/
Output:
Welcome to the Axum server!
curl http://127.0.0.1:3000/demo.json
Output:
{
"message": "This is a demo JSON response",
"status": "success"
}
curl -X PUT -H "Content-Type: application/json" -d '{"key":"value"}' http://127.0.0.1:3000/demo.json
Output:
Received JSON data: {"key":"value"}
curl http://127.0.0.1:3000/items?key=value
Output:
Query parameters: {"key": "value"}
curl http://127.0.0.1:3000/items/42
Output:
Item ID: 42
This project uses tracing
for structured logging. Example log output:
INFO Server running at http://127.0.0.1:3000
INFO Accessed /hello-world route
INFO Received PUT request on /demo.json with data {"key":"value"}
Logs provide valuable debugging information and insights into request handling.
This project uses the following dependencies:
Dependency | Version | Functionality |
---|---|---|
axum |
0.7 | A web framework for building asynchronous applications in Rust. Used for creating routes, handling requests, and managing responses. |
tokio |
1 | An asynchronous runtime for Rust. Provides the foundation for running async tasks, including the Axum server. |
tracing |
0.1.41 | For structured and leveled logging. Used to provide detailed logs for debugging and monitoring. |
tracing-subscriber |
0.3.19 | A library for managing and formatting logs from tracing . Enhances log output readability. |
serde_json |
1.0.133 | A library for serializing and deserializing JSON. Used for handling JSON requests and responses in the application. |
anyhow |
1 | Simplifies error handling in Rust with detailed context and backtraces. Used for managing errors in the project. |