A complete Rust API with authentication and post management using Axum and SQLite.
- User authentication (register, login, profile)
- JWT token authentication
- Post CRUD operations
- SQLite database
- Input validation
- CORS support
-
Setup
git clone <repository-url> cd api-rustone
-
Environment
# Create .env file DATABASE_URL=sqlite:./api_rust_one.db JWT_SECRET=your-super-secret-jwt-key RUST_LOG=info
-
Run
cargo run
Server starts at http://127.0.0.1:8081
POST /auth/register
- Register userPOST /auth/login
- Login userGET /auth/profile
- Get profile (auth required)PUT /auth/profile
- Update profile (auth required)
GET /posts
- Get all postsGET /posts/{id}
- Get specific postPOST /posts
- Create post (auth required)GET /posts/my
- Get user's posts (auth required)PUT /posts/{id}
- Update post (auth required)DELETE /posts/{id}
- Delete post (auth required)
# All tests
cargo test
# Integration tests only
cargo test --test integration_test
# Unit tests only
cargo test --lib
# Start server
cargo run
# Test endpoints
curl -X POST http://localhost:8081/auth/register \
-H "Content-Type: application/json" \
-d '{"name": "Test User", "email": "[email protected]", "password": "TestPass123"}'
curl -X POST http://localhost:8081/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "TestPass123"}'
# Use returned token for protected endpoints
curl -X GET http://localhost:8081/auth/profile \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
Success:
{
"message": "Success message",
"data": { ... }
}
Error:
{
"error": "Error type",
"message": "Error description"
}
src/
├── main.rs # App entry point
├── lib.rs # Library exports
├── model/ # Data models
├── handlers/ # Route handlers
├── db/ # Database setup
└── helpers/ # Utilities
- Framework: Axum
- Database: SQLite
- Auth: JWT + bcrypt
- Validation: Custom regex
- Logging: Tracing # axum-rest-auth