Automated Action 493289730d Add secure SECRET_KEY generation and improve key documentation
- Update config.py to auto-generate a secure SECRET_KEY if not provided
- Add warning log when using an auto-generated key
- Update README with clearer information about the two different keys
- Add detailed security notes explaining each key's purpose
2025-05-27 18:57:18 +00:00

99 lines
2.8 KiB
Markdown

# News Aggregation Service
A FastAPI application that aggregates news from various sources using the Mediastack API.
## Features
- User authentication and authorization
- News article fetching and storage from Mediastack API
- Filtering news by keywords, sources, categories, countries, and languages
- User preferences for personalized news
- Save articles for later reading
- Background tasks for periodic news updates
## Tech Stack
- **FastAPI**: High-performance web framework
- **SQLite**: Database for storing news articles and user data
- **SQLAlchemy**: ORM for database interactions
- **Alembic**: Database migration tool
- **Pydantic**: Data validation and settings management
- **JWT**: Token-based authentication
## Requirements
- Python 3.8+
- Mediastack API key (required for fetching news data)
## Setup
1. Clone the repository
2. Install dependencies:
```bash
pip install -r requirements.txt
```
3. Set up environment variables:
```
# Required for fetching news from Mediastack API
MEDIASTACK_API_KEY=your_api_key_here
# Optional - For JWT authentication security
# If not set, a secure random key will be generated on startup
# but JWT tokens will be invalidated when the server restarts
SECRET_KEY=your_secret_key_here
```
4. Run database migrations:
```bash
alembic upgrade head
```
5. Start the server:
```bash
uvicorn main:app --reload
```
## API Endpoints
The API is documented with Swagger UI, available at `/docs` when the server is running.
### Main Endpoints
- `/api/v1/users/register`: Register a new user
- `/api/v1/users/token`: Login and get JWT token
- `/api/v1/news`: Get news articles with optional filtering
- `/api/v1/news/personalized`: Get personalized news based on user preferences
- `/api/v1/news/saved`: Save articles for later reading
- `/health`: Health check endpoint
## Project Structure
- `app/api`: API routes and endpoints
- `app/core`: Core functionality (config, security, utils)
- `app/db`: Database connection and session
- `app/models`: SQLAlchemy models
- `app/schemas`: Pydantic schemas for validation
- `app/services`: Business logic services
- `migrations`: Database migrations
## Security Notes
The application uses two distinct keys for different purposes:
1. **Mediastack API Key**: Used to authenticate with the external Mediastack news API service. This key is required to fetch news data.
2. **Secret Key**: Used internally for JWT token generation and verification in the authentication system. If not provided via the SECRET_KEY environment variable, a secure random key will be generated automatically on each startup. Note that this means all active JWT tokens will be invalidated when the server restarts.
## Development
To run linting checks:
```bash
ruff check .
```
To fix linting issues automatically:
```bash
ruff check --fix .
```