This project demonstrates a web API built using Warp and Rust. The API uses PostgreSQL for database operations and includes basic user management functionalities with password encryption and JWT authentication. Environment variables are used for configuration, and Warp provides an efficient, asynchronous web framework for handling requests.
.
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│ ├── controllers
│ │ └── mod.rs
│ ├── errors
│ │ └── mod.rs
│ ├── helpers
│ │ └── mod.rs
│ ├── main.rs
│ ├── middlewares
│ │ └── mod.rs
│ └── routes
│ └── mod.rs
└── todo.md
- Password Encryption: Uses Argon2 for securely hashing and verifying passwords.
- JWT Authentication: Implements JSON Web Tokens (JWT) for secure user authentication.
This project uses PostgreSQL for database operations. To run PostgreSQL in a Docker container, use the following command:
docker run -e POSTGRES_PASSWORD=<password> -p 5432:5432 -d postgresThis command sets up a PostgreSQL instance with the password choosed, mapping port 5432 on the container to port 5432 on your host machine.
- Create a
.envfile in the project root and add the necessary environment variables:
ADMIN_PASSWORD='12345'
DATABASE_URL='postgres://postgres:12345@localhost:5432/postgres'
JWT_SECRET='your_jwt_secret_key'
PORT=8080- The environment variables are used as follows:
DATABASE_URL: Specifies the connection string for PostgreSQL. It includes the database user, password, host, port, and database name. In this example, it connects to a PostgreSQL instance running locally with the default port.JWT_SECRET: Used for signing and verifying JSON Web Tokens (JWT). Replace'your_jwt_secret_key'with a secure key for your application.ADMIN_PASSWORD: Used to create an initial admin user. Ensure this password is secure and properly managed.PORT: The port on which the server will listen for incoming connections.
To run the project locally, follow these steps:
- Install project dependencies using Cargo:
cargo build- Run the server:
cargo runThis project provides the following API endpoints:
| Endpoint | Description | HTTP Method |
|---|---|---|
/authenticate |
User authentication endpoint, requires email and password | POST |
/status |
Check server status | GET |
/users/create_user |
Create a new user (requires authentication) | POST |
/users/delete_user/{id} |
Delete a user by id (requires authentication) | DELETE |
/users/get_users |
Retrieve a list of all users (requires authentication) | GET |
/users/update_user/{id} |
Update a user by id (requires authentication) | PUT |
-
/authenticate: Provides a JWT token upon successful authentication. The token must be included in theAuthorizationheader for requests to protected endpoints. -
Protected Endpoints: All endpoints except
/statusand/authenticaterequire the user to be authenticated. Ensure that requests to these endpoints include a valid JWT token in theAuthorizationheader.



