Skip to content

Commit

Permalink
issue #3:
Browse files Browse the repository at this point in the history
- use python-decouple for environment settings
- add .env.example file to get started
- remove default db user and password mentionings
- update README documentation
  • Loading branch information
defgsus committed Dec 21, 2021
1 parent f4a1e1b commit 155a473
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 121 deletions.
4 changes: 0 additions & 4 deletions .dockerignore

This file was deleted.

16 changes: 10 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
.idea/
*.iml
__pycache__/
*.pyc
env/
venv/
.env*

cache/
web/static/
settings_local.py
/web/static/
/web/media/
*credentials*

.idea/
*.iml

__pycache__/
*.pyc
41 changes: 0 additions & 41 deletions Dockerfile

This file was deleted.

64 changes: 21 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,37 @@ Alternatively, you can run the
docker run --name some-postgis -e POSTGRES_PASSWORD=<the password> -d postgis/postgis
```

#### Create and setup database
#### Setup and create database

```shell script
First copy the [web/.env.example](web/.env.example) file to `web/.env` and edit.
Specifically `POSTGRES_USER` and `POSTGRES_PASSWORD` must be defined.

The create the database execute these commands and replace `<user>` and `<password>`
with your values:

```sh
# start psql
sudo -u postgres psql

CREATE USER "park_api" WITH PASSWORD '...';
CREATE DATABASE "parkapi2" ENCODING=UTF8 OWNER="park_api";
CREATE USER "<user>" WITH PASSWORD "<password>";
CREATE DATABASE "parkapi2" ENCODING=UTF8 OWNER="<user>";
CREATE DATABASE "parkapi2-test" ENCODING=UTF8 OWNER="<user>";

# connect to each database and enable postgis
\c parkapi2
CREATE EXTENSION postgis;

# allow park_api user to create the unittest database and
# enable the postgis extension
ALTER USER "park_api" SUPERUSER;
\c parkapi2-test
CREATE EXTENSION postgis;
```

