Skip to content

NCKU-CCS/dengue-backend

Repository files navigation

Dengue App - Backend Server

Table of content

Getting Started

Prerequistes

Install Postgis on macOS

brew install PostgreSQL
brew install postgis
brew install gdal
brew install libgeoip

Install Redis on macOS

brew install redis

Install Pillow Dependency on Ubuntu

sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

Run dengue-backend OUTSIDE Docker Container

Setup Backend

  • Install Backend Packages
pipenv install
  • Setup environment variables
export DENGUE_SECRET_KEY="some hard to guess value"
export DENGUE_DB_NAME="database name for postgis"
export DENGUE_DB_USER="user name for postgis"
export DENGUE_DB_PASSWORD="user password for postgis"
export DENGUE_DB_HOST="host for postgis"
export DENGUE_DB_PORT="port for postgis"
export DENGUE_CACHE_LOCATION="redis uri"
export AWS_ACCESS_KEY="AWS access key"
export AWS_SECRET_KEY="AWS secret key"
export GOOGLE_MAP_API_KEY="Google Map API key"

Setup Postgis

  • Create user
$ createuser -P -e dengue_user

Enter password for new role: dengue
Enter it again: dengue
CREATE ROLE dengue_user PASSWORD ...
  • Create database
createdb dengue_db
  • Start Postgres server
pg_ctl -D /usr/local/var/postgres start
  • Create postgis extension
$ psql

postgres=# GRANT ALL PRIVILEGES ON DATABASE dengue_db TO dengue_user;
postgres=# \c dengue_db;
dengue_db=# CREATE EXTENSION postgis;
CREATE EXTENSION
  • Initial Database
    • Different can be configed in dengue/dengue/settings
pipenv run python dengue/manage.py migrate --settings=dengue.settings.<ENV>
  • Create Superuser
    • Different can be configed in dengue/dengue/settings
$ pipenv run python dengue/manage.py createsuperuser --settings=dengue.settings.<ENV>

Username: admin
Email address: [email protected]
Password: some-secret
Password: (again): some-secret
Superuser created successfully.
  • Initialize Data
    • Different can be configed in dengue/dengue/settings
# Insert Substitute
pipenv run python dengue/manage.py init_taiwan_data --settings=dengue.settings.<ENV>

# Insert Hospitial
pipenv run python dengue/manage.py init_hospital_data --settings=dengue.settings.<ENV>

Setup Redis

  • Start
redis-server

Setup Frontend

cd dengue/static/
npm install
npm run typings install
npx tsc

Start Backend Server

  • Start a local server
    • Different can be configed in dengue/dengue/settings
pipenv run python dengue/manage.py runserver --settings=dengue.settings.<ENV>
  • Start production server
cd dengue/
sudo uwsgi --ini dengue.ini
  • Stop production server
sudo killall -s INT uwsgi

Run dengue-backend INSIDE Docker Container

  • Setup environement variables by creating env.cfg at the root directory (Use env-template.cfg as the template for env.cfg)
    • Note that the following key pairs should have the same value
      • POSTGRES_DBNAME, DENGUE_DB_NAME
      • DENGUE_DB_USER, POSTGRES_USER
      • DENGUE_DB_PASSWORD, POSTGRES_PASS
    • INIT_DB should be true and GOOGLE_MAP_API_KEY should be proper API key only when the database is first created and used to initial data
    • The hostname and other nginx configuration can be configured in. docker/nginx.
docker-compose build
docker-compose up -d

Migrate Database Manually

It's possible that the "web" container runs before "postgis" contain is ready and thus cannot migrate database in time.
If this happens, you will get a 502 response when you enter http://127.0.0.1.
In such case, you should manually migrate the database.

Use docker ps command to find the container ID of "dengue-backend_web" and execute the following command to migrate the database.

docker exec -it <CONTAINER ID>  python3 dengue/manage.py migrate --settings=dengue.settings.production

Create Super User

docker exec -it <CONTAINER ID>  python3 dengue/manage.py createsuperuser --settings=dengue.settings.production

Clean Up Docker Image and Volumes (Use with Cautious)

Note that this command will clean up all the data.

docker-compose down -v --rmi local

License

Copyright (c) NCKU The Mosquito Man Project. All rights reserved.

Licensed under the MIT License.