Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ jobs:
- name: Build docker images
run: docker compose build

- name: Run containers
run: docker compose up -d

- name: Run PyTest
run: docker compose run test
run: docker exec mapdb-django-dev pytest
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Follow this guide to set up the api locally for development.
> [!note]
> By default, we are using Docker for the sake of simplicity. If you want to run the API natively, you can check out the section [Native Guide](#native-guide).

- Docker with compose
- Docker
- An IDE (PyCharm strongly recommended)

### Set up environment variables
Expand All @@ -41,11 +41,47 @@ You can copy `example.env` to `.env` and fill in the required values.
Run the following command to start the API:

```bash
docker compose up nginx-server -d
docker compose up -d
```

> Now you can access the API at `http://localhost`

### Running the tests

Once the api is started you can run the tests using the following command.

Run tests
```
docker exec -it mapdb-django-dev pytest
```

## Production

> This section is still a WIP...

Follow this guide to set up the app for production.

### Prerequisites

- Docker

### Set up environment variables

You can copy `example.env` to `.env` and fill in the required values.

- Set `POSTGRES_PASSWORD` to a secure password.
- Set `SECRET_KEY` to a secure secret key. You can use `from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())` in a python shell to generate it.
- You MUST set `DEBUG` to `0`
- You MUST set `RUN_ENVIRONMENT` to `production`

### Running the API

Run the following command to start the API:

```bash
docker compose -f docker-compose.prod.yml up -d
```

## Native Guide

You should use docker compose, but here are the native OS instructions in case you don't want to.
Expand Down
44 changes: 44 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
services:
db:
container_name: mapdb-postgres
image: postgres
volumes:
- ${POSTGRES_DATA_DIR}:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
env_file:
- .env
ports:
- "127.0.0.1:${POSTGRES_PORT}:${POSTGRES_PORT}"
command: -p ${POSTGRES_PORT}

django:
container_name: mapdb-django
build:
context: .
dockerfile: docker/Dockerfile
target: prod
volumes:
- ${HOST_MEDIA_ROOT}:/data/cncnet_silo # django will save user-uploaded files here. MEDIA_ROOT
- ${HOST_STATIC_ROOT}:/data/cncnet_static # django will gather static files here. STATIC_ROOT
ports:
- "8000:8000"
env_file:
- .env
depends_on:
- db

nginx-server:
# nginx proxies requests to django via gunicorn.
container_name: mapdb-nginx
image: nginx:latest
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ${HOST_STATIC_ROOT}:/usr/share/nginx/html/static # website static assets.
- ${HOST_MEDIA_ROOT}:/usr/share/nginx/html/silo # website user-uploaded files.
ports:
- "80:80"
depends_on:
- django
44 changes: 8 additions & 36 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
services:
db:
container_name: mapdb-postgres-dev
image: postgres
volumes:
- ${POSTGRES_DATA_DIR}:/var/lib/postgresql/data
Expand All @@ -14,8 +15,11 @@ services:
command: -p ${POSTGRES_PORT}

django:
# Won't serve files on its own. Launch nginx-server to run the whoe app.
build: .
container_name: mapdb-django-dev
build:
context: .
dockerfile: docker/Dockerfile
target: dev
volumes:
- .:/cncnet-map-api
- ${HOST_MEDIA_ROOT}:/data/cncnet_silo # django will save user-uploaded files here. MEDIA_ROOT
Expand All @@ -28,46 +32,14 @@ services:
- db

nginx-server:
# This is the prod server service.
# `docker compose up nginx -d` will run the whole app.
# nginx proxies requests to django via gunicorn.
container_name: mapdb-nginx-dev
image: nginx:latest
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./docker/nginx.conf:/etc/nginx/nginx.conf:ro
- ${HOST_STATIC_ROOT}:/usr/share/nginx/html/static # website static assets.
- ${HOST_MEDIA_ROOT}:/usr/share/nginx/html/silo # website user-uploaded files.
ports:
- "80:80"
depends_on:
- django

test:
build:
context: ./
dockerfile: test.DockerFile
volumes:
- .:/cncnet-map-api
env_file:
- .env
environment:
POSTGRES_TEST_HOST: db # Necessary to connect to docker db. Overrides the .env setting.
depends_on:
- db
command:
- pytest

windows-dev:
# Use this for windows. The LZO libraries are annoying to deal with without using docker.
# Chairman Bing of Massivesoft strikes again.
build:
context: ./
dockerfile: test.DockerFile
volumes:
- .:/cncnet-map-api
- ./data/tmp:/tmp/pytest-of-root # For inspecting files during pytests
env_file:
- .env
environment:
POSTGRES_TEST_HOST: db # Necessary to connect to docker db. Overrides the .env setting.
depends_on:
- db
19 changes: 15 additions & 4 deletions Dockerfile → docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12-bookworm as base
FROM python:3.12-bookworm AS base

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
Expand All @@ -12,11 +12,22 @@ COPY requirements.txt /cncnet-map-api
COPY requirements-dev.txt /cncnet-map-api
COPY web_entry_point.sh /cncnet-map-api

RUN apt-get update && apt-get install -y liblzo2-dev # Compression library used by westwood.
RUN apt-get install libmagic1 # File type checking.
# liblzo2 is a compression library used by westwood.
# libmagic1 is used for detecting file mime types by analyzing the file contents.
RUN apt-get update && apt-get install -y liblzo2-dev libmagic1
RUN pip install --upgrade pip
RUN chmod +x /cncnet-map-api/web_entry_point.sh

FROM base AS dev
# The cflags are needed to build the lzo library on Apple silicon.
RUN CFLAGS=-I$(brew --prefix)/include LDFLAGS=-L$(brew --prefix)/lib pip install -r ./requirements-dev.txt
ENTRYPOINT "/cncnet-map-api/web_entry_point.sh"

FROM base AS prod
# The cflags are needed to build the lzo library on Apple silicon.
RUN CFLAGS=-I$(brew --prefix)/include LDFLAGS=-L$(brew --prefix)/lib pip install -r ./requirements.txt

COPY . /cncnet-map-api

RUN chmod +x /cncnet-map-api/web_entry_point.sh
ENTRYPOINT "/cncnet-map-api/web_entry_point.sh"

File renamed without changes.
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ POSTGRES_DATA_DIR=./data/db/
# In docker the host will always be DB, but when run locally it will probably be local host.
# The reason this is separate from production host is because putting PyCharm breakpoints in docker has issues
# and pytests often need to be run locally to have breakpoints work.
POSTGRES_TEST_HOST=localhost
POSTGRES_TEST_HOST=db

# Port for postgres
POSTGRES_PORT=5432
Expand Down
18 changes: 0 additions & 18 deletions test.DockerFile

This file was deleted.