Giter Club home page Giter Club logo

tabiri_api's Introduction

Tabiri

Tabiri API

A Django REST API leveraging Geodjango to provide GIS capabilities to tabiri-ui as visible below. The API is integrated with a SARIMA model that predicts the demand for child vaccines.

Sneak Peek


Table of Contents


Installation

Requirements

Requirements include:

  • The Python programming language (Python > 3)
  • The Django web framework
  • The PostgreSQL database for persisting data
  • The PostGIS extension for supporting spatial features in the PostgreSQL database
  • pip for installing dependencies
  • The virtualenvwrapper module for managing a virtual environment

Install Dependencies

Install Python

First check if python is installed $ python3 --version

If its not installed run sudo apt install python3

Install dependencies of GeoDjango

$ sudo apt install gdal-bin libgdal-dev
$ sudo apt install python3-gdal
$ sudo apt install binutils libproj-dev

Install Postgres

$ sudo apt install python3-pip libpq-dev postgresql postgresql-contrib nginx curl

Setting Up Database

Import database

Create the database first

  • Option 1 Change user to postgres

    $ su - postgres

    Create user

    $ createuser --interactive --pwprompt

    Create database

    $ createdb -O user dbname

  • Option 2 Log in to postgres

    $ psql -U username dbname;

    Create database

    $ dbname=# create database dbname;

    Then grant superuser privileges to user

    $ dbname=# GRANT all privileges ON DATABASE dbname TO user;

Import database from sql file

The tabiri_api_db.sql can be found here. Oops! Did I forget to mention its around 150MB, will upload it on to GIT LFS later.

$ psql -h hostname -d databasename -U username -f file.sql

Log in to postgres

$ psql -U username dbname;

Enable the postgis extension. Connect to psql then execute the below script

$ dbname=# CREATE EXTENSION postgis;

Setting up the API

Setting up the virtual environment

Install virtualenvwrapper

$ sudo pip3 install virtualenvwrapper

Add config to .bashrc on the home directory

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

Execute the virtualenvwrapper

$ source .bashrc

Create virtual env

$ mkvirtualenv tabiri_venv

To work on the tabiri_venv

$ workon tabiri_venv

Setting up the project

Clone the api from the repo

$ git clone https://github.com/ochomoswill/tabiri_api.git

The folder structure will be as follows after cloning

tabiri_api/    
    tabiri_api/
        apps/
        __init.py
        settings.py
        urls.py
        wsgi.py            
    .gitignore
    manage.py
    requirements.txt
    setup.py
    README.md

Move into the root directory of the project. Run the below command to install the required packages

$ pip3 install -r requirements.txt

Add the allowed hosts to settings.py

ALLOWED_HOSTS = ['your_server_domain_or_IP', 'second_domain_or_IP', . . .,'127.0.0.1',  'localhost']

Fake migrate

$ python manage.py migrate --fake

To start the application. Run the below the root directory

$ python manage.py runserver

You can test that the api is app by

curl 127.0.0.1:8000/api/gis/countries

Features

Technology Used

  • Django
  • Geodjango
  • Django Rest Framework
  • Pandas
  • Numpy
  • Matplotlib
  • Scikit Learn

Contributing

Step 1

  • Option 1 Fork this repo!

  • Option 2 Clone this repo to your local machine

    $ git clone https://github.com/ochomoswill/tabiri_api.git

Step 2

HACK AWAY! ๐Ÿ”จ๐Ÿ”จ๐Ÿ”จ

Step 3

Create a new pull request using https://github.com/ochomoswill/tabiri_api/compare/.


Deployment

We use Gunicorn for the deployment

Test Gunicorn's Ability to Serve the Project

On the root directory of the project run

$ gunicorn --bind 0.0.0.0:8000 tabiri_api.wsgi

Create systemd Socket and Service Files for Gunicorn

Create and open a systemd socket file for Gunicorn

$ sudo nano /etc/systemd/system/gunicorn.socket

Edit /etc/systemd/system/gunicorn.socket as below:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Create and open a systemd service file for Gunicorn

$ sudo nano /etc/systemd/system/gunicorn.service

Edit /etc/systemd/system/gunicorn.service as below:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=user_account_name
Group=www-data
WorkingDirectory=/home/user_account_name/myprojectdir
ExecStart=/home/user_account_name/.virtualenvs/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          djangoprojectname.wsgi:application

[Install]
WantedBy=multi-user.target

Start and enable the Gunicorn socket

$ sudo systemctl start gunicorn
$ sudo systemctl enable gunicorn

Check the status of the Gunicorn service

$ sudo systemctl status gunicorn

You can test that the api is app by

$ curl --unix-socket /run/gunicorn.sock 127.0.0.1:8000/api/gis/countries

If you make changes to the /etc/systemd/system/gunicorn.service file, reload the daemon to reread the service definition and restart the Gunicorn process by typing:

$ sudo systemctl daemon-reload
$ sudo systemctl restart gunicorn

Configure Nginx to Proxy Pass to Gunicorn

Create and open a new server block in Nginx's sites-available directory:

$ sudo nano /etc/nginx/sites-available/myproject

Edit /etc/nginx/sites-available/myproject as below:

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/user_account_name/myprojectdir;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Enable the file by linking it to the sites-enabled directory

$ sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Test your Nginx configuration for syntax errors by typing:

$ sudo nginx -t

If no errors are reported, go ahead and restart Nginx by typing:

$ sudo systemctl restart nginx

Finally, we need to open up our firewall to normal traffic on port 80. Since we no longer need access to the development server, we can remove the rule to open port 8000 as well:

$ sudo ufw delete allow 8000
$ sudo ufw allow 'Nginx Full'

FYI

Database

After installing PostgreSQL, if you can't seem to log in to the postgres server using psql, follow the below steps

Open the file pg_hba.conf in /etc/postgresql/<postgres_version>/main and change this line:

local   all             postgres                                peer

to

local   all             postgres                                trust

Restart the server

$ sudo service postgresql restart

Login into psql and set your password

$ psql -U postgres

Change the password for role postgres

$ dbname=# ALTER USER postgres with password 'your-pass';

Finally change the pg_hba.conf from

local   all             postgres                                trust

to

local   all             postgres                                md5

After restarting the postgresql server, you can access it with your own password

Authentication methods details:

  • trust - anyone who can connect to the server is authorized to access the database
  • peer - use client's operating system user name as database user name to access it.
  • md5 - password-base authentication

API

If you update your Django application, you can restart the Gunicorn process to pick up the changes by typing:

$ sudo systemctl restart gunicorn

If you change Gunicorn socket or service files, reload the daemon and restart the process by typing:

$ sudo systemctl daemon-reload
$ sudo systemctl restart gunicorn.socket gunicorn.service

If you change the Nginx server block configuration, test the configuration and then Nginx by typing:

$ sudo nginx -t && sudo systemctl restart nginx

Support

Reach out to me at one of the following places!


License

License

tabiri_api's People

Contributors

ochomoswill avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.