RafayGhafoor / ihr-django

Internet Health Report API

Home Page:https://ihr.iijlab.net/ihr/en-us/api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ihr-django

Internet Health Report API

This is the implementation for the IHR API: https://ihr.iijlab.net/ihr/en-us/api

Installation of Django and IHR app

Required packages for Ubuntu:

sudo apt install apache2 python3 python3-pip postgresql postgresql-contrib

Install virtualenv and django:

pip3 install virtualenv
virtualenv ihr

Install django:

cd ihr
. bin/activate
pip install django

Create a new django project:

django-admin startproject internetHealthReport

Copy IHR's django application and install dependencies:

cd internetHealthReport
git clone git@github.com:InternetHealthReport/ihr-django.git ihr
pip install -r ihr/requirements.txt

Then copy settings.py, urls.py, wsgi.py to the correct place:

cp ihr/config/*.py internetHealthReport/

You may have to adjust some variables in settings.py to match your database, smtp account, recapcha credentials.

Create the database and django user (change password as needed):

sudo su postgres
psql
postgres=# CREATE DATABASE ihr;
CREATE DATABASE
postgres=# CREATE USER django WITH PASSWORD '123password456';
CREATE ROLE
postgres=# ALTER ROLE django SET client_encoding TO 'utf8';
ALTER ROLE
postgres=# ALTER ROLE django SET timezone TO 'UTC';
ALTER ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE ihr TO django;
GRANT
postgres=#\q
exit

Remove migration files and create tables: (TODO move production migration files to a different repository)

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete
./manage.py makemigrations
manage.py migrate

Start django:

./manage.py runserver

Go to http://127.0.0.1:8000/hegemony/ to check if it is working.

Add test data to the database

In the production database some of the ids are changed to BIGINT. We should locally apply these changes before importing data:

psql -U django -d ihr -c "ALTER TABLE ihr_hegemony ALTER COLUMN id SET DATA TYPE bigint"
psql -U django -d ihr -c "ALTER TABLE ihr_hegemony_prefix ALTER COLUMN id SET DATA TYPE bigint"
psql -U django -d ihr -c "ALTER TABLE ihr_hegemony_country ALTER COLUMN id SET DATA TYPE bigint"
psql -U django -d ihr -c "ALTER TABLE ihr_atlas_delay ALTER COLUMN id SET DATA TYPE bigint"

Download a database snapshot and load it:

wget https://ihr-archive.iijlab.net/ihr-dev/psql-snapshot/2022-03-10/2022-03-10_psql_snapshot.sql.lz4 
lz4 2022-03-10_psql_snapshot.sql.lz4 
psql -U django ihr < 2022-03-10_psql_snapshot.sql

Running the application

Activate the python environment and lunch django server:

cd ihr 
. bin/activate
internetHealthReport/manage.py runserver

Go to http://127.0.0.1:8000/hegemony/ to check if it is working.

Working with a local instance of IHR website (https://github.com/InternetHealthReport/ihr-website)

To redirect all API calls to the local django server you should change the API URL in ihr-website/src/plugins/IhrApi.js:

const IHR_API_BASE = 'http://127.0.0.1:8000/'

About

Internet Health Report API

https://ihr.iijlab.net/ihr/en-us/api

License:GNU General Public License v3.0


Languages

Language:Python 73.8%Language:HTML 25.7%Language:CSS 0.5%Language:JavaScript 0.0%