diff --git a/.env.example b/.env.example index 9f3dd381..509331ec 100644 --- a/.env.example +++ b/.env.example @@ -1,18 +1,26 @@ # SECURITY WARNING: keep the secret key used in production secret! -export SECRET_KEY="7(2w1sedok=aznpq)ta1mc4i%4h=xx@hxwx*o57ctsuml0x%fr" +SECRET_KEY=7(2w1sedok=aznpq)ta1mc4i%4h=xx@hxwx*o57ctsuml0x%fr # SECURITY WARNING: don't run with debug turned on in production! -export DEBUG=true +DEBUG=true -export DOMAIN=your.domain.here +DOMAIN=your.domain.here ## Leave unset to allow all hosts -# export ALLOWED_HOSTS="localhost,127.0.0.1,[::1]" +# ALLOWED_HOSTS="localhost,127.0.0.1,[::1]" -export OL_URL="https://openlibrary.org" +OL_URL=https://openlibrary.org ## Database backend to use. ## Default is postgres, sqlite is for dev quickstart only (NOT production!!!) -export FEDIREADS_DATABASE_BACKEND=postgres +FEDIREADS_DATABASE_BACKEND=postgres -export MEDIA_ROOT="images/" \ No newline at end of file +MEDIA_ROOT=images/ + +POSTGRES_PASSWORD=fedireads +POSTGRES_USER=fedireads +POSTGRES_DB=fedireads + +RABBITMQ_DEFAULT_USER=rabbit +RABBITMQ_DEFAULT_PASS=changeme +CELERY_BROKER=amqp://rabbit:changeme@rabbitmq:5672 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c845035f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 +ENV PYTHONUNBUFFERED 1 +RUN mkdir /app +WORKDIR /app +COPY requirements.txt /app/ +RUN pip install -r requirements.txt +COPY ./fedireads /app diff --git a/README.md b/README.md index e63912df..1fa8cd3e 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,17 @@ And go to the app at `localhost:8000` For most testing, you'll want to use ngrok. Remember to set the DOMAIN in `.env` to your ngrok domain. +### Docker +You can also run the application in a docker container. You'll have to install the Docker and docker-compose: + +```bash +docker-compose build +docker-compose up +docker-compose exec web python manage.py migrate +docker-compose exec web python manage.py shell -c 'import init_db' +``` + + ## Project structure All the url routing is in `fedireads/urls.py`. This includes the application views (your home page, user page, book page, etc), application endpoints (things that happen when you click buttons), and federation api endpoints (inboxes, outboxes, webfinger, etc). diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..a119f25d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,46 @@ +version: '3' + +services: + db: + image: postgres + env_file: .env + volumes: + - pgdata:/var/lib/postgresql/data + networks: + - main + web: + build: . + command: python manage.py runserver 0.0.0.0:8000 + volumes: + - .:/app + ports: + - "8000:8000" + depends_on: + - db + - celery_worker + networks: + - main + rabbitmq: + env_file: .env + image: rabbitmq:latest + networks: + - main + ports: + - "5672:5672" + restart: on-failure + celery_worker: + env_file: .env + build: . + networks: + - main + command: celery -A fedireads worker -l info + volumes: + - .:/app + depends_on: + - db + - rabbitmq + restart: on-failure +volumes: + pgdata: +networks: + main: diff --git a/fedireads/__init__.py b/fedireads/__init__.py index e69de29b..b23e378a 100644 --- a/fedireads/__init__.py +++ b/fedireads/__init__.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import, unicode_literals + +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ('celery_app',) + diff --git a/fedireads/celery.py b/fedireads/celery.py new file mode 100644 index 00000000..7c26dc06 --- /dev/null +++ b/fedireads/celery.py @@ -0,0 +1,24 @@ +from __future__ import absolute_import, unicode_literals + +import os + +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fedireads.settings') + +app = Celery('fedireads') + +# Using a string here means the worker doesn't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object('django.conf:settings', namespace='CELERY') + +# Load task modules from all registered Django app configs. +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) diff --git a/fedireads/settings.py b/fedireads/settings.py index c5cdfd9b..c4ce0b2c 100644 --- a/fedireads/settings.py +++ b/fedireads/settings.py @@ -17,11 +17,16 @@ SECRET_KEY = env('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = env.bool('DEBUG', True) -# TODO: annoying that I keep changing and re-commiting this DOMAIN = env('DOMAIN') ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['*']) OL_URL = env('OL_URL') +# celery/rebbitmq +CELERY_BROKER_URL = env('CELERY_BROKER') +CELERY_ACCEPT_CONTENT = ['json'] +CELERY_TASK_SERIALIZER = 'json' +CELERY_RESULT_BACKEND = 'amqp' + # Application definition INSTALLED_APPS = [ @@ -33,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'django.contrib.humanize', 'fedireads', + 'celery', ] MIDDLEWARE = [ @@ -75,10 +81,10 @@ FEDIREADS_DATABASE_BACKEND = env('FEDIREADS_DATABASE_BACKEND', 'postgres') FEDIREADS_DBS = { 'postgres': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'fedireads', - 'USER': 'fedireads', - 'PASSWORD': 'fedireads', - 'HOST': '', + 'NAME': env('POSTGRES_DB', 'fedireads'), + 'USER': env('POSTGRES_USER', 'fedireads'), + 'PASSWORD': env('POSTGRES_PASSWORD', 'fedireads'), + 'HOST': 'db', 'PORT': 5432 }, 'sqlite': { @@ -91,6 +97,7 @@ DATABASES = { 'default': FEDIREADS_DBS[FEDIREADS_DATABASE_BACKEND] } + LOGIN_URL = '/login/' AUTH_USER_MODEL = 'fedireads.User' diff --git a/requirements.txt b/requirements.txt index 1320435c..0375a27d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +celery==4.4.2 Django==3.0.3 django-model-utils==4.0.0 environs==7.2.0