A FastAPI-based REST API for managing employees and departments with authentication and logging capabilities.
- User authentication with JWT tokens
- Employee management (CRUD operations)
- Department management (CRUD operations)
- Health check endpoint
- Comprehensive logging system
- File-based JSON storage
- Password encryption with salt
.
├── app
│ ├── config
│ │ ├── config.json
│ │ ├── database.py
│ │ ├── __init__.py
│ │ └── settings.py
│ ├── data
│ │ ├── departments.json
│ │ ├── employees.json
│ │ └── users.json
│ ├── __init__.py
│ ├── main.py
│ ├── models
│ │ ├── department.py
│ │ ├── employee.py
│ │ ├── __init__.py
│ │ └── user.py
│ ├── routers
│ │ ├── auth.py
│ │ ├── departments.py
│ │ ├── employees.py
│ │ ├── health.py
│ │ └── __init__.py
│ └── utils
│ ├── auth.py
│ ├── __init__.py
│ └── logger.py
├── docker-compose.yml
├── Dockerfile
├── logs/
├── README.md
└── requirements.txt
- Python 3.8+
- pip (Python package installer)
- Clone the repository:
git clone <repository-url>
cd employee-management-api
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
Start the server with:
uvicorn app.main:app --reload
The API will be available at http://localhost:8000
After starting the server, you can access:
- Interactive API documentation (Swagger UI):
http://localhost:8000/docs
- Alternative API documentation (ReDoc):
http://localhost:8000/redoc
POST /auth/register
- Register a new userPOST /auth/login
- Login and get access token
GET /health
- Check API health status (no authentication required)
GET /employees
- List all employeesPOST /employees
- Create a new employeePUT /employees/{employee_id}
- Update an employee (full update)PATCH /employees/{employee_id}
- Update an employee (partial update)DELETE /employees/{employee_id}
- Delete an employeeGET /employees/current-user
- Get current user information
GET /departments
- List all departmentsPOST /departments
- Create a new departmentPUT /departments/{department_id}
- Update a department (full update)PATCH /departments/{department_id}
- Update a department (partial update)DELETE /departments/{department_id}
- Delete a department
The API uses JWT tokens for authentication. To access protected endpoints:
- Register a user using
/auth/register
- Login using
/auth/login
to get an access token - Include the token in the Authorization header:
Bearer <token>
Logs are stored in the logs
directory with the following format:
- File naming:
log-{HOUR}-{DAY}-{MONTH}-{YEAR}.log
- Log format:
TIMESTAMP - Event or operation Name
LOG
-------------------------------------
The application uses JSON files for data storage:
data/users.json
- User informationdata/employees.json
- Employee recordsdata/departments.json
- Department information
- Password hashing with bcrypt
- JWT token-based authentication
- Salted password storage
- Protected endpoints requiring authentication
The API implements proper error handling with appropriate HTTP status codes and error messages for:
- Authentication failures
- Resource not found
- Invalid input data
- Duplicate entries
- Server errors
To contribute to the project:
- Create a new branch for your feature
- Implement your changes
- Write or update tests if necessary
- Submit a pull request
As the application is running in a dev container that starts the server automatically, the logs can be watched from another terminal using docker:
docker logs -f <CONTAINER_ID> | <CONTAINER_NAME>
Python base image is available here