搞定docker所需的通过环境变量控制
This commit is contained in:
@@ -22,12 +22,12 @@ BASE_DIR = Path(__file__).resolve().parent.parent
|
|||||||
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
|
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
|
||||||
|
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = 'django-insecure-p^*6tak7wy1z#bw__#o^s5hsydearm=(-s(km!-61j2(#)*+-t'
|
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'django-insecure-p^*6tak7wy1z#bw__#o^s5hsydearm=(-s(km!-61j2(#)*+-t')
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
DEBUG = os.environ.get('DJANGO_DEBUG', 'True').lower() == 'true'
|
||||||
|
|
||||||
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
|
ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '127.0.0.1,localhost').split(',')
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
@@ -47,6 +47,7 @@ INSTALLED_APPS = [
|
|||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
@@ -121,6 +122,7 @@ USE_TZ = True
|
|||||||
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = 'static/'
|
STATIC_URL = 'static/'
|
||||||
|
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
||||||
|
|
||||||
# Media files (uploaded images)
|
# Media files (uploaded images)
|
||||||
MEDIA_URL = '/media/'
|
MEDIA_URL = '/media/'
|
||||||
@@ -134,6 +136,8 @@ SESSION_COOKIE_SECURE = False if DEBUG else True
|
|||||||
CSRF_COOKIE_SECURE = False if DEBUG else True
|
CSRF_COOKIE_SECURE = False if DEBUG else True
|
||||||
CSRF_COOKIE_SAMESITE = 'Lax'
|
CSRF_COOKIE_SAMESITE = 'Lax'
|
||||||
|
|
||||||
|
CSRF_TRUSTED_ORIGINS = os.environ.get('DJANGO_CSRF_TRUSTED_ORIGINS', '').split(',') if os.environ.get('DJANGO_CSRF_TRUSTED_ORIGINS') else []
|
||||||
|
|
||||||
X_FRAME_OPTIONS = 'DENY'
|
X_FRAME_OPTIONS = 'DENY'
|
||||||
|
|
||||||
# Default primary key field type
|
# Default primary key field type
|
||||||
@@ -142,9 +146,12 @@ X_FRAME_OPTIONS = 'DENY'
|
|||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
# Elasticsearch configuration
|
# Elasticsearch configuration
|
||||||
|
_ES_URL = os.environ.get('ELASTICSEARCH_URL', 'http://localhost:9200')
|
||||||
|
if not (_ES_URL.startswith('http://') or _ES_URL.startswith('https://')):
|
||||||
|
_ES_URL = 'http://' + _ES_URL
|
||||||
ELASTICSEARCH_DSL = {
|
ELASTICSEARCH_DSL = {
|
||||||
'default': {
|
'default': {
|
||||||
'hosts': 'localhost:9200'
|
'hosts': _ES_URL
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
Dockerfile
Normal file
21
Dockerfile
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
build-essential \
|
||||||
|
libjpeg62-turbo-dev \
|
||||||
|
zlib1g-dev \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY requirements.txt /app/
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY . /app
|
||||||
|
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
CMD ["sh","-c","python manage.py migrate && python manage.py collectstatic --noinput && gunicorn Achievement_Inputing.wsgi:application --bind 0.0.0.0:8000 --workers 3"]
|
||||||
@@ -6,7 +6,7 @@ import hmac
|
|||||||
from django.http import JsonResponse, HttpResponseBadRequest
|
from django.http import JsonResponse, HttpResponseBadRequest
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.views.decorators.http import require_http_methods
|
from django.views.decorators.http import require_http_methods
|
||||||
from django.views.decorators.csrf import csrf_protect
|
from django.views.decorators.csrf import csrf_protect, ensure_csrf_cookie
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from .es_client import get_user_by_username
|
from .es_client import get_user_by_username
|
||||||
@@ -14,6 +14,7 @@ from .crypto import salt_for_username, hmac_sha256
|
|||||||
|
|
||||||
|
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
|
@ensure_csrf_cookie
|
||||||
def login_page(request):
|
def login_page(request):
|
||||||
return render(request, "accounts/login.html")
|
return render(request, "accounts/login.html")
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ Django版本的ES连接和操作模块
|
|||||||
"""
|
"""
|
||||||
from elasticsearch import Elasticsearch
|
from elasticsearch import Elasticsearch
|
||||||
from elasticsearch_dsl import connections
|
from elasticsearch_dsl import connections
|
||||||
|
import os
|
||||||
from .documents import AchievementDocument, UserDocument, GlobalDocument
|
from .documents import AchievementDocument, UserDocument, GlobalDocument
|
||||||
from .indexes import ACHIEVEMENT_INDEX_NAME, USER_INDEX_NAME, GLOBAL_INDEX_NAME
|
from .indexes import ACHIEVEMENT_INDEX_NAME, USER_INDEX_NAME, GLOBAL_INDEX_NAME
|
||||||
import hashlib
|
import hashlib
|
||||||
@@ -11,8 +12,11 @@ import time
|
|||||||
from datetime import datetime, timezone, timedelta
|
from datetime import datetime, timezone, timedelta
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
# 使用Django的ES连接配置
|
# 使用环境变量配置ES连接,默认为本机
|
||||||
connections.create_connection(hosts=['localhost:9200'])
|
_ES_URL = os.environ.get('ELASTICSEARCH_URL', 'http://localhost:9200')
|
||||||
|
if not (_ES_URL.startswith('http://') or _ES_URL.startswith('https://')):
|
||||||
|
_ES_URL = 'http://' + _ES_URL
|
||||||
|
connections.create_connection(hosts=[_ES_URL])
|
||||||
|
|
||||||
# 获取默认的ES客户端
|
# 获取默认的ES客户端
|
||||||
es = connections.get_connection()
|
es = connections.get_connection()
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
|
--index-url https://pypi.tuna.tsinghua.edu.cn/simple
|
||||||
|
--extra-index-url https://pypi.org/simple
|
||||||
Django==5.2.8
|
Django==5.2.8
|
||||||
elasticsearch==8.17.1
|
elasticsearch==7.17.9
|
||||||
django-elasticsearch-dsl==7.3.0
|
django-elasticsearch-dsl==7.4.0
|
||||||
|
django-elasticsearch-dsl-drf==0.22
|
||||||
|
elasticsearch-dsl==7.4.1
|
||||||
requests==2.32.3
|
requests==2.32.3
|
||||||
openai==1.52.2
|
openai==1.52.2
|
||||||
Pillow==10.4.0
|
Pillow==10.4.0
|
||||||
|
gunicorn==21.2.0
|
||||||
|
whitenoise==6.6.0
|
||||||
|
|||||||
Reference in New Issue
Block a user