소스 검색

add docker boilerplate

subDesTagesMitExtraKaese 1 년 전
부모
커밋
d0b0b470c2
17개의 변경된 파일159개의 추가작업 그리고 31개의 파일을 삭제
  1. 1 0
      .dockerignore
  2. 20 0
      Dockerfile
  3. 5 0
      development.sh
  4. 34 0
      docker-compose.yml
  5. 32 0
      locale/de/LC_MESSAGES/django.po
  6. 1 1
      manage.py
  7. 5 0
      production.sh
  8. 0 0
      project/__init__.py
  9. 0 0
      project/asgi.py
  10. 33 5
      project/settings.py
  11. 0 0
      project/urls.py
  12. 0 0
      project/wsgi.py
  13. 2 0
      requirements.txt
  14. 2 2
      zitap/forms.py
  15. 10 9
      zitap/static/zitap/css/date-picker.css
  16. 3 3
      zitap/urls.py
  17. 11 11
      zitap/views.py

+ 1 - 0
.dockerignore

@@ -0,0 +1 @@
+data/

+ 20 - 0
Dockerfile

@@ -0,0 +1,20 @@
+# syntax=docker/dockerfile:1
+FROM python:3-slim-bullseye
+ENV PYTHONDONTWRITEBYTECODE=1
+ENV PYTHONUNBUFFERED=1
+WORKDIR /app
+
+RUN apt-get update && \
+  apt-get install -y libmariadb-dev gcc
+
+COPY ./requirements.txt /app/
+
+ARG CACHEBUST
+
+RUN pip install -r requirements.txt && apt-get purge -y libmariadb-dev gcc
+
+COPY . /app/
+
+EXPOSE 8000
+
+CMD ["/app/production.sh"]

+ 5 - 0
development.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+
+python3 manage.py makemessages --ignore="static" --ignore=".env"  -l de
+python3 manage.py compilemessages
+python3 manage.py runserver 0.0.0.0:8001

+ 34 - 0
docker-compose.yml

@@ -0,0 +1,34 @@
+version: "3.7"
+   
+services:
+  zitap:
+    container_name: django
+
+    build: .
+    
+    restart: on-failure
+
+    environment:
+      DJANGO_SECRET_KEY: <SECRET_KEY>
+      DJANGO_DEBUG: "False"
+      DJANGO_ALLOWED_HOSTS: <IP>,<DOMAIN>,localhost
+      DJANGO_CSRF_TRUSTED_ORIGINS: <DOMAIN>
+
+      DATABASE_ENGINE: mysql
+      DATABASE_NAME: django
+      DATABASE_USERNAME: <USERNAME>
+      DATABASE_PASSWORD: <PASSWORD>
+      DATABASE_HOST: <IP>
+      DATABASE_PORT: 3306
+      DATABASE_OPTIONS: "{\"init_command\": \"SET sql_mode='STRICT_TRANS_TABLES'\"}"
+
+    volumes:
+      - ./project:/app/project
+      - ./zitap:/app/zitap
+      - ./static:/static
+      - ./data:/data
+
+    ports:
+      - 8000:8000
+
+    command: ["/code/production.sh"]

+ 32 - 0
locale/de/LC_MESSAGES/django.po

@@ -0,0 +1,32 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2022-12-15 18:55+0000\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: zitap/templates/zitap/create-event.html:5
+#: zitap/templates/zitap/index.html:23
+msgid "Create Event"
+msgstr ""
+
+#: zitap/templates/zitap/index.html:16
+msgid "Home"
+msgstr ""
+
+#: zitap/templates/zitap/index.html:17
+msgid "About"
+msgstr ""

+ 1 - 1
manage.py

@@ -6,7 +6,7 @@ import sys
 
 def main():
     """Run administrative tasks."""
-    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
+    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
     try:
         from django.core.management import execute_from_command_line
     except ImportError as exc:

+ 5 - 0
production.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+python3 manage.py migrate
+python3 manage.py collectstatic --noinput
+chown -R 101:101 /static
+gunicorn --bind :8000 --workers 2 website.wsgi:application --timeout 120

+ 0 - 0
app/__init__.py → project/__init__.py


+ 0 - 0
app/asgi.py → project/asgi.py


+ 33 - 5
app/settings.py → project/settings.py

