From 55afca2074a72b3a4f28526b9b97dc40afa22be5 Mon Sep 17 00:00:00 2001 From: stdpi Date: Sat, 25 May 2024 18:01:23 +0700 Subject: [PATCH] feat: opensearch and optimization for using production config and fuck you, elasticsearch --- fuhoblog/settings/base.py | 6 ++++ fuhoblog/settings/production.py | 56 ++++++++++++++++++++++++--------- requirements.txt | 6 ++-- run.sh | 1 + 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/fuhoblog/settings/base.py b/fuhoblog/settings/base.py index 5a9e8e3..3fbf3c5 100644 --- a/fuhoblog/settings/base.py +++ b/fuhoblog/settings/base.py @@ -12,10 +12,14 @@ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os +import dotenv + +dotenv.load_dotenv() PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(PROJECT_DIR) +SECRET_KEY = os.environ["SECRET_KEY"] or "" # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ @@ -187,3 +191,5 @@ # Base URL to use when referring to full URLs within the Wagtail admin backend - # e.g. in notification emails. Don't include '/admin' or a trailing slash WAGTAILADMIN_BASE_URL = 'http://example.com' + +CSRF_COOKIE_SECURE = True \ No newline at end of file diff --git a/fuhoblog/settings/production.py b/fuhoblog/settings/production.py index 6b87a29..d3043af 100644 --- a/fuhoblog/settings/production.py +++ b/fuhoblog/settings/production.py @@ -1,26 +1,54 @@ from .base import * +import os +from urllib.parse import urlparse + DEBUG = False -ENV_KEYS = ['DB_NAME', 'DB_HOST', 'DB_PORT', 'DB_PASS', 'DB_USER'] -ENV_VARS = list(map(os.environ.get, ENV_KEYS)) +if SECRET_KEY == '': + print('no SECRET_KEY env') + exit(1) DATABASES = DATABASES +WAGTAILSEARCH_BACKENDS = WAGTAILSEARCH_BACKENDS + +if os.environ.get('DATABASE') is not None: + try: + DATABASE_ENVIRON = urlparse(os.environ['DASTABASE']) + if DATABASE_ENVIRON.scheme == 'mysql': + DATABASES |= { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': os.environ['MYSQL_DATABASE'], + 'USER': os.environ['MYSQL_PASSWORD'], + 'PASSWORD': os.environ['MYSQL_PASSWORD'], + 'HOST': os.environ['MYSQL_HOST'], + 'PORT': os.environ['MYSQL_PORT'], + }, + } + else: + print('DATABASE environment unknown scheme', DATABASE_ENVIRON.scheme) + except ValueError: + print('DATABASE environment variable invalid') +else: + print('Did not tried using DATABASE despite tried to load PRODUCTION config') -if all( - not k.startswith('DB_') or ENV_VARS[i] is not None for i, k in enumerate(ENV_KEYS) -): - print('Using MYSQL instead') - DATABASES = { + +if os.environ['OPENSEARCH_HOST'] is not None: + WAGTAILSEARCH_BACKENDS = { 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': ENV_VARS[0], - 'USER': ENV_VARS[4], - 'PASSWORD': ENV_VARS[3], - 'HOST': ENV_VARS[1], - 'PORT': ENV_VARS[2], - }, + 'BACKEND': 'wagtail.search.backends.elasticsearch7', + 'URLS': [os.environ['OPENSEARCH_HOST']], + 'INDEX': 'blog', + 'TIMEOUT': 5, + 'OPTIONS': {}, + 'INDEX_SETTINGS': {}, + } } +else: + print('Did not tried using OPENSEARCH despite tried to load PRODUCTION config') + +ALLOWED_HOSTS = ['*'] try: from .local import * diff --git a/requirements.txt b/requirements.txt index c7c3f76..2a1d8d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,8 @@ Django>=4.2,<4.3 wagtail>=6,<7 -# wagtail>=5.0,<5.1 gunicorn==22 +elasticsearch==7.13.4 wagtail-localize==1.9 -wagtail_modeladmin==2.0 \ No newline at end of file +wagtail_modeladmin==2.0 +python-dotenv +mysqlclient \ No newline at end of file diff --git a/run.sh b/run.sh index e6294ef..61ce3c8 100644 --- a/run.sh +++ b/run.sh @@ -1,3 +1,4 @@ +export DJANGO_SETTINGS_MODULE="fuhoblog.settings.production" rm -rv static python manage.py collectstatic python manage.py migrate --noinput