Skip to content

Commit

Permalink
Use sensible defaults so everything runs out of the box
Browse files Browse the repository at this point in the history
Change the Django settings so the demo site runs without having to
define any environment variables.

Similarly change the docker-compose.yml file to provide sensible
defaults so the images are built and the containers run without any
additional configuration.

The .env and .env.docker files are replaced by .env.example which
contains all the environment variables used in the project. Now you
only need to create .env when you want to change the way the project
is configured, for example, running the database in a container when
you also have PostgreSQL installed natively.

As a result the getting started guide is simplified with the steps for
adding PostgreSQL and RabbitMQ accounts are now options and are moved
to the end of the guide.
  • Loading branch information
StuartMacKay committed Oct 8, 2023
1 parent 9e4cb8b commit 4e83d6a
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 135 deletions.
39 changes: 0 additions & 39 deletions .env

This file was deleted.

31 changes: 0 additions & 31 deletions .env.docker

This file was deleted.

86 changes: 86 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
# Environment variables used for running the demo site
#
# Django settings and docker-compose.yml are configured with sensible default
# so whether you run the demo with a virtualenv or containers everything works
# out of the box. You will only need to set environment variables if you want
# to change the configuration in some way. For example, you have postgres
# installed natively but you want to use a container instead. In this case you
# need to set DOCKER_POSTGRES_PORT_FORWARD so the port does not clash with the
# post used by the native postgres.

# Configuration for PostgreSQL

# Both POSTGRES_USER and POSTGRES_PASSWORD are required when building the
# postgres image. Both are set to use the default value of 'postgres' in
# docker-compose.yml. Override them here, along with any other configuration
# changes you want.

#POSTGRES_USER=postgres
#POSTGRES_PASSWORD=postgres
#POSTGRES_DB=postgres

# This is the external port visible locally. POSTGRES_PORT is the port in
# the docker virtual network.

#DOCKER_POSTGRES_PORT_FORWARD=5432

# Configuration for RabbitMQ

#RABBITMQ_DEFAULT_USER=guest
#RABBITMQ_DEFAULT_PASS=guest
#RABBITMQ_DEFAULT_VHOST=

# Ports on the local network so the rabbitmq service and flower webserver
# are accessible.

#DOCKER_RABBITMQ_PORT_FORWARD=5672
#DOCKER_FLOWER_PORT_FORWARD=5555

# Configuration for Celery

#CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672/

# Django-feeds settings

# Crontab setting the schedule on which the Celery task, load_feeds, that
# checks for Feeds to be loaded, runs.

#FEEDS_TASK_SCHEDULE=0 * * * *

# Crontab setting the default schedule a Feed is loaded on. This schedule
# can be set on Feeds individually. This schedule must coincide with the
# FEEDS_TASK_SCHEDULE otherwise no feed will be loaded. The Celery task
# runs every hour on the hour however you can set the default schedule to
# every hour on the hour between 8am and 8pm for example. That way you
# can limit fetches to the times that new posts are more likely to be
# published.

#FEEDS_LOAD_SCHEDULE=0 * * * *

# Extra. Accessing postgres on the command line.
#
# This is useful if you want to define Makefile targets for managing the
# database.
#
# Set environment variables used by the postgresql commands, psql, createdb,
# dropdb, etc. to connect to the database. PGPASSWORD is particularly useful
# to avoid having to enter it for each command. You will need to configure
# the server to allow username/password (md5) authentication on local (socket)
# connections and so avoid having to su to the postgres user first.
#
# /etc/postgresql/<version>/main/pg_hba.conf
# local all all md5
#
# For more info on environment variables see,
# https://www.postgresql.org/docs/current/libpq-envars.html
#
# The values must match the variables defined above, obviously.

#PGUSER=postgres
#PGPASSWORD=postgres
#PGDATABASE=postgres

# PGPORT must match DOCKER_POSTGRES_PORT_FORWARD

