This project is a simple web server built with Actix-web, a powerful and flexible web framework for Rust. The server demonstrates basic CRUD operations, including handling JSON input and generating random values. It also includes logging functionality to track requests.
- Greeting Endpoint: Returns a welcome message.
- Echo Endpoint: Echoes back a JSON message provided in the request.
- Reverse Endpoint: Reverses a string provided in the request.
- Random Endpoint: Generates a random number and returns it in a JSON response.
- Request Logging: Logs request details using Actix-web's logging middleware.
- Rust (1.70.0 or later)
- Cargo (Rust's package manager)
git clone https://github.com/yourusername/your-repo.git
cd your-repo
Make sure you have Rust and Cargo installed, then run:
cargo build
This will download and compile the dependencies.
To start the server, use:
cargo run
The server will start and listen on http://127.0.0.1:8080
.
-
GET /: Returns a welcome message.
- Response:
200 OK
with body"Welcome to the Rust Web server!"
- Response:
-
POST /echo: Echoes back a JSON message.
- Request Body:
{ "id": 1, "name": "John Doe", "rollno": 1234, "marks": 85, "grade": "A" }
- Response:
200 OK
with the same JSON message
- Request Body:
-
POST /reverse: Reverses a string provided in a JSON message.
- Request Body:
{ "text": "hello" }
- Response:
200 OK
with body{"text": "olleh"}
- Request Body:
-
GET /random: Generates and returns a random number.
- Response:
200 OK
with a JSON body containing the random number
- Response:
The server logs request details using Actix-web's logging middleware. To see the logs:
-
Ensure you have set the logging environment variable. You can configure this in your shell or a
.env
file.export RUST_LOG=actix_web=info
-
Run the server and observe the logs in your console.
actix-web
: Web framework for Rustserde
: Serialization and deserialization frameworkrand
: Library for generating random numbersenv_logger
: Logger initialization for environment-based configuration
This project is licensed under the MIT License. See the LICENSE file for details.