Merge pull request #2746 from WesleyAC/env-var-improvements

Env var improvements
This commit is contained in:
Mouse Reeve 2023-03-20 14:04:11 -07:00 committed by GitHub
commit 018cadf0cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 15 deletions

View file

@ -4,6 +4,7 @@ from environs import Env
import requests import requests
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ImproperlyConfigured
# pylint: disable=line-too-long # pylint: disable=line-too-long
@ -18,7 +19,7 @@ RELEASE_API = env(
"https://api.github.com/repos/bookwyrm-social/bookwyrm/releases/latest", "https://api.github.com/repos/bookwyrm-social/bookwyrm/releases/latest",
) )
PAGE_LENGTH = env("PAGE_LENGTH", 15) PAGE_LENGTH = env.int("PAGE_LENGTH", 15)
DEFAULT_LANGUAGE = env("DEFAULT_LANGUAGE", "English") DEFAULT_LANGUAGE = env("DEFAULT_LANGUAGE", "English")
JS_CACHE = "a7d4e720" JS_CACHE = "a7d4e720"
@ -26,7 +27,7 @@ JS_CACHE = "a7d4e720"
# email # email
EMAIL_BACKEND = env("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend") EMAIL_BACKEND = env("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend")
EMAIL_HOST = env("EMAIL_HOST") EMAIL_HOST = env("EMAIL_HOST")
EMAIL_PORT = env("EMAIL_PORT", 587) EMAIL_PORT = env.int("EMAIL_PORT", 587)
EMAIL_HOST_USER = env("EMAIL_HOST_USER") EMAIL_HOST_USER = env("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD") EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD")
EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", True) EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", True)
@ -68,13 +69,15 @@ FONT_DIR = os.path.join(STATIC_ROOT, "fonts")
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DEBUG", True) DEBUG = env.bool("DEBUG", True)
USE_HTTPS = env.bool("USE_HTTPS", not DEBUG) USE_HTTPS = env.bool("USE_HTTPS", not DEBUG)
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("SECRET_KEY")
if not DEBUG and SECRET_KEY == "7(2w1sedok=aznpq)ta1mc4i%4h=xx@hxwx*o57ctsuml0x%fr":
raise ImproperlyConfigured("You must change the SECRET_KEY env variable")
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", ["*"]) ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", ["*"])
# Application definition # Application definition
@ -205,14 +208,14 @@ WSGI_APPLICATION = "bookwyrm.wsgi.application"
# redis/activity streams settings # redis/activity streams settings
REDIS_ACTIVITY_HOST = env("REDIS_ACTIVITY_HOST", "localhost") REDIS_ACTIVITY_HOST = env("REDIS_ACTIVITY_HOST", "localhost")
REDIS_ACTIVITY_PORT = env("REDIS_ACTIVITY_PORT", 6379) REDIS_ACTIVITY_PORT = env.int("REDIS_ACTIVITY_PORT", 6379)
REDIS_ACTIVITY_PASSWORD = requests.utils.quote(env("REDIS_ACTIVITY_PASSWORD", "")) REDIS_ACTIVITY_PASSWORD = requests.utils.quote(env("REDIS_ACTIVITY_PASSWORD", ""))
REDIS_ACTIVITY_DB_INDEX = env("REDIS_ACTIVITY_DB_INDEX", 0) REDIS_ACTIVITY_DB_INDEX = env.int("REDIS_ACTIVITY_DB_INDEX", 0)
REDIS_ACTIVITY_URL = env( REDIS_ACTIVITY_URL = env(
"REDIS_ACTIVITY_URL", "REDIS_ACTIVITY_URL",
f"redis://:{REDIS_ACTIVITY_PASSWORD}@{REDIS_ACTIVITY_HOST}:{REDIS_ACTIVITY_PORT}/{REDIS_ACTIVITY_DB_INDEX}", f"redis://:{REDIS_ACTIVITY_PASSWORD}@{REDIS_ACTIVITY_HOST}:{REDIS_ACTIVITY_PORT}/{REDIS_ACTIVITY_DB_INDEX}",
) )
MAX_STREAM_LENGTH = int(env("MAX_STREAM_LENGTH", 200)) MAX_STREAM_LENGTH = env.int("MAX_STREAM_LENGTH", 200)
STREAMS = [ STREAMS = [
{"key": "home", "name": _("Home Timeline"), "shortname": _("Home")}, {"key": "home", "name": _("Home Timeline"), "shortname": _("Home")},
@ -221,12 +224,12 @@ STREAMS = [
# Search configuration # Search configuration
# total time in seconds that the instance will spend searching connectors # total time in seconds that the instance will spend searching connectors
SEARCH_TIMEOUT = int(env("SEARCH_TIMEOUT", 8)) SEARCH_TIMEOUT = env.int("SEARCH_TIMEOUT", 8)
# timeout for a query to an individual connector # timeout for a query to an individual connector
QUERY_TIMEOUT = int(env("QUERY_TIMEOUT", 5)) QUERY_TIMEOUT = env.int("QUERY_TIMEOUT", 5)
# Redis cache backend # Redis cache backend
if env("USE_DUMMY_CACHE", False): if env.bool("USE_DUMMY_CACHE", False):
CACHES = { CACHES = {
"default": { "default": {
"BACKEND": "django.core.cache.backends.dummy.DummyCache", "BACKEND": "django.core.cache.backends.dummy.DummyCache",
@ -256,7 +259,7 @@ DATABASES = {
"USER": env("POSTGRES_USER", "bookwyrm"), "USER": env("POSTGRES_USER", "bookwyrm"),
"PASSWORD": env("POSTGRES_PASSWORD", "bookwyrm"), "PASSWORD": env("POSTGRES_PASSWORD", "bookwyrm"),
"HOST": env("POSTGRES_HOST", ""), "HOST": env("POSTGRES_HOST", ""),
"PORT": env("PGPORT", 5432), "PORT": env.int("PGPORT", 5432),
}, },
} }

View file

@ -6,8 +6,8 @@ from bookwyrm.settings import *
# pylint: disable=line-too-long # pylint: disable=line-too-long
REDIS_BROKER_PASSWORD = requests.utils.quote(env("REDIS_BROKER_PASSWORD", "")) REDIS_BROKER_PASSWORD = requests.utils.quote(env("REDIS_BROKER_PASSWORD", ""))
REDIS_BROKER_HOST = env("REDIS_BROKER_HOST", "redis_broker") REDIS_BROKER_HOST = env("REDIS_BROKER_HOST", "redis_broker")
REDIS_BROKER_PORT = env("REDIS_BROKER_PORT", 6379) REDIS_BROKER_PORT = env.int("REDIS_BROKER_PORT", 6379)
REDIS_BROKER_DB_INDEX = env("REDIS_BROKER_DB_INDEX", 0) REDIS_BROKER_DB_INDEX = env.int("REDIS_BROKER_DB_INDEX", 0)
REDIS_BROKER_URL = env( REDIS_BROKER_URL = env(
"REDIS_BROKER_URL", "REDIS_BROKER_URL",
f"redis://:{REDIS_BROKER_PASSWORD}@{REDIS_BROKER_HOST}:{REDIS_BROKER_PORT}/{REDIS_BROKER_DB_INDEX}", f"redis://:{REDIS_BROKER_PASSWORD}@{REDIS_BROKER_HOST}:{REDIS_BROKER_PORT}/{REDIS_BROKER_DB_INDEX}",
@ -29,7 +29,7 @@ CELERY_TIMEZONE = env("TIME_ZONE", "UTC")
CELERY_WORKER_CONCURRENCY = env("CELERY_WORKER_CONCURRENCY", None) CELERY_WORKER_CONCURRENCY = env("CELERY_WORKER_CONCURRENCY", None)
CELERY_TASK_SOFT_TIME_LIMIT = env("CELERY_TASK_SOFT_TIME_LIMIT", None) CELERY_TASK_SOFT_TIME_LIMIT = env("CELERY_TASK_SOFT_TIME_LIMIT", None)
FLOWER_PORT = env("FLOWER_PORT") FLOWER_PORT = env.int("FLOWER_PORT", 8888)
INSTALLED_APPS = INSTALLED_APPS + [ INSTALLED_APPS = INSTALLED_APPS + [
"celerywyrm", "celerywyrm",