#PGPORT=5432
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ __pycache__/
!.dockerignore
!.editorconfig
!.envrc
!.env
!.env.docker
!.env.example
!.flake8
!.gitattributes
!.github
Expand Down
19 changes: 13 additions & 6 deletions demo/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""
Demo site settings
The database and celery settings assume everything is running
on localhost. When using a virtualenv, no environment variables
need to be set to run the demo site. When running the site using
docker the variables are defined in the docker-compose file.
"""
import logging
import os
Expand Down Expand Up @@ -58,11 +63,11 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "feeds",
"USER": "feeds",
"PASSWORD": "feeds",
"HOST": os.environ["DB_HOST"],
"PORT": 5432,
"NAME": os.environ.get("POSTGRES_DB", "postgres"),
"USER": os.environ.get("POSTGRES_USER", "postgres"),
"PASSWORD": os.environ.get("POSTGRES_PASS", "postgres"),
"HOST": os.environ.get("POSTGRES_HOST", "localhost"),
"PORT": os.environ.get("POSTGRES_PORT", 5432),
}
}

Expand All @@ -74,7 +79,9 @@

USE_TZ = True

CELERY_BROKER_URL = os.environ.get("CELERY_BROKER_URL", "")
CELERY_BROKER_URL = os.environ.get(
"CELERY_BROKER_URL", "amqp://guest:guest@localhost:5672/"
)

CELERY_TASK_ALWAYS_EAGER = CELERY_BROKER_URL == ""

Expand Down
52 changes: 29 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
version: '3.1'

services:
db:
postgres:
image: postgres:15
ports:
- "15432:5432"
- "${DOCKER_POSTGRES_PORT_FORWARD-5432}:5432"
volumes:
- ${DB_DATA_DIR-./.data/db}:/var/lib/postgresql/data
env_file:
- .env.docker
- ${PGDATA-./.data/db}:/var/lib/postgresql/data
environment:
POSTGRES_USER: "${POSTGRES_USER-postgres}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD-postgres}"
POSTGRES_DB: "${POSTGRES_DB-postgres}"

broker:
rabbitmq:
image: rabbitmq:3.11.9-management
env_file:
- .env.docker
environment:
RABBITMQ_DEFAULT_USER: "${RABBITMQ_DEFAULT_USER-guest}"
RABBITMQ_DEFAULT_PASS: "${RABBITMQ_DEFAULT_PASS-guest}"
RABBITMQ_DEFAULT_VHOST: "${RABBITMQ_DEFAULT_VHOST}"

web:
stdin_open: true
Expand All @@ -23,35 +27,37 @@ services:
volumes: &web-volumes
- ./:/code
ports:
- "8000:8000"
- "${DOCKER_WEB_PORT_FORWARD-8000}:8000"
depends_on: &web-depends-on
- db
- broker
env_file:
- .env.docker
- postgres
- rabbitmq
environment: &web-environment
POSTGRES_USER: "${POSTGRES_USER-postgres}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD-postgres}"
POSTGRES_DB: "${POSTGRES_DB-postgres}"
POSTGRES_HOST: "${POSTGRES_HOST-postgres}"
POSTGRES_PORT: "${POSTGRES_PORT-5432}"
CELERY_BROKER_URL: "${CELERY_BROKER_URL-amqp://guest:guest@rabbitmq:5672/}"

flower:
image: mher/flower
ports:
- "15555:5555"
- "${DOCKER_FLOWER_PORT_FORWARD-5555}:5555"
command: celery flower --broker_api=http://feeds:feeds@broker:15672/api/feeds
depends_on:
- broker
env_file:
- .env.docker
- rabbitmq
environment: *web-environment

celery-beat:
build: .
command: celery --app feeds beat --loglevel debug
command: celery --app demo beat --loglevel debug
volumes: *web-volumes
depends_on: *web-depends-on
env_file:
- .env.docker
environment: *web-environment

celery-worker:
build: .
command: celery --app feeds worker --loglevel debug
command: celery --app demo worker --loglevel debug
volumes: *web-volumes
depends_on: *web-depends-on
env_file:
- .env.docker
environment: *web-environment
Loading

0 comments on commit 4e83d6a

Please sign in to comment.