The backend for the CnCNet map browser.
UI is here https://github.com/CnCNet/cncnet-map-ui
The mascot for the backend API is Kirovy, by Direct & Dominate
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.
- Docker
- An IDE (PyCharm strongly recommended)
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 usefrom django.core.management.utils import get_random_secret_key; print(get_random_secret_key())
in a python shell to generate it. - Optionally you can set
DEBUG
to1
if needed - Set
RUN_ENVIRONMENT=dev
if you want to see the developer endpoints.
Run the following command to start the API:
docker compose up -d
Now you can access the API at
http://localhost
Once the api is started you can run the tests using the following command.
Run tests
docker exec -it mapdb-django-dev pytest
This section is still a WIP...
Follow this guide to set up the app for production.
- Docker
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 usefrom 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
to0
- You MUST set
RUN_ENVIRONMENT
toproduction
Run the following command to start the API:
docker compose -f docker-compose.prod.yml up -d
You should use docker compose, but here are the native OS instructions in case you don't want to.
Warning
The native OS instructions will not be maintained going forward. If you notice an issue with them, please PR the fix.
- Download and install pyenv
You don't have to use
pyenv
but it makes life much easier when dealing with virtual environments. - Install PostgreSQL for your system. This is required for Django
- On Mac you can do
brew install postgresql
if you have brew installed.
- On Mac you can do
- Install LibMagic for Python Magic
- On Mac you can do
brew install libmagic
if you have brew installed.
LibMagic is used for checking file types.
- On Mac you can do
- Checkout the repository
- Switch to the repository directory
- Setup Python
- Install Python 3.12
pyenv install 3.12
or whatever the latest python is. - Setup the virtual environments
pyenv virtualenv 3.12 cncnet-map-api
- Set the virtual environment for the directory
pyenv local cncnet-map-api
- Install Python 3.12
- Install the dev requirements
pip install -r requirements-dev.txt
- On Apple Silicon you'll need to install lzo with
brew install lzo
then runCFLAGS=-I$(brew --prefix)/include LDFLAGS=-L$(brew --prefix)/lib pip install -r requirements-dev.txt
to getpython-lzo
to install. You shouldn't need to include those flags again unlesspython-lzo
updates.
- On Apple Silicon you'll need to install lzo with
- Install the pre-commit hooks
pre-commit install
- Setup the environment variables
- Create a
.env
file at the root of the repo - Copy the contents of
example.env
to your.env
file. - Fill out the required values in
.env
- If the app doesn't run due to a missing required variable, add said variable to
example.env
because the person who made the variable forgot to do so.
- Create a
- Run the
db
service indocker-compose
- Load your
.env
file into your shell, (you can usesource load_env.sh && read_env
) then migrate the database./manage.py migrate
- Run the django server with
./manage.py runserver
Tests can be run by following these instructions
Chairman Bing of the Massivesoft corporation strikes again; getting the LZO
libraries running
natively on Windows is a... less-than-pleasant effort. So use docker instead.
- Install docker for windows. I have had success with Rancher Desktop or Docker Desktop
- After docker is running, switch to your local git repo and run
docker compose up windows-dev -d
. Make sure the build succeeds. - Set
windows-dev
as your python interpreter for whichever editor you use.
Tip
In Pycharm you go to Settings > Project > Python Interpreter > Add Interpreter > Docker Compose
I strongly recommend using PyCharm (or any other Python IDE with breakpoints) and the .env
plugin for running the PyTests.
All you need to do is run the database from docker-compose
, then launch the tests via PyCharm.
If you want to run the tests via CLI:
- Make sure your database is running from the docker compose file.
docker-compose start db
- Make sure your environment variables are setup and loaded to your shell. See backend dev setup
- Run
DJANGO_SETTINGS_MODULE="kirovy.settings.testing" pytest tests
Django should automatically run migrations as part of the test startup.
Run tests with docker compose:
docker-compose up --build test