Skip to content

Commit 24dfa84

Browse files
committed
App dockerized
1 parent 764a788 commit 24dfa84

31 files changed

+165
-12
lines changed

.env.dev

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DB_HOST=db
2+
DB_NAME=django_images_api_project
3+
DB_USER=test
4+
DB_PASS=test
5+
POSTGRES_DB=django_images_api_project
6+
POSTGRES_USER=test
7+
POSTGRES_PASSWORD=test

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:alpine
2+
3+
ENV PYTHONUNBUFFERED 1
4+
COPY ./requirements.txt /requirements.txt
5+
RUN apk add --update --no-cache postgresql-client jpeg-dev
6+
RUN apk add --update --no-cache --virtual .tmp-build-deps \
7+
gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
8+
RUN pip install -r /requirements.txt
9+
RUN apk del .tmp-build-deps
10+
11+
RUN mkdir /app
12+
COPY ./app /app
13+
WORKDIR /app

app/accounts/__init__.py

Whitespace-only changes.
File renamed without changes.

app/accounts/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountsConfig(AppConfig):
5+
name = 'accounts'

app/accounts/management/__init__.py

Whitespace-only changes.

app/accounts/management/commands/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import time
2+
from django.db import connections
3+
from django.db.utils import OperationalError
4+
from django.core.management import BaseCommand
5+
6+
class Command(BaseCommand):
7+
"""Django command to pause execution until db is available"""
8+
9+
def handle(self, *args, **options):
10+
self.stdout.write('Waiting for database...')
11+
db_conn = None
12+
while not db_conn:
13+
try:
14+
db_conn = connections['default']
15+
except OperationalError:
16+
self.stdout.write('Database unavailable, waititng 1 second...')
17+
time.sleep(1)
18+
19+
self.stdout.write(self.style.SUCCESS('Database available!'))
File renamed without changes.

accounts/validators.py renamed to app/accounts/validators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ def compare(self, value, schema):
1010
except jsonschema.exceptions.ValidationError:
1111
raise ValidationError(
1212
'%(value)s failed JSON schema check', params={'value': value}
13-
)
13+
)

app/accounts/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

app/images_api/__init__.py

Whitespace-only changes.

app/images_api/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
File renamed without changes.
File renamed without changes.
File renamed without changes.

images_api/signals.py renamed to app/images_api/signals.py

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ def make_thumbnails(sender, instance, **kwargs):
2222
])
2323
instance._thumbnail_drafts = jobs.apply_async()
2424

25-
"""instance.owner.tier or instance.owner.tier.thumbnail_heights changed?"""
26-
2725

2826
@receiver(post_save, sender=UploadedImage)
2927
def initiate_thumbnails(sender, instance, created, **kwargs):
File renamed without changes.
File renamed without changes.
File renamed without changes.

images_api/views.py renamed to app/images_api/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from rest_framework import generics, permissions
2+
from rest_framework.response import Response
23
from .serializers import ImageSerializer
34
from .models import UploadedImage
45

56
class ImageListCreateAPIView(generics.ListCreateAPIView):
67
serializer_class = ImageSerializer
7-
permission_classes = [permissions.IsAuthenticated]
8+
permission_classes = (permissions.IsAuthenticated, )
89

910
def get_queryset(self):
1011
return UploadedImage.objects.filter(owner=self.request.user.useraccount)
File renamed without changes.

app/images_api_project/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for images_api_project project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'images_api_project.settings')
15+
16+
application = get_asgi_application()
File renamed without changes.

images_api_project/settings.py renamed to app/images_api_project/settings.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
For the full list of settings and their values, see
1010
https://docs.djangoproject.com/en/3.1/ref/settings/
1111
"""
12-
12+
import os
1313
from pathlib import Path
1414

1515
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -81,15 +81,13 @@
8181
DATABASES = {
8282
'default': {
8383
'ENGINE': 'django.db.backends.postgresql',
84-
'NAME': 'django_images_api_project',
85-
'USER': 'test',
86-
'PASSWORD': 'test',
87-
'HOST': 'localhost',
88-
'POST': '5432'
84+
'HOST': os.environ.get('DB_HOST'),
85+
'NAME': os.environ.get('DB_NAME'),
86+
'USER': os.environ.get('DB_USER'),
87+
'PASSWORD': os.environ.get('DB_PASS'),
8988
}
9089
}
9190

92-
9391
# Password validation
9492
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
9593

@@ -131,8 +129,14 @@
131129
MEDIA_ROOT = Path(BASE_DIR / 'media')
132130
MEDIA_URL = '/media/'
133131

132+
REST_FRAMEWORK = {
133+
'DEFAULT_AUTHENTICATION_CLASSES': [
134+
'rest_framework.authentication.BasicAuthentication',
135+
]
136+
}
137+
134138
# CELERY related settings
135-
REDIS_HOST = 'localhost'
139+
REDIS_HOST = 'redis'
136140
REDIS_PORT = '6379'
137141
CELERY_BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
138142
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
File renamed without changes.

app/images_api_project/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for images_api_project project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'images_api_project.settings')
15+
16+
application = get_wsgi_application()

app/manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'images_api_project.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

docker-compose.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: '3'
2+
3+
services:
4+
app:
5+
build:
6+
context: .
7+
ports:
8+
- "8000:8000"
9+
volumes:
10+
- ./app:/app
11+
command: >
12+
sh -c "python3 manage.py migrate &&
13+
python3 manage.py wait_for_db &&
14+
python3 manage.py runserver 0.0.0.0:8000"
15+
env_file:
16+
- ./.env.dev
17+
depends_on:
18+
- db
19+
db:
20+
image: postgres:10-alpine
21+
env_file:
22+
- ./.env.dev
23+
ports:
24+
- "5432:5432"
25+
redis:
26+
image: redis:alpine
27+
celery:
28+
restart: always
29+
build:
30+
context: .
31+
command: celery -A images_api_project worker -l info -P solo
32+
volumes:
33+
- ./app:/app
34+
env_file:
35+
- ./.env.dev
36+
depends_on:
37+
- db
38+
- redis
39+
- app

requirements.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
celery==5.2.7
2+
Django==3.1.7
3+
djangorestframework==3.12.4
4+
jsonschema==4.5.1
5+
Pillow==8.4.0
6+
psycopg2==2.9.1
7+
redis==4.3.3

0 commit comments

Comments
 (0)