Skip to content

kkayacan/filterable-news-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

filterable-news-api

mysql/php/codeigniter powered news collector. Feel free to report bugs, suggest features and contribute.

Check filterable-news-ui5 for frontend.

Features

Requirements

  • PHP 7.2+
  • MySQL 5.1+

Setup

  1. Enable Apache mod_rewrite
sudo a2enmod rewrite
  1. Set AllowOverride and [for production system] set environment variable
sudo nano /etc/apache2/apache2.conf

Paste this:

<Directory /var/www/api.example.com/public_html/>
    AllowOverride All
    SetEnv CI_ENV production
</Directory>
  1. Restart Apache
sudo systemctl restart apache2
  1. Create database and grant authorization to db user
mysql -u root -p
CREATE DATABASE dbname DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL ON dbname.* TO 'dbuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;
  1. Clone this repo as public_html will be document root
cd /var/www
sudo git clone https://github.com/kkayacan/filterable-news-api.git
sudo mv filterable-news-api api.example.com
  1. Install dependencies
cd /var/www/api.example.com/application
sudo su
sudo curl -s https://getcomposer.org/installer | php
exit
sudo php composer.phar install
  1. Copy configuration files to environment directory
cd /var/www/api.example.com/application/config
sudo mkdir production
sudo cp config.php production/config.php
sudo cp database.php production/database.php
sudo cp rest.php production/rest.php
  1. Set base url and newsapi.org api key in config.php
$config['base_url'] = 'https://api.example.com/';
$config['newsapikey'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  1. Set database info in database.php
$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'dbuser',
        'password' => 'password',
        'database' => 'dbname',
  1. [If frontend is on different origin] Enable CORS in rest.php
$config['check_cors'] = TRUE;

If frontend domain unknown

$config['allow_any_cors_domain'] = TRUE;

If frontend domain known

$config['allowed_cors_origins'] = ['https://example.com', 'https://www.example.com'];
  1. Change ownership and permissions
cd /var/www
sudo chown -R ownername:www-data api.example.com  
cd api.example.com  
sudo find . -type f -exec chmod 664 {} +  
sudo find . -type d -exec chmod 775 {} +
  1. Launch your browser and execute database migration script
api.example.com/migrate
  1. Schedule a cron job to start collecting news, ie every 3 minutes
crontab -e

Paste this

*/3 * * * * /usr/bin/curl --silent https://api.example.com/collect/news >/dev/null 2>&1