@@ -10,6 +10,9 @@ For the full list of settings and their values, see
 https://docs.djangoproject.com/en/4.0/ref/settings/
 """
 
+import os
+import json
+import socket
 from pathlib import Path
 
 # Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -20,13 +23,17 @@ BASE_DIR = Path(__file__).resolve().parent.parent
 # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
 
 # SECURITY WARNING: keep the secret key used in production secret!
-SECRET_KEY = 'django-insecure-xdx9^5o*$-z$qh31^t$*gj8gs2y6ljnbt_c$v+rib5dle1q&&6'
+SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'debug_secret_key')
 
 # SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = True
+DEBUG = os.getenv('DJANGO_DEBUG', "True") == "True"
 
-ALLOWED_HOSTS = []
+if DEBUG:
+  hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
+  INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + ["127.0.0.1", "localhost"]
 
+ALLOWED_HOSTS = os.getenv('DJANGO_ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',')
+CSRF_TRUSTED_ORIGINS = os.getenv('DJANGO_CSRF_TRUSTED_ORIGINS', 'http://localhost').split(',')
 
 # Application definition
 
@@ -50,7 +57,9 @@ MIDDLEWARE = [
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]
 
-ROOT_URLCONF = 'app.urls'
+LOCALE_PATHS = ( os.path.join(BASE_DIR, 'locale'), )
+
+ROOT_URLCONF = 'project.urls'
 
 TEMPLATES = [
     {
@@ -68,7 +77,7 @@ TEMPLATES = [
     },
 ]
 
-WSGI_APPLICATION = 'app.wsgi.application'
+WSGI_APPLICATION = 'project.wsgi.application'
 
 
 # Database
@@ -80,6 +89,19 @@ DATABASES = {
         'NAME': BASE_DIR / 'db.sqlite3',
     }
 }
+DATABASES = {
+    'default': {
+      'ENGINE': os.getenv('DATABASE_ENGINE', 'django.db.backends.sqlite3'),
+      'NAME': os.getenv('DATABASE_NAME', 'db.sqlite3'),
+      'USER': os.getenv('DATABASE_USERNAME', 'myprojectuser'),
+      'PASSWORD': os.getenv('DATABASE_PASSWORD', 'password'),
+      'HOST': os.getenv('DATABASE_HOST', '127.0.0.1'),
+      'PORT': os.getenv('DATABASE_PORT', 3306),
+      'OPTIONS': json.loads(
+          os.getenv('DATABASE_OPTIONS', '{}')
+      ),
+    }
+}
 
 
 # Password validation
@@ -110,6 +132,8 @@ TIME_ZONE = 'UTC'
 
 USE_I18N = True
 
+USE_L10N = True
+
 USE_TZ = True
 
 
@@ -118,7 +142,11 @@ USE_TZ = True
 
 STATIC_URL = 'static/'
 
+STATIC_ROOT = '../static'
+
 # Default primary key field type
 # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
 
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
+
+MEDIA_ROOT = os.path.join(BASE_DIR, '../data')

+ 0 - 0
app/urls.py → project/urls.py


+ 0 - 0
app/wsgi.py → project/wsgi.py


+ 2 - 0
requirements.txt

@@ -1 +1,3 @@
 django
+mysqlclient
+gunicorn

+ 2 - 2
zitap/forms.py

@@ -5,5 +5,5 @@ from django import forms
 from .widgets import DatePickerWidget
 
 class CreateEventForm(forms.Form):
-  event_name = forms.CharField(label='Event name', max_length=100)
-  event_date = forms.Field(label='Event date', widget=DatePickerWidget)
+    event_name = forms.CharField(label='Event name', max_length=100)
+    event_date = forms.Field(label='Event date', widget=DatePickerWidget)

+ 10 - 9
zitap/static/zitap/css/date-picker.css

@@ -17,21 +17,21 @@
     padding: 3px;
 }
 .date-picker .day.hidden {
-  visibility: hidden;
+    visibility: hidden;
 }
 .date-picker .day.today {
-  font-weight: bold;
+    font-weight: bold;
 }
 .date-picker .day {
-  border: 1px solid #000;
-  background-color: rgb(240, 223, 223);
-  transition: all .5s ease;
-  cursor: pointer;
-  position: relative;
+    border: 1px solid #000;
+    background-color: rgb(240, 223, 223);
+    transition: all .5s ease;
+    cursor: pointer;
+    position: relative;
 }
 .date-picker .day input[type=checkbox] {
-  visibility: hidden;
-  position: absolute;
+    visibility: hidden;
+    position: absolute;
 }
 .date-picker .day-number {
     font-size: 10pt;
@@ -42,6 +42,7 @@
     display: grid;
     align-items: center;
     justify-items: center;
+    cursor: pointer;
 }
 .date-picker .day:hover {
     background-color: rgb(255, 255, 255);

+ 3 - 3
zitap/urls.py

@@ -3,7 +3,7 @@ from django.urls import path, include
 from . import views
 
 urlpatterns = [
-  path('', views.index, name='index'),
-  path('about', views.about, name='about'),
-  path('create-event', views.create_event, name='create-event'),
+    path('', views.index, name='index'),
+    path('about', views.about, name='about'),
+    path('create-event', views.create_event, name='create-event'),
 ]

+ 11 - 11
zitap/views.py

@@ -4,18 +4,18 @@ from django.shortcuts import render
 from .forms import CreateEventForm
 
 def index(request):
-  return render(request, 'zitap/index.html')
+    return render(request, 'zitap/index.html')
 
 def about(request):
-  return render(request, 'zitap/about.html')
+    return render(request, 'zitap/about.html')
 
 def create_event(request):
-  if request.method == 'POST':
-    form = CreateEventForm(request.POST)
-    if form.is_valid():
-      data = form.cleaned_data
-      print(data)
-      #return HttpResponseRedirect('/edit-event')
-  else:
-    form = CreateEventForm()
-  return render(request, 'zitap/create-event.html', {'form': form})
+    if request.method == 'POST':
+        form = CreateEventForm(request.POST)
+        if form.is_valid():
+            data = form.cleaned_data
+            print(data)
+            #return HttpResponseRedirect('/edit-event')
+    else:
+        form = CreateEventForm()
+    return render(request, 'zitap/create-event.html', {'form': form})