|
|
преди 1 месец | |
|---|---|---|
| .. | ||
| .hypothesis | преди 1 месец | |
| middleware | преди 1 месец | |
| routers | преди 1 месец | |
| schemas | преди 1 месец | |
| script | преди 1 месец | |
| services | преди 1 месец | |
| test | преди 1 месец | |
| .env.example | преди 1 месец | |
| JWT_AUTHENTICATION_GUIDE.md | преди 1 месец | |
| README.md | преди 1 месец | |
| SAMPLE_DATA_README.md | преди 1 месец | |
| config.py | преди 1 месец | |
| config.yaml | преди 1 месец | |
| create_test_user.py | преди 1 месец | |
| database.py | преди 1 месец | |
| main.py | преди 1 месец | |
| models.py | преди 1 месец | |
| pytest.ini | преди 1 месец | |
| requirements.txt | преди 1 месец | |
| test_oauth_config.py | преди 1 месец | |
| test_oauth_flow.py | преди 1 месец | |
FastAPI-based backend for the annotation platform with JWT authentication.
Install dependencies:
pip install -r requirements.txt
Configure environment variables:
cp .env.example .env
# Edit .env with your configuration
Important: Set a secure JWT_SECRET_KEY in production:
# Generate a secure random key
python -c "import secrets; print(secrets.token_urlsafe(32))"
| Variable | Description | Default |
|---|---|---|
JWT_SECRET_KEY |
Secret key for JWT token signing | Auto-generated (dev only) |
JWT_ALGORITHM |
JWT signing algorithm | HS256 |
ACCESS_TOKEN_EXPIRE_MINUTES |
Access token expiration time | 15 minutes |
REFRESH_TOKEN_EXPIRE_DAYS |
Refresh token expiration time | 7 days |
DATABASE_PATH |
SQLite database file path | annotation_platform.db |
Development mode:
python main.py
Or with uvicorn directly:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
Once the server is running, visit:
Register a new user:
curl -X POST "http://localhost:8000/api/auth/register" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "securepassword123"
}'
Response:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"token_type": "bearer",
"user": {
"id": "user_abc123",
"username": "testuser",
"email": "test@example.com",
"role": "annotator",
"created_at": "2024-01-01T00:00:00"
}
}
Login with existing credentials:
curl -X POST "http://localhost:8000/api/auth/login" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "securepassword123"
}'
Include the access token in the Authorization header for protected endpoints:
curl -X GET "http://localhost:8000/api/projects" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Refresh your access token using the refresh token:
curl -X POST "http://localhost:8000/api/auth/refresh" \
-H "Content-Type: application/json" \
-d '{
"refresh_token": "YOUR_REFRESH_TOKEN"
}'
Get information about the currently authenticated user:
curl -X GET "http://localhost:8000/api/auth/me" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
All API endpoints except authentication endpoints require a valid JWT token.
POST /api/auth/register - User registrationPOST /api/auth/login - User loginPOST /api/auth/refresh - Token refreshGET / - Health checkGET /health - Health checkGET /docs - API documentationGET /redoc - API documentationGET /api/auth/me - Get current user info/api/projects/* endpoints/api/tasks/* endpoints/api/annotations/* endpointsDELETE /api/projects/{project_id} - Delete projectDELETE /api/tasks/{task_id} - Delete taskAnnotations:
Tasks:
assigned_to is not provided when creating a task, it defaults to the current userProjects:
backend/
├── main.py # FastAPI application entry point
├── config.py # Configuration management
├── database.py # Database connection and initialization
├── models.py # Database models (User, Project, Task, Annotation)
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── middleware/
│ ├── __init__.py
│ └── auth_middleware.py # JWT authentication middleware
├── routers/ # API route handlers
│ ├── __init__.py
│ ├── auth.py # Authentication endpoints
│ ├── project.py # Project management
│ ├── task.py # Task management
│ └── annotation.py # Annotation management
├── services/ # Business logic
│ ├── jwt_service.py # JWT token operations
│ └── auth_service.py # Authentication logic
├── schemas/ # Pydantic schemas for validation
│ ├── auth.py # Authentication schemas
│ ├── project.py # Project schemas
│ ├── task.py # Task schemas
│ └── annotation.py # Annotation schemas
└── test/ # Test files
└── ...
The application uses SQLite for data storage. The database file is created automatically on first run.
Default location: annotation_platform.db
To use a custom location, set the DATABASE_PATH environment variable.
users: User accounts with authentication info
id, username, email, password_hash, role, oauth_provider, oauth_id, created_at
projects: Annotation projects
id, name, description, config, created_at
tasks: Annotation tasks within projects
id, project_id, name, data, status, assigned_to, created_at
annotations: User annotations for tasks
id, task_id, user_id, result, created_at, updated_atRun tests with pytest:
pytest
Run tests with coverage:
pytest --cov=. --cov-report=html
main.py to match your frontend domainMake sure you include the Authorization header with a valid Bearer token:
Authorization: Bearer YOUR_ACCESS_TOKEN
Your access token has expired. Use the refresh token to get a new access token:
POST /api/auth/refresh
Your user role doesn't have permission for this operation. Contact an admin if you need elevated permissions.
[Your License Here]