From 259cb195cbf0cd44a07dd8d156bc57dae8864cd3 Mon Sep 17 00:00:00 2001 From: rkun123 Date: Sat, 3 Jul 2021 15:55:45 +0000 Subject: [PATCH] [update] docker-compose --- .gitignore | 1 + Dockerfile | 15 +++++ Pipfile | 5 +- Pipfile.lock | 38 +++++++++--- docker-compose.yml | 28 +++++++++ server/server/settings/base.py | 26 ++++++++ server/server/settings/docker_development.py | 62 ++++++++++++++++++++ server/server/settings/docker_production.py | 62 ++++++++++++++++++++ server/works/serializer.py | 10 +++- 9 files changed, 236 insertions(+), 11 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 server/server/settings/docker_development.py create mode 100644 server/server/settings/docker_production.py diff --git a/.gitignore b/.gitignore index af469bf..5cfcce2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ db.sqlite3 local_settings.py __pycache__/ /server/media/images/system/* +/server/staticfiles diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9d01c7a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.9.6-alpine + +RUN mkdir /app + +WORKDIR /app + +COPY . /app + +RUN apk add gcc musl-dev g++ zlib-dev jpeg-dev libjpeg postgresql-libs postgresql-dev + +RUN pip install pipenv + +RUN pipenv install + +RUN pip install uvicorn \ No newline at end of file diff --git a/Pipfile b/Pipfile index e0b23e4..7774637 100644 --- a/Pipfile +++ b/Pipfile @@ -4,7 +4,7 @@ verify_ssl = true name = "pypi" [packages] -asgiref = "==3.2.10" +asgiref = "==3.4.1" astroid = "==2.4.2" boto3 = "==1.14.20" botocore = "==1.17.20" @@ -43,11 +43,12 @@ toml = "==0.10.1" urllib3 = "==1.25.9" whitenoise = "==5.1.0" wrapt = "==1.12.1" -Django = "==3.0.7" +Django = "==3.2.5" Pillow = "==7.2.0" djangorestframework = "*" django-cors-headers = "*" drf-nested-routers = "*" +uvicorn = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 40e0100..abc7337 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "e6362390568d6dc89e117922d27b89cd4b56c4cce23ce684d039ede8e1128a32" + "sha256": "84474f86c1d629c31fb884f632c8448842e687e1c8e167c86ab2a5d09cd632e6" }, "pipfile-spec": 6, "requires": { @@ -18,11 +18,11 @@ "default": { "asgiref": { "hashes": [ - "sha256:7e51911ee147dd685c3c8b805c0ad0cb58d360987b56953878f8c06d2d1c6f1a", - "sha256:9fc6fb5d39b8af147ba40765234fa822b39818b12cc80b35ad9b0cef3a476aed" + "sha256:4ef1ab46b484e3c706329cedeff284a5d40824200638503f5768edb6de7d58e9", + "sha256:ffc141aa908e6f175673e7b1b3b7af4fdb0ecb738fc5c8b88f69f055c2415214" ], "index": "pypi", - "version": "==3.2.10" + "version": "==3.4.1" }, "astroid": { "hashes": [ @@ -64,6 +64,14 @@ "index": "pypi", "version": "==3.0.4" }, + "click": { + "hashes": [ + "sha256:8c04c11192119b1ef78ea049e0a6f0463e4c48ef00a30160c704337586f3ad7a", + "sha256:fba402a4a47334742d782209a7c79bc448911afe1149d07bdabdf480b3e2f4b6" + ], + "markers": "python_version >= '3.6'", + "version": "==8.0.1" + }, "cloudinary": { "hashes": [ "sha256:07974152945bdb5b03c35c0ee52e1ca5a6c615de592b458a1b866fef1e48dc2b" @@ -89,11 +97,11 @@ }, "django": { "hashes": [ - "sha256:5052b34b34b3425233c682e0e11d658fd6efd587d11335a0203d827224ada8f2", - "sha256:e1630333248c9b3d4e38f02093a26f1e07b271ca896d73097457996e0fae12e8" + "sha256:3da05fea54fdec2315b54a563d5b59f3b4e2b1e69c3a5841dda35019c01855cd", + "sha256:c58b5f19c5ae0afe6d75cbdd7df561e6eb929339985dbbda2565e1cabb19a62e" ], "index": "pypi", - "version": "==3.0.7" + "version": "==3.2.5" }, "django-appconf": { "hashes": [ @@ -191,6 +199,14 @@ "index": "pypi", "version": "==20.0.4" }, + "h11": { + "hashes": [ + "sha256:36a3cb8c0a032f56e2da7084577878a035d3b61d104230d4bd49c0c6b555a9c6", + "sha256:47222cb6067e4a307d535814917cd98fd0a57b6788ce715755fa2b6c28b56042" + ], + "markers": "python_version >= '3.6'", + "version": "==0.12.0" + }, "idna": { "hashes": [ "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6", @@ -445,6 +461,14 @@ "index": "pypi", "version": "==1.25.9" }, + "uvicorn": { + "hashes": [ + "sha256:2a76bb359171a504b3d1c853409af3adbfa5cef374a4a59e5881945a97a93eae", + "sha256:45ad7dfaaa7d55cab4cd1e85e03f27e9d60bc067ddc59db52a2b0aeca8870292" + ], + "index": "pypi", + "version": "==0.14.0" + }, "whitenoise": { "hashes": [ "sha256:60154b976a13901414a25b0273a841145f77eb34a141f9ae032a0ace3e4d5b27", diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2b918a7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +version: '3' +services: + api: + build: . + working_dir: /app/server + command: ['pipenv', 'run', 'gunicorn', 'server.asgi:application', '-k', 'uvicorn.workers.UvicornWorker', '-b', '0.0.0.0:8000', '--capture-output'] + volumes: + - .:/app + depends_on: + - db + environment: + - DATABASE_URL=postgres://root:root@db:5432/api + - DJANGO_SETTINGS_MODULE=server.settings.docker_production + - SECRET_KEY=hogefuga + - LANG=ja_JP.utf8 + - LC_ALL=ja_JP.utf8 + ports: + - "8000:8000" + db: + image: postgres + volumes: + - ./postgres/init:/docker-entrypoint-initdb.d + - ./postgres/data:/var/lib/postgresql/data + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: root + POSTGRES_DB: api + POSTGRES_INITDB_ARGS: "--encoding=UTF-8" \ No newline at end of file diff --git a/server/server/settings/base.py b/server/server/settings/base.py index 96ebba5..7b41f79 100644 --- a/server/server/settings/base.py +++ b/server/server/settings/base.py @@ -69,6 +69,32 @@ }, ] +LOGGING={ + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'production': { + 'format': '%(asctime)s [%(levelname)s] %(process)d %(thread)d ' + '%(pathname)s:%(lineno)d %(message)s' + }, + }, + 'handlers': { + 'file': { + 'class': 'logging.FileHandler', + 'filename': 'logs/django.log', + 'formatter': 'production', + 'level': 'INFO' + } + }, + 'loggers': { + 'django': { + 'handlers': ['file'], + 'level': 'INFO', + 'propagate': False + } + } +} + WSGI_APPLICATION = 'server.wsgi.application' diff --git a/server/server/settings/docker_development.py b/server/server/settings/docker_development.py new file mode 100644 index 0000000..d5d4769 --- /dev/null +++ b/server/server/settings/docker_development.py @@ -0,0 +1,62 @@ +""" +Django settings for server project. + +Generated by 'django-admin startproject' using Django 3.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os +import dj_database_url +from .base import * + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ['SECRET_KEY'] + +# SECURITY WARNING: don't run with debug turned on in production! +if os.getenv("DEBUG", "False") == "False": + DEBUG = False + +ALLOWED_HOSTS = ["*"] + + +# Application definition + + +ROOT_URLCONF = 'server.urls' + +WSGI_APPLICATION = 'server.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': dj_database_url.config() +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] diff --git a/server/server/settings/docker_production.py b/server/server/settings/docker_production.py new file mode 100644 index 0000000..d5d4769 --- /dev/null +++ b/server/server/settings/docker_production.py @@ -0,0 +1,62 @@ +""" +Django settings for server project. + +Generated by 'django-admin startproject' using Django 3.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os +import dj_database_url +from .base import * + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ['SECRET_KEY'] + +# SECURITY WARNING: don't run with debug turned on in production! +if os.getenv("DEBUG", "False") == "False": + DEBUG = False + +ALLOWED_HOSTS = ["*"] + + +# Application definition + + +ROOT_URLCONF = 'server.urls' + +WSGI_APPLICATION = 'server.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': dj_database_url.config() +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] diff --git a/server/works/serializer.py b/server/works/serializer.py index 4281640..a82441c 100644 --- a/server/works/serializer.py +++ b/server/works/serializer.py @@ -2,7 +2,7 @@ from django.core import serializers from django.db.models.base import Model from rest_framework import serializers -from .models import Genre, Work, Comment +from .models import Genre, Tag, Work, Comment from datetime import datetime class GenreSerializer(serializers.ModelSerializer): @@ -12,12 +12,18 @@ class Meta: fields = ['id', 'works', 'title', 'bg_color'] depth = 2 +class TagSerializer(serializers.ModelSerializer): + + class Meta: + model = Tag + fields = ['id', 'name', 'color'] + class WorkSerializer(serializers.ModelSerializer): # genre = GenreSerializer() class Meta: model = Work - fields=['id', 'title', 'description', 'genre', 'team', 'type_choice', 'goods', 'comments'] + fields=['id', 'title', 'description', 'genre', 'team', 'type_choice', 'goods', 'comments', 'tags'] depth = 1 class CommentSerializer(serializers.ModelSerializer):