
- Updated migrations/env.py to add project root to Python path - Created run_migrations.py helper script for running migrations - Updated README with instructions for using the helper script - Fixed the 'No module named app' error when running migrations
HR Platform Backend
A comprehensive HR platform backend built with FastAPI and SQLite, providing employee management, authentication, and category-based search functionality.
Features
- Authentication & Authorization: JWT-based authentication system with user roles
- Employee Management: Complete CRUD operations for employees
- Department Management: Organize employees by departments
- Job Title Management: Manage job positions and titles
- Category-Based Search: Advanced search functionality by skills, categories, departments, and more
- OpenAPI Documentation: Interactive API documentation with Swagger UI and ReDoc
- Health Check Endpoint: Monitor the health of the API and database
Tech Stack
- Framework: FastAPI
- Database: SQLite
- ORM: SQLAlchemy
- Migrations: Alembic
- Authentication: JWT (JSON Web Tokens)
- API Documentation: OpenAPI (Swagger and ReDoc)
Project Structure
.
├── alembic.ini # Alembic configuration
├── app # Main application package
│ ├── api # API endpoints
│ │ ├── deps.py # API dependencies
│ │ ├── routes.py # API router
│ │ └── v1 # API version 1 endpoints
│ ├── core # Core application modules
│ │ ├── config.py # Application configuration
│ │ └── security.py # Security utilities
│ ├── crud # CRUD operations
│ ├── db # Database setup
│ │ ├── base.py # Base models import
│ │ ├── base_class.py # Base model class
│ │ ├── init_db.py # Database initialization
│ │ └── session.py # Database session
│ ├── models # SQLAlchemy models
│ └── schemas # Pydantic schemas
├── main.py # Application entry point
├── migrations # Alembic migrations
│ ├── env.py # Migration environment
│ ├── script.py.mako # Migration script template
│ └── versions # Migration versions
├── openapi.json # Generated OpenAPI schema
├── openapi_schema.py # Script to generate OpenAPI schema
├── requirements.txt # Python dependencies
└── run_migrations.py # Helper script for running Alembic migrations
Getting Started
Prerequisites
- Python 3.8 or higher
- pip (Python package installer)
Installation
-
Clone the repository:
git clone <repository-url> cd hrplatformbackend
-
Install the dependencies:
pip install -r requirements.txt
-
Set environment variables (optional, defaults are provided):
export SECRET_KEY="your-secret-key" export FIRST_SUPERUSER="admin@example.com" export FIRST_SUPERUSER_PASSWORD="admin123"
Running Migrations
Run Alembic migrations to set up the database schema using the provided script:
python run_migrations.py upgrade head
Alternatively, if you need to create a new migration:
python run_migrations.py revision --autogenerate -m "description"
Running the Application
Start the FastAPI server:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
The API will be available at http://localhost:8000
API Documentation
Once the server is running, you can access the interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
Usage
Authentication
-
Register a new user:
curl -X POST "http://localhost:8000/api/v1/auth/register" \ -H "Content-Type: application/json" \ -d '{"email": "user@example.com", "password": "password123", "full_name": "Example User"}'
-
Login to get access token:
curl -X POST "http://localhost:8000/api/v1/auth/login" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=user@example.com&password=password123"
-
Use the token for authenticated requests:
curl -X GET "http://localhost:8000/api/v1/users/me" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Employee Management
-
Create a new employee:
curl -X POST "http://localhost:8000/api/v1/employees/" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "first_name": "John", "last_name": "Doe", "email": "john.doe@example.com", "hire_date": "2023-01-15", "department_id": "department_id_here", "job_title_id": "job_title_id_here" }'
-
Search employees by category:
curl -X GET "http://localhost:8000/api/v1/employees/search/?categories=IT&skills=Python&department_id=dept_id" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Environment Variables
Variable | Description | Default Value |
---|---|---|
PROJECT_NAME | Name of the project | HR Platform API |
SECRET_KEY | Secret key for JWT encoding | Auto-generated |
ACCESS_TOKEN_EXPIRE_MINUTES | JWT token expiration time in minutes | 11520 (8 days) |
FIRST_SUPERUSER | Email for the initial admin user | admin@example.com |
FIRST_SUPERUSER_PASSWORD | Password for the initial admin user | admin123 |
SQLALCHEMY_DATABASE_URI | Database connection URI | sqlite:////app/storage/db/db.sqlite |
License
This project is licensed under the MIT License - see the LICENSE file for details.