The News Hub API is a robust, feature-rich platform designed and developed by Eng. Hassan Gomaa to aggregate and process news articles from multiple sources, including:
- The Guardian API
- New York Times API
- NewsAPI
The system provides categorized, filtered, and sanitized news content while implementing industry-standard security and optimization practices.
Easily fetch and seed articles into the database using the following commands:
php artisan guardianapi:fetch --section=technology --from-date=2024-01-01
php artisan nytimes:fetch --query=technology --begin-date=20240101 --end-date=20241231 --page=0
php artisan newsapi:test --country=us --category=business --source=newsapi
The API requires the following keys, which must be defined in the .env
file:
NEWSAPI_API_KEY=<YOUR_NEWSAPI_KEY>
GUARDIAN_API_KEY=<YOUR_GUARDIAN_KEY>
NYT_API_KEY=<YOUR_NYT_KEY>
This system provides a set of endpoints to interact with articles, authors, sources, and categories:
- GET /api/articles: List all articles with filters and pagination.
- GET /api/authors: List all authors with optional filters and pagination.
- GET /api/sources: List all sources with optional filters and pagination.
- GET /api/categories: List all categories with optional filters and pagination.
The platform incorporates advanced middleware to:
- Prevent XSS and injection attacks: Using a custom XSS sanitizer.
- Rate Limiting: Throttle API requests to 30 requests per minute to ensure stability.
The project utilizes a relational database with the following key tables:
articles
: Stores article data, including title, URL, publication date, etc.authors
: Stores author information.sources
: Stores information about news sources.categories
: Stores categories to which articles belong.
Refer to the attached SQL schema for details.
The project is organized into a clean and modular structure for maintainability:
app/
├── Console/Commands # Artisan commands for APIs
├── Exceptions # Custom exception handling
├── Helpers # Helper classes for seeding and utilities
├── Http # Controllers, middleware, requests, resources
├── Models # Eloquent models
├── Providers # Service providers
├── Repositories # Data repositories
├── Services # Service classes for API integrations
├── Traits # Shared traits
git clone https://github.com/hassangomaa/news-hub-api.git
composer install
Create a .env
file and add the required environment variables (API keys and database configuration).
php artisan migrate
You can seed articles by running the respective API commands.
php artisan serve
Detailed API documentation is available and should be updated regularly using Swagger.
This system follows best practices:
-
SOLID
- Single Responsibility: Each service handles a specific API; each command manages fetching and seeding.
- Open/Closed: Add new APIs by extending
AbstractAPIService
andBaseCommand
. - Liskov Substitution: All services work interchangeably under common contracts.
-
DRY
- Common API logic centralized in
AbstractAPIService
. - Command logic streamlined with
BaseCommand
.
- Common API logic centralized in
-
KISS
- API handling abstracted for simplicity.
- Minimal logic in commands, standardized configurations.
- AbstractAPIService: Centralized API handling (e.g., headers, responses).
- BaseCommand: Shared command logic for fetch-and-seed flow.
- Extending Example:
- Services like
NYTimesAPIService
map and fetch API data. - Commands like
NYTimesAPI
handle parameters and seed data.
- Services like
This architecture ensures simplicity, modularity, and scalability.
The full Entity-Relationship Diagram (ERD) for the NewsHubAPI database is available at the link below. It provides a detailed visual representation of all database tables, their fields, and relationships.
Diagram Name: DiagramNewsHubAPI
Link: DiagramNewsHubAPI ERD
This section provides step-by-step instructions to set up and run the News Hub API using Docker and Docker Compose.
Before you start, ensure you have the following installed on your system:
- Docker: Download Docker
- Docker Compose (included with Docker Desktop)
Run the following command to clone the repository:
git clone https://github.com/hassangomaa/news-hub-api.git
cd news-hub-api
Create a .env
file (if not already present) by copying the example configuration:
cp .env.example .env
Then, update the .env
file with your database credentials and API keys.
Run the following command to build and start the containers:
docker-compose up --build -d
This will:
✅ Build the PHP Laravel container
✅ Start the MySQL database container
✅ Start the Nginx web server
✅ Start PhpMyAdmin for database management
To check if all containers are running properly, use:
docker ps
You should see containers running for:
✅ laravel_app
(Laravel App)
✅ laravel_mysql
(MySQL Database)
✅ laravel_nginx
(Nginx Server)
✅ laravel_phpmyadmin
(PhpMyAdmin)
After the containers are up, run database migrations inside the Laravel container:
docker exec -it laravel_app php artisan migrate --seed
This will set up the database schema and seed initial data.
- API Endpoints:
http://localhost:8000/api
- PhpMyAdmin (Database UI):
http://localhost:8080
- Username:
laravel_user
- Password:
laravel_password
- Username:
To stop the containers:
docker-compose down
To restart the containers:
docker-compose up -d
To run any Laravel Artisan command, use:
docker exec -it laravel_app php artisan <command>
For example, to clear the cache:
docker exec -it laravel_app php artisan cache:clear
- If you get permission errors, run:
sudo chmod -R 777 storage bootstrap/cache
- To check logs inside the app container:
docker logs laravel_app
- Rate Limiting: Configured at
throttle:30,1
. - Security: Includes middleware for XSS sanitization and request validation.
- Logging: All API and command errors are logged for debugging.
This project is fully developed and maintained by Eng. Hassan Gomaa. It integrates cutting-edge technologies and practices to deliver a seamless news aggregation experience.