Skip to content

Commit

Permalink
Add Django as backend API to development env:
Browse files Browse the repository at this point in the history
- update docker-compose to include django and
  database services
- add STRTA scripts for local development
- bootstrap Django to be used as API backend with
  necessary dependencies, middleware, db config
- update start command in the package.json for frontend
  so that it can proxy the django API locally
- update README for the STRTA section
- update gitignore to ignore byte-compiled python
  files and static files from Django
  • Loading branch information
aaronxsu committed Mar 24, 2022
1 parent 0bedcc4 commit d1d63a3
Show file tree
Hide file tree
Showing 25 changed files with 495 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ typings/

# Amplify configs for the frontend app
taui/src/aws-exports.js

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

src/backend/static
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ versions are not installed).
Navigate to http://localhost:9966 to view the development environment.


### STRTA

This project uses [`scripts-to-rule-them-all`](https://github.com/azavea/architecture/blob/master/doc/arch/adr-0000-scripts-to-rule-them-all.md) to bootstrap, test, and maintain projects consistently across all teams. Below is a quick explanation for the specific usage of each script.

| Script | Use |
| ----------- | ---------------------------------------------------------- |
| `bootstrap` | Pull down secrets from S3 |
| `infra` | Execute Terraform subcommands with remote state management |
| `manage` | Issue Django management commands |
| `server` | Start the frontend and backend services |
| `setup` | Setup the project development environment |
| `test` | Run linters and tests |
| `update` | Update project, assemble, run migrations |


### Logging In

Once it is running, log in using staging credentials. From there, you can make a Client ID by
Expand Down
7 changes: 6 additions & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
version: '3'
version: '2.1'
services:
taui:
environment:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY

terraform:
image: quay.io/azavea/terraform:0.11.11
volumes:
Expand All @@ -17,10 +18,14 @@ services:
- ECHOLOCATOR_SITE_BUCKET=${ECHOLOCATOR_SITE_BUCKET:-echo-locator-staging-site-us-east-1}
working_dir: /usr/local/src
entrypoint: bash

amplify:
build: ./deployment/amplify
volumes:
- ./deployment/amplify:/usr/local/src
- $HOME/.aws:/root/.aws:ro
working_dir: /usr/local/src
entrypoint: bash

django:
image: "echolocator:${GIT_COMMIT:-latest}"
51 changes: 50 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: '2.1'
services:
taui:
build: ./taui
Expand All @@ -10,3 +10,52 @@ services:
- "9966:9966"
entrypoint: yarn
command: start

django:
image: echolocator
environment:
- POSTGRES_HOST=database
- POSTGRES_PORT=5432
- POSTGRES_USER=echolocator
- POSTGRES_PASSWORD=echolocator
- POSTGRES_DB=echolocator
- DJANGO_ENV=Development
- DJANGO_SECRET_KEY=secret
- DJANGO_LOG_LEVEL=INFO
build:
context: ./src/backend
dockerfile: Dockerfile
volumes:
- ./src/backend:/usr/local/src/backend
- $HOME/.aws:/root/.aws:ro
working_dir: /usr/local/src/backend
depends_on:
database:
condition: service_healthy
command:
- "-b :8085"
- "--reload"
- "--timeout=90"
- "--access-logfile=-"
- "--error-logfile=-"
- "--log-level=debug"
- "echo.wsgi"
ports:
- "8085:8085"
cpus: 2

database:
image: postgis/postgis:13-3.1
expose:
- "5432"
environment:
- POSTGRES_USER=echolocator
- POSTGRES_PASSWORD=echolocator
- POSTGRES_DB=echolocator
healthcheck:
test: [ "CMD", "pg_isready", "-U", "echolocator" ]
interval: 3s
timeout: 3s
retries: 3
start_period: 5s
command: postgres -c log_statement=all
35 changes: 35 additions & 0 deletions scripts/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

set -e

if [[ -n "${ECHOLOCATOR_DEBUG}" ]]; then
set -x
fi

DIR="$(dirname "${0}")/../"

function usage() {
echo -n \
"Usage: $(basename "$0")
Pull down secrets from S3.
"
}

# TODO: potentially as a part of this issue: https://github.com/azavea/echo-locator/issues/376
# after switching to a different bundler, we may want to copy and populate some env vars
function pull_env() {
pushd "${DIR}"

echo "Pulling .env from ${1}"
# aws s3 cp "s3://${1}/.env" ".env"

popd
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
if [ "${1:-}" = "--help" ]; then
usage
else
pull_env
fi
fi
25 changes: 25 additions & 0 deletions scripts/manage
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -e

if [[ -n "${ECHOLOCATOR_DEBUG}" ]]; then
set -x
fi

function usage() {
echo -n \
"Usage: $(basename "$0")
Run a Django management command
"
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
if [ "${1:-}" = "--help" ]; then
usage
else
docker-compose \
run --rm --entrypoint python \
django \
manage.py "$@"
fi
fi
5 changes: 2 additions & 3 deletions scripts/server
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ fi
function usage() {
echo -n \
"Usage: $(basename "$0")
Run a development server.
Starts servers using docker-compose.
"
}

Expand All @@ -20,6 +19,6 @@ then
then
usage
else
docker-compose up --build
docker-compose up
fi
fi
23 changes: 23 additions & 0 deletions scripts/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -e

if [[ -n "${ECHOLOCATOR_DEBUG}" ]]; then
set -x
fi

function usage() {
echo -n \
"Usage: $(basename "$0")
Attempts to setup the project's development environment.
"
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
if [ "${1:-}" = "--help" ]; then
usage
else
./scripts/bootstrap
./scripts/update
fi
fi
41 changes: 41 additions & 0 deletions scripts/update
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

set -e

if [[ -n "${ECHOLOCATOR_DEBUG}" ]]; then
set -x
fi

function usage() {
echo -n \
"Usage: $(basename "$0")
Build container images and execute database migrations.
"
}

function cleanup() {
docker-compose stop
}

trap cleanup ERR

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
if [ "${1:-}" = "--help" ]; then
usage
else
# Ensure container images are current
docker-compose build

# Bring up PostgreSQL and Django in a way that respects
# configured service health checks.
docker-compose up -d database django

# Apply any outstanding Django migrations
./scripts/manage migrate

# Collect Django static files
./scripts/manage collectstatic --no-input

docker-compose stop
fi
fi
28 changes: 28 additions & 0 deletions src/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM quay.io/azavea/django:3.2-python3.9-slim

RUN mkdir -p /usr/local/src/backend
WORKDIR /usr/local/src/backend

COPY requirements.txt /usr/local/src/backend/
RUN set -ex \
&& buildDeps=" \
build-essential \
" \
&& deps=" \
postgresql-client-13 \
" \
&& apt-get update && apt-get install -y $buildDeps $deps --no-install-recommends \
&& pip install --no-cache-dir -r requirements.txt \
&& apt-get purge -y --auto-remove $buildDeps

COPY . /usr/local/src/backend

CMD ["-b :8085", \
"--workers=1", \
"--timeout=60", \
"--access-logfile=-", \
"--access-logformat=%({X-Forwarded-For}i)s %(h)s %(l)s %(u)s %(t)s \"%(r)s\" %(s)s %(b)s \"%(f)s\" \"%(a)s\"", \
"--error-logfile=-", \
"--log-level=info", \
"--capture-output", \
"echo.wsgi"]
1 change: 1 addition & 0 deletions src/backend/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'api.apps.ApiConfig'
3 changes: 3 additions & 0 deletions src/backend/api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions src/backend/api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
Empty file.
3 changes: 3 additions & 0 deletions src/backend/api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions src/backend/api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions src/backend/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
Empty file added src/backend/echo/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions src/backend/echo/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for echo project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'echo.settings')

application = get_asgi_application()
Loading

0 comments on commit d1d63a3

Please sign in to comment.