> Note that `ALTER USER "park_api" CREATEDB;` is usually enough for
> running the unittests but the `postgis` extension
> [can only be enabled by a superuser](https://dba.stackexchange.com/questions/175319/postgresql-enabling-extensions-without-super-user/175469#175469).
### Run local server and unittests

Then in the `web/` directory call:
In the `web/` directory call:

```shell script
# run unittests
./manage.py test
./manage.py test --keepdb

# init the main database
./manage.py migrate
Expand All @@ -170,7 +178,7 @@ By default, the django admin interface is available at
[localhost:8000/admin/](http://localhost:8000/admin/) and the
swagger api documentation is at
[localhost:8000/api/docs/](http://localhost:8000/api/docs/) and
a simple overview page at [localhost:8000/](http://localhost:8000/).
a simple *developmental* overview page at [localhost:8000/](http://localhost:8000/).


To get data into the database call:
Expand All @@ -182,33 +190,3 @@ To get data into the database call:
./manage.py pa_find_locations
```


## Docker and CI

The [Dockerfile](Dockerfile) is an Ubuntu based image. It's probably possible
to switch to Alpine but the postgis libraries are currently not working in
my attempts.

```shell script
docker build --tag parkapi-dev .
```

#### run unittests in docker container

```shell script
docker run -ti --env PARKAPI_RUN_TESTS=1 --net host parkapi-dev
```

Running the container with `--net host` will attach to the postgres at localhost. Please
check the example in the
[postgis docker README](https://github.com/postgis/docker-postgis#readme)
how to connect to a different host.

#### running the server in docker container

```shell script
docker run -ti --env DJANGO_DEBUG=True --net host parkapi-dev
```

Running in non-DEBUG mode will not deliver static files as this
requires a webserver like nginx or apache.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ djangorestframework==3.12.4
drf-yasg==1.20.0
Markdown==3.3.6
psycopg2-binary==2.9.2
python-decouple==3.5
25 changes: 25 additions & 0 deletions web/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -- django settings --

# This must never be True in production!
https://docs.djangoproject.com/en/4.0/ref/settings/#debug
DJANGO_DEBUG=False

# https://docs.djangoproject.com/en/4.0/ref/settings/#secret-key
DJANGO_SECRET_KEY=<put some long random garbage here>

# for production this must contain your.host.name
# https://docs.djangoproject.com/en/4.0/ref/settings/#allowed-hosts
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1

# -- database settings --

POSTGRES_DATABASE=parkapi2
POSTGRES_TEST_DATABASE=parkapi2-test

# define database user and password
POSTGRES_USER=
POSTGRES_PASSWORD=

# define these if the postgres does not run on localhost:5432
# POSTGRES_HOST=
# POSTGRES_PORT=
36 changes: 16 additions & 20 deletions web/park_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path

DATETIME_FORMAT = "Y-m-d H:i:s T"
from decouple import config


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -24,30 +24,23 @@

# --- CI variables --

POSTGRES_DATABASE = os.environ.get('POSTGRES_DATABASE', 'parkapi2')
POSTGRES_TEST_DATABASE = os.environ.get('POSTGRES_DATABASE', 'parkapi2_test')
POSTGRES_HOST = os.environ.get('POSTGRES_HOST', 'localhost')
POSTGRES_PORT = os.environ.get('POSTGRES_PORT', '5432')
POSTGRES_USER = os.environ.get('POSTGRES_USER', 'park_api')
POSTGRES_PASSWORD = os.environ.get('POSTGRES_PASSWORD', 'park_api')
POSTGRES_DATABASE = config('POSTGRES_DATABASE')
POSTGRES_TEST_DATABASE = config('POSTGRES_TEST_DATABASE')
POSTGRES_HOST = config('POSTGRES_HOST', default='localhost')
POSTGRES_PORT = config('POSTGRES_PORT', default=5432, cast=int)
POSTGRES_USER = config('POSTGRES_USER')
POSTGRES_PASSWORD = config('POSTGRES_PASSWORD')

SECRET_KEY = os.environ.get(
'DJANGO_SECRET_KEY',
'SECURITY WARNING: keep the secret key used in production secret!'
)
SECRET_KEY = config("DJANGO_SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True if os.environ.get("DJANGO_DEBUG") == "True" else False
DEBUG = config("DJANGO_DEBUG", default=False, cast=bool)

if os.environ.get('DJANGO_ALLOWED_HOSTS'):
ALLOWED_HOSTS = os.environ['DJANGO_ALLOWED_HOSTS'].split()
else:
ALLOWED_HOSTS = ['*']
ALLOWED_HOSTS = config('DJANGO_ALLOWED_HOSTS').split()

if os.environ.get("DJANGO_CSRF_TRUSTED_ORIGINS"):
CSRF_TRUSTED_ORIGINS = os.environ["DJANGO_CSRF_TRUSTED_ORIGINS"].split()
STATIC_ROOT = config("DJANGO_STATIC_PATH", default=BASE_DIR / "static", cast=Path)

STATIC_ROOT = os.environ.get("DJANGO_STATIC_PATH", BASE_DIR / "static")
# --- end CI variables ---

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

Expand Down Expand Up @@ -173,6 +166,9 @@

USE_TZ = False

# make django admin show times flagged with UTC
DATETIME_FORMAT = "Y-m-d H:i:s T"


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
Expand Down
8 changes: 1 addition & 7 deletions web/start-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@
#./manage.py compilemessages || exit 1
./manage.py collectstatic --no-input || exit 1

# check for existence of special env variable to run unittest
if [[ -z "${PARKAPI_RUN_TESTS}" ]]; then
# or run server
gunicorn -b 127.0.0.1:8000 park_api.wsgi || exit 1
else
./manage.py test || exit 1
fi
gunicorn -b 127.0.0.1:8000 park_api.wsgi || exit 1

0 comments on commit 155a473

Please sign in to comment.