Automated Action f207cc3c64 Create RESTful API service with FastAPI and SQLite
- 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
2025-06-16 14:56:03 +00:00

2.8 KiB

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

  1. Clone the repository:
git clone https://github.com/yourusername/restapiservice.git
cd restapiservice
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Run database migrations:
alembic upgrade head
  1. 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

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 token
  • POST /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 information
  • PUT /api/v1/users/me: Update current user information
  • GET /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.