A RESTful API for managing users, built with Rust, Axum, and SQLx.
- Language: Rust (2024 edition)
- Web Framework: Axum
- Database: PostgreSQL
- ORM/Query Builder: SQLx
- Runtime: Tokio
- Serialization: Serde
- Documentation: Utoipa (Swagger UI)
- Security: Bcrypt (Password Hashing)
- Rust (latest stable)
- PostgreSQL running locally or accessible via URL.
- SQLx CLI:
cargo install sqlx-cli(Recommended for migrations)
-
Clone the repository:
git clone <repository_url> cd user_service
-
Environment Configuration:
Create a
.envfile in the root directory:DATABASE_URL=postgres://postgres:postgres@localhost/mydb RUST_LOG=debug PORT=3001 HOST=0.0.0.0
-
Database Setup:
Run database migrations to set up the schema (including
usersandusers_authtables):sqlx database create sqlx migrate run
Run the server with cargo:
cargo runThe server will start at http://0.0.0.0:3001.
Interactive API documentation is available via Swagger UI:
- Swagger UI: http://localhost:3001/swagger-ui/
- OpenAPI Spec: http://localhost:3001/api-docs/openapi.json
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
POST |
/users/register |
Register a new user | { "username": "...", "email": "...", "password": "...", "created_by": "..." } |
POST |
/users/login |
Login user | { "email": "...", "password": "..." } |
GET |
/users |
Get all users | - |
POST |
/users |
Create a simple user (Legacy) | { "name": "...", "email": "..." } |
GET |
/users/:id |
Get a user by ID | - |
PUT |
/users/:id |
Update a user by ID | { "name": "...", "email": "..." } |
DELETE |
/users/:id |
Delete a user by ID | - |
Register User:
curl -X POST http://localhost:3001/users/register \
-H "Content-Type: application/json" \
-d '{"username": "johndoe", "email": "john@example.com", "password": "securepassword", "created_by": "system"}'Login User:
curl -X POST http://localhost:3001/users/login \
-H "Content-Type: application/json" \
-d '{"email": "john@example.com", "password": "securepassword"}'src/
├── docs.rs # Swagger/OpenAPI configuration
├── handlers/ # HTTP request handlers
├── models/ # Data structures and DTOs
├── repository/ # Database access layer
├── services/ # Business logic
├── routes.rs # Route definitions
├── state.rs # Application state (DB pool)
└── main.rs # Entry point