
- Set up project structure with FastAPI and dependency files - Configure SQLAlchemy with SQLite database - Implement user authentication using JWT tokens - Create comprehensive API routes for user management - Add health check endpoint for application monitoring - Set up Alembic for database migrations - Add detailed documentation in README.md
RESTAPIService
A RESTful API service built with FastAPI and SQLite.
Features
- FastAPI framework for high-performance API development
- SQLite database with SQLAlchemy ORM
- Alembic for database migrations
- JWT authentication with role-based access control
- Comprehensive user management API
- Health check endpoint
- Documentation with OpenAPI (Swagger UI and ReDoc)
Requirements
- Python 3.8+
- Virtual environment (recommended)
Environment Variables
The application uses the following environment variables:
Variable | Description | Default Value |
---|---|---|
SECRET_KEY | Secret key for JWT token generation | supersecretkey |
ACCESS_TOKEN_EXPIRE_MINUTES | Token expiration time in minutes | 30 |
Installation
- Clone the repository:
git clone https://github.com/yourusername/restapiservice.git
cd restapiservice
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Initialize the database with initial data:
python -m app.initial_data
Running the Application
Start the application with uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
The API will be available at http://localhost:8000
API Documentation
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
API Endpoints
Root Endpoint
GET /
: Returns basic information about the API
Health Check
GET /health
: Returns the health status of the application
Authentication
POST /api/v1/auth/login
: Authenticate and get access tokenPOST /api/v1/auth/test-token
: Test if a token is valid
Users
GET /api/v1/users/
: List all users (admin only)POST /api/v1/users/
: Create a new user (admin only)GET /api/v1/users/me
: Get current user informationPUT /api/v1/users/me
: Update current user informationGET /api/v1/users/{user_id}
: Get user by ID (admin only)PUT /api/v1/users/{user_id}
: Update user by ID (admin only)
Development
Database Migrations
To create a new migration after modifying models:
alembic revision --autogenerate -m "description"
To apply migrations:
alembic upgrade head
Code Style
This project uses Ruff for linting and formatting:
ruff check .
ruff format .
License
This project is licensed under the MIT License - see the LICENSE file for details.
Description
Languages
Python
97.7%
Mako
2.3%