
- 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
130 lines
2.8 KiB
Markdown
130 lines
2.8 KiB
Markdown
# 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:
|
|
|
|
```bash
|
|
git clone https://github.com/yourusername/restapiservice.git
|
|
cd restapiservice
|
|
```
|
|
|
|
2. Create and activate a virtual environment:
|
|
|
|
```bash
|
|
python -m venv venv
|
|
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
```
|
|
|
|
3. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
4. Run database migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
5. Initialize the database with initial data:
|
|
|
|
```bash
|
|
python -m app.initial_data
|
|
```
|
|
|
|
## Running the Application
|
|
|
|
Start the application with uvicorn:
|
|
|
|
```bash
|
|
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 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:
|
|
|
|
```bash
|
|
alembic revision --autogenerate -m "description"
|
|
```
|
|
|
|
To apply migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
### Code Style
|
|
|
|
This project uses Ruff for linting and formatting:
|
|
|
|
```bash
|
|
ruff check .
|
|
ruff format .
|
|
```
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the LICENSE file for details. |