hackoregon / backend-examplar-2018

an example dockerized geo-aware django backend
MIT License
4 stars 5 forks source link

Example Geo-Capable Django REST framework (DRF) API using Docker

This is a simple example repo to quickstart a DRF API within a Docker container.

Allows for multiple environments to run API, through series of docker-compose files. You should be able to replace the Django Code, making a few updates and get this running with a new API or existing one.

TL;DR Does this work?

This exemplar is the basis for the APIs that are currently successfully deployed here:

  1. Transportation Systems (API, source repo)
  2. Disaster Resilience (API, source repo)

Main Parts of Repo

DOCKER related:

API Related:

Quickstart for your own API - Development

Once you understand the sample you can create your own api. This repo contains a quickstart script that is intended to remove the sample app and quickly spin up a development repo. It is only intended to remove the sample application. It will not delete an api if it is already began. See Manual Setup steps below on how to manually remove an existing API and start or copy in your own.

  1. cp env.sample .env in the root of the repo (this file is already in the .gitignore, so you should not have to worry about it getting accidentally checked into a GitHub repo)

  2. Edit your .env file and change the variables to appropriate values for your project's database backup. Ensure that the values PROJECT_NAME, POSTGRES_NAME, and DATABASE_OWNER are set correctly. You should not need to change any of the other values. If you unsure of what values to use for these settings or do not have a database backup file, please contact your Team's Data Manager before proceeding further.

PROJECT_NAME=dead_songs

# the database superuser name - this is the default
POSTGRES_USER=postgres

# the database name the API will connect to - "dbname" in most PostgreSQL command-line tools
POSTGRES_NAME=dead_songs

# the database owner - automatic restore needs this (most likely only used in dev)
DATABASE_OWNER=sampleuser

# *service* name (*not* image name) of the database in the Docker network
POSTGRES_HOST=db_development

# port the database is listening on in the Docker network
POSTGRES_PORT=5432

# password for the PostgreSQL database superuser in the database container
POSTGRES_PASSWORD=sit-down-c0mic

# the password for the teams
TEAM_PASSWORD=d0wn!0ff!a!duck

# Django secret key in the API container
DJANGO_SECRET_KEY=r0ck.ar0und.the.c10ck
  1. Copy your database backup file into the backup folder. Database container is a PostGIS-enabled 9.6 container. Backup can be a .backup, .sql, or .sql.gz format.

  2. Run the create-project script: ./bin/create-api-project.sh (This will delete all files related to the sample app and replace with a new Django REST framework app with your project name. It will also replace the default settings.py file with the sample.py, which has been pre-configured a bit for our stack.)

  3. This would be a good point to instantiate a new Git history for the project going forward. You can remove the existing git by removing the hidden folder - make sure you are in the repo directory and run rm -rf .git. You can then run git init to start a new history and then add your remote branch.

  4. Run the bin/build.sh script to build the project. Since you are going to be running it on the local machine you will want to run: ./bin/build.sh -d, this command is doing a docker-compose build in the background. It is downloading the images needed for the project to your local machine.

  5. Once this completes you will now want to start up the project. We will use the start.sh script for this, again using the -d flag to run locally: ./bin/start.sh -d The first time you run this you will see the database restores. You will also see the api container start up.

  6. Create your api code. Checkout the Django REST framework Guide on how to proceed.

Manually Setup an API - Development

If you would prefer or need to startup your own API, here are the steps to walk through:

  1. Follow Steps 1-3 under the Quickstart Guide

  2. Remove the Django related and any old database files, not related to your project. For the sample app this would be:

  1. You will want to tear down and remove the entire docker stack to confirm no extra artifacts from the sample app are remaining in your new build: docker-compose -f development-docker-compose.yml down --rmi all --volumes --remove-orphans

  2. Now you will want to create the sample app within your docker container. The development-docker-compose.yml file gives the basic config of your setup, connecting to the env variables specified. It specifies the root of the repo directory as the local storage volume so you will see files created within the repo. :

docker-compose -f development-docker-compose.yml run --no-deps --rm \
  api_development \
  /bin/bash -c "django-admin.py startproject something_else_2fhdfsl . ; python manage.py startapp api"
  1. If on Linux, you may need to change ownership on the files created from the docker container:
ls -l
echo "sudo chown -R `id -u $USER`:`id -g $USER` ."
sudo chown -R `id -u $USER`:`id -g $USER` .
ls -l
  1. There is a recommended settings.py file in the bin called example_settings.py. This is the basic current example which works with the intended python packages for both development and production. It is configured to pickup the os variables related to Django and the Database. In a new API you should be able to copy over the file, renaming and replacing the auto generated settings.py. After this, you can replace all occurances of <EXAMPLE_PROJECT_NAME> with your actual project name

  2. Continue with steps 5-7 of Quickstart guide to complete building the API

Run the Sample

There is currently a Sample API included within the repo. To run:

  1. First cd into the root directory of your clone of the repo and run the command cp env.sample .env

  2. Build the development containers using the command: ./bin/build.sh -d. If this script won't run, you may need to confirm you have executable perms on all the scripts in the ./bin folder: $ chmod +x ./bin/*.sh Feel free to read each one and assign perms individually, 'cause it is your computer :stuck_out_tongue_winking_eye: and security is a real thing.

  3. Once this completes you will now want to start up the project. We will use the start.sh script for this, again using the -d flag to run locally: ./bin/start.sh -d The first time you run this you will see the database restores. You will also see the api container start up.

  4. Open your browser and you will be able to access the Django REST framework browserable front end. The IP address you use will depend on your Docker hosting:

    • Windows 10 Pro + Docker for Windows, MacOS or Linux: API root http://localhost:8000/api, Swagger API schema http://localhost:8000/schema
    • Docker Toolbox running on Windows or Mac: API root http://MACHINE-IP:8000/api, Swagger API schema http://MACHINE-IP:8000/schema

      where MACHINE-IP is the IP address docker-machine ip returns.

  5. You can stop the container using Ctrl-C to stop the process in the terminal window.

Run the Tests

This repo contains some tests for the Sample API. It will import a fixtures file containing example data that has been exported from the Django API using the dumpdata command:

./manage.py dumpdata --indent=2 -v 2 --traceback api > ./api/fixtures/songs.json

To run the tests:

  1. Run ./bin/test.sh -d - this will load the fixtures and run tests.

Currently this repo contains two type of tests:

Contributors and History

This repo represents the work of many members of the Hack Oregon project team. The roots of this work began with the 2017 backend-service-pattern, the work of the DevOps and platform teams, and the APIs deployed for the 2017 seasons.

This current implementation builds on the transportation-system-backend and passenger_census_api. The database structure is an implementation of the PostGIS container of the data-science-pet-containers repo.

Major Contributors