/python-api-based-on-flask-restful

The current python API is based on flask-restful.

Primary LanguageSQLPLMIT LicenseMIT

Python API - Based on Flask Restful

Table of content

Installation

Initial requirements

You will need the following services on your machine before you continue:

  • MySQL
  • memcached
  • git
  • python
  • pip

Set up a Virtual Environment

Ubuntu 16.08

To install virtual environment on Ubuntu 16.06 use the commands below:

apt-get update
apt-get install virtualenv
virtualenv -p python my-api
ls my-api/lib

Access the environment

source my-api/bin/activate

Exit the environment

deactivate

Windows

To install virtual environment on Windows use the commands below:

pip install virtualenv
mkvirtualenv my-api
setprojectdir .

Access the environment

workon my-api

Exit the environment

deactivate

Install the required packages

Run the following lines of code:

pip install Flask-RESTful
pip install Flask-JWT-Extended
pip install PyMySQL
pip install datetime
pip install python-dateutil
pip install pymemcache
pip install python-dateutil
pip install uwsgi

pip freeze > requirements.txt

Database

Open folder "database" and use the SQL file to build the database which is required by the API.

File structure

database/ - here we store the database structure
files/ - in this folder we will store images uploaded by users who use the API
helpers/ - here are stored classes which execute specific tasks (db connection, email sending, etc.)
resources/ - here are stored all controllers to which we have access via a web browser
log/ - this folder does not exist yet but once you run uWSGI service it will be created. Here we store the log files logged by uWSGI.
app.py - Initialise the app
config.py - configuration
email_template_html_en_UK.html - Email template (HTML)
email_template_plain_en_UK.txt - Email template (Plain text)
run.py - Used by uWSGI to start the API
uwsgi.ini - uWSGI config file

Run with NGINX and uWSGI on Ubuntu 16.04

In order to run the API with NGINX and uWSGI be sure that you have NGINX and uWSGI installed and running.

NGINX config

Here is an example NGINX configuration:

server {
    listen 80;
    listen [::]:80;

    server_name PUBLIC_URL;
    return 302 https://$server_name$request_uri;
}

server {
    listen 443;
    listen [::]:443;

    ssl        on;
    ssl_certificate         /etc/ssl/certs/cert.pem;
    ssl_certificate_key     /etc/ssl/private/key.pem;
    ssl_client_certificate /etc/ssl/certs/cloudflare.crt;
    server_name PUBLIC_URL;

    real_ip_header X-Forwarded-For;
    set_real_ip_from 127.0.0.1;


    location / {
        include uwsgi_params;
        uwsgi_pass unix:///PATH_TO_YOUR_API_FOLDER/socket.sock;
        uwsgi_modifier1 30;
    }
}

Take into account that the configuration takes into account the work of the API with SSL certificate generated by CloudFlare.

uWSGI setup

Create the following file:

nano /etc/systemd/system/uwsgi_api.service

Inside the file add the following configuration:

[Unit]
Description=uWSGI dev api

[Service]
ExecStart=/PATH_TO_YOUR_API_FOLDER/my-api/bin/uwsgi --master --emperor /PATH_TO_YOUR_API_FOLDER/uwsgi.ini --die-on-term --uid www-data --gid www-data --logto /PATH_TO_YOUR_API_FOLDER/log/emperor.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Save and close.

Go to the directory where the API is located and open uwsgi.ini file. Set up correct values for the variables inside.

Run

Run the service you created above.

systemctl restart uwsgi_api
systemctl start uwsgi_api
systemctl enable uwsgi_api

After the execution of the commands above you can access the API via your web browser. Take into account that if you make changes of the python code you will have to reset uwsgi_api service in order to take the new changes into account.

Useful links

Contributing

How can you contribute:

  1. Fork it.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Added some feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Create a new Pull Request.

License

The current code is licensed under the MIT license.