A Rust backend API for managing schools and students, built with Axum and PostgreSQL.
- RESTful API with Axum web framework
- PostgreSQL database with SQLx for type-safe queries
- JWT-based authentication with role-based access control
- Interactive Swagger/OpenAPI 3.0 documentation
- CORS enabled for cross-origin requests
- Structured logging with tracing
- Connection pooling for optimal performance
- Standardized error responses
- Rust (latest stable version)
- PostgreSQL 17 or higher
- Ubuntu/Debian Linux (or similar)
sudo apt update
sudo apt install -y postgresql postgresql-contribsudo systemctl start postgresql
sudo systemctl enable postgresql# Set password for postgres user
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres';"
# Create the database
sudo -u postgres createdb school_db
# Run migrations
sudo -u postgres psql -d school_db -f migrations/001_init.sqlThe .env file is already configured with:
DATABASE_URL=postgres://postgres:postgres@localhost:5432/school_db
You can modify this if you use different credentials.
cargo runThe server will start on http://localhost:3000
You should see output like:
INFO school_backend: Database migrations completed successfully
INFO school_backend: Server running on http://127.0.0.1:3000
cargo build --release
./target/release/school-backendOnce the server is running, access the interactive API documentation at:
The Swagger UI provides:
- Complete API endpoint documentation
- Interactive "Try it out" functionality
- Request/response schemas with examples
- Authentication support (JWT Bearer tokens)
- Organized by tags (Health, Authentication, Admin, Student, Mentor, School)
The raw OpenAPI 3.0 specification is available at:
http://localhost:3000/api-docs/openapi.json
GET /health- Check API health status
Public Endpoints:
POST /auth/admin/register- Register new admin userPOST /auth/admin/login- Admin loginPOST /auth/student/register- Register new student userPOST /auth/student/login- Student loginPOST /auth/mentor/register- Register new mentor userPOST /auth/mentor/login- Mentor loginPOST /auth/refresh- Refresh access token
Protected Endpoints:
POST /auth/logout- Logout (requires authentication)GET /auth/me- Get current user profilePOST /auth/verify- Verify token validity
GET /admin/dashboard- Get admin dashboardGET /admin/users- List all usersGET /admin/statistics- Get user statisticsPOST /admin/users/{user_id}/activate- Activate user accountPOST /admin/users/{user_id}/deactivate- Deactivate user account
GET /admin/schools- List all schoolsPOST /admin/schools/create- Create new schoolGET /admin/schools/{school_id}- Get school detailsPUT /admin/schools/{school_id}- Update schoolDELETE /admin/schools/{school_id}- Delete schoolGET /admin/schools/{school_id}/statistics- Get school statistics
GET /student/dashboard- Get student dashboardGET /student/profile- Get student profileGET /student/courses- Get enrolled coursesPOST /student/assignments/{assignment_id}/submit- Submit assignmentGET /student/grades- Get gradesPOST /student/messages/mentor- Send message to mentor
GET /mentor/dashboard- Get mentor dashboardGET /mentor/profile- Get mentor profileGET /mentor/students- Get assigned studentsGET /mentor/students/{student_id}/progress- Get student progressPOST /mentor/assignments/{assignment_id}/grade- Grade assignmentPOST /mentor/assignments/create- Create new assignmentPOST /mentor/messages/student/{student_id}- Send message to studentGET /mentor/courses/{course_id}/assignments- Get course assignments
The API uses JWT (JSON Web Tokens) for authentication.
- Register or login using the appropriate endpoint:
curl -X POST http://localhost:3000/auth/admin/login \
-H "Content-Type: application/json" \
-d '{"email":"admin@school.com","password":"your_password"}'- The response will include an
access_token:
{
"user": {...},
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "...",
"token_type": "Bearer",
"expires_in": 3600
}Include the token in the Authorization header for protected endpoints:
curl -X GET http://localhost:3000/admin/dashboard \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"- Click the "Authorize" button in Swagger UI
- Enter:
Bearer YOUR_ACCESS_TOKEN - Click "Authorize"
- Now you can test protected endpoints directly in the browser
id- UUID primary keyemail- VARCHAR, required, uniquepassword_hash- VARCHAR, requiredfirst_name- VARCHAR, requiredlast_name- VARCHAR, requiredrole- VARCHAR (admin, student, mentor), requiredis_active- BOOLEAN, default truecreated_at- TIMESTAMPupdated_at- TIMESTAMP
id- UUID primary keyname- VARCHAR, requiredlocation- VARCHARprincipal- VARCHAR
id- UUID primary keyuser_id- Foreign key to users tableschool_id- Foreign key to schools tableenrollment_date- VARCHAR
id- UUID primary keyuser_id- Foreign key to users tableschool_id- Foreign key to schools tablespecialization- VARCHAR
All error responses follow a standardized format:
{
"error": "ErrorType",
"message": "Human-readable error message",
"timestamp": "2024-01-15T10:30:00Z"
}Common HTTP status codes:
200 OK- Successful GET, PUT, DELETE201 Created- Successful POST400 Bad Request- Invalid request data401 Unauthorized- Missing or invalid token403 Forbidden- Valid token but insufficient permissions404 Not Found- Resource not found409 Conflict- Resource conflict (e.g., email already exists)500 Internal Server Error- Server error
If you see "password authentication failed", ensure:
- PostgreSQL is running:
sudo systemctl status postgresql - Password is set correctly:
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres';" .envfile has the correct DATABASE_URL
If port 3000 is already in use, you can change it in src/main.rs:
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?; // Change 3000 to your preferred port- Ensure the server is running on port 3000
- Navigate to
http://localhost:3000/docs(not/swagger) - Check server logs for any errors
- Verify the OpenAPI spec is accessible at
http://localhost:3000/api-docs/openapi.json
You may see false errors from rust-analyzer about "struct is not supported in traits or impls" related to utoipa macros. These are parsing issues and can be ignored - the code will compile correctly.
- API Documentation: See SWAGGER_INTEGRATION.md for detailed information about the Swagger/OpenAPI integration
- Authentication: See AUTH_DOCUMENTATION.md for authentication details
- Interactive API Docs: http://localhost:3000/docs (when server is running)
- Framework: Axum 0.7
- Database: PostgreSQL with SQLx
- Authentication: JWT (jsonwebtoken)
- Password Hashing: bcrypt
- API Documentation: utoipa + utoipa-swagger-ui
- Logging: tracing + tracing-subscriber
- Async Runtime: Tokio