Table of content
- Installation
- File structure
- Run with NGINX and uWSGI on Ubuntu 16.04
- Useful links
- Contributing
- License
You will need the following services on your machine before you continue:
- MySQL
- memcached
- git
- python
- pip
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
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
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
Open folder "database" and use the SQL file to build the database which is required by the API.
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
In order to run the API with NGINX and uWSGI be sure that you have NGINX and uWSGI installed and running.
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.
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 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.
- How to set up a virtual environment on Windows - http://timmyreilly.azurewebsites.net/python-pip-virtualenv-installation-on-windows/
- How to set up a virtual environment on Linux - https://www.linode.com/docs/development/python/create-a-python-virtualenv-on-ubuntu-1610/
- Blacklisting - https://github.com/vimalloc/flask-jwt-extended/tree/master/examples/database_blacklist
How can you contribute:
- Fork it.
- Create your feature branch (git checkout -b my-new-feature).
- Commit your changes (git commit -am 'Added some feature').
- Push to the branch (git push origin my-new-feature).
- Create a new Pull Request.
The current code is licensed under the MIT license.