This project demonstrates a web API built using Actix Web and Rust. The API uses SQLite (in-memory) for database operations, and includes basic user management functionalities with password encryption and JWT authentication. Environment variables are used for configuration, and Actix provides high-performance web handling.
.
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── controllers
│ │ ├── authentication.rs
│ │ ├── mod.rs
│ │ └── users.rs
│ ├── main.rs
│ ├── types
│ │ ├── authentication.rs
│ │ ├── mod.rs
│ │ └── users.rs
│ └── utils
│ └── 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 an in-memory SQLite database provided by rusqlite
. The database schema is created dynamically during runtime.
To configure the project, follow these steps:
-
Create a
.env
file in the project root and add the necessary environment variables:HOST='127.0.0.1' JWT_SECRET_KEY='jwt_secret_key' NEW_ADMIN_PASSWORD='12345' PORT=8080
-
The
HOST
andPORT
variables define the address and port the server will listen to. TheNEW_ADMIN_PASSWORD
is used to create an initial admin user. TheSECRET_KEY
is used for signing JWTs.
To run the project locally, follow these steps:
-
Install project dependencies using Cargo:
cargo build
-
Run the server:
cargo run
The server will start on the address defined by the
HOST
andPORT
environment variables.
This project provides the following API endpoints:
Endpoint | Description | HTTP Method |
---|---|---|
/# |
User login endpoint, requires email and password | POST |
/status |
Check server status | GET |
/users/create_user |
Create a new user | POST |
/users/delete_user_by_id/{id} |
Delete a user by id | DELETE |
/users/get_users |
Retrieve a list of all users (admin only) | GET |
/users/update_user_by_id/{id} |
Update a user by id | PUT |
-
/#
: Provides a JWT token upon successful authentication. The token must be included in theAuthorization
header for requests to protected endpoints. -
Protected Endpoints: The
/users/get_users
endpoint requires the user to be an admin (is_admin
field set totrue
). The JWT token is validated, and only users with the admin role can access this endpoint.