Skip to content

Commit

Permalink
feat: opensearch and optimization for using production config
Browse files Browse the repository at this point in the history
and fuck you, elasticsearch
  • Loading branch information
hUwUtao committed May 25, 2024
1 parent 3bb7820 commit 55afca2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
6 changes: 6 additions & 0 deletions fuhoblog/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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
56 changes: 42 additions & 14 deletions fuhoblog/settings/production.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
wagtail_modeladmin==2.0
python-dotenv
mysqlclient
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export DJANGO_SETTINGS_MODULE="fuhoblog.settings.production"
rm -rv static
python manage.py collectstatic
python manage.py migrate --noinput
Expand Down

0 comments on commit 55afca2

Please sign in to comment.