Skip to content

Commit

Permalink
Merge pull request #187 from maykinmedia/feature/streamline-envvars
Browse files Browse the repository at this point in the history
🔧 Streamline environment variables
  • Loading branch information
stevenbal authored May 17, 2024
2 parents 706adbc + 0add466 commit 00b8d28
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 140 deletions.
14 changes: 14 additions & 0 deletions src/openklant/accounts/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import factory


class UserFactory(factory.django.DjangoModelFactory):
username = factory.Sequence(lambda n: f"user-{n}")
password = factory.PostGenerationMethodCall("set_password", "secret")

class Meta:
model = "accounts.User"


class SuperUserFactory(UserFactory):
is_staff = True
is_superuser = True
88 changes: 71 additions & 17 deletions src/openklant/conf/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import warnings

from django.urls import reverse_lazy

import sentry_sdk
from corsheaders.defaults import default_headers as default_cors_headers
from log_outgoing_requests.formatters import HttpFormatter

from .api import * # noqa
Expand Down Expand Up @@ -31,6 +33,7 @@

# = domains we're running on
ALLOWED_HOSTS = config("ALLOWED_HOSTS", default="", split=True)
USE_X_FORWARDED_HOST = config("USE_X_FORWARDED_HOST", default=False)

IS_HTTPS = config("IS_HTTPS", default=not DEBUG)

Expand All @@ -54,7 +57,7 @@
#
DATABASES = {
"default": {
"ENGINE": config("DB_ENGINE", "django.db.backends.postgresql"),
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME", "openklant"),
"USER": config("DB_USER", "openklant"),
"PASSWORD": config("DB_PASSWORD", "openklant"),
Expand Down Expand Up @@ -84,7 +87,14 @@
"IGNORE_EXCEPTIONS": True,
},
},
"oidc": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"},
"oidc": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{config('CACHE_OIDC', 'localhost:6379/0')}",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"IGNORE_EXCEPTIONS": True,
},
},
}


Expand Down Expand Up @@ -219,16 +229,26 @@
EMAIL_USE_TLS = config("EMAIL_USE_TLS", default=False)
EMAIL_TIMEOUT = 10

DEFAULT_FROM_EMAIL = "[email protected]"
DEFAULT_FROM_EMAIL = config("DEFAULT_FROM_EMAIL", "[email protected]")

#
# LOGGING
#
LOG_STDOUT = config("LOG_STDOUT", default=False)
LOG_REQUESTS = config("LOG_REQUESTS", default=True)
LOG_LEVEL = config("LOG_LEVEL", default="WARNING")
LOG_QUERIES = config("LOG_QUERIES", default=False)
LOG_REQUESTS = config("LOG_REQUESTS", default=False)
if LOG_QUERIES and not DEBUG:
warnings.warn(
"Requested LOG_QUERIES=1 but DEBUG is false, no query logs will be emited.",
RuntimeWarning,
)

LOGGING_DIR = os.path.join(BASE_DIR, "log")

_root_handlers = ["console"] if LOG_STDOUT else ["project"]
_django_handlers = ["console"] if LOG_STDOUT else ["django"]

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
Expand All @@ -241,6 +261,7 @@
"performance": {
"format": "%(asctime)s %(process)d | %(thread)d | %(message)s",
},
"db": {"format": "%(asctime)s | %(message)s"},
"outgoing_requests": {"()": HttpFormatter},
},
"filters": {
Expand All @@ -261,6 +282,11 @@
"class": "logging.StreamHandler",
"formatter": "timestamped",
},
"console_db": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "db",
},
"django": {
"level": "DEBUG",
"class": "logging.handlers.RotatingFileHandler",
Expand Down Expand Up @@ -296,25 +322,30 @@
},
},
"loggers": {
"": {
"handlers": _root_handlers,
"level": "ERROR",
"propagate": False,
},
"openklant": {
"handlers": ["project"] if not LOG_STDOUT else ["console"],
"level": "INFO",
"handlers": _root_handlers,
"level": LOG_LEVEL,
"propagate": True,
},
"mozilla_django_oidc": {
"handlers": _root_handlers,
"level": LOG_LEVEL,
},
"django.request": {
"handlers": ["django"] if not LOG_STDOUT else ["console"],
"level": "ERROR",
"handlers": _django_handlers,
"level": LOG_LEVEL,
"propagate": True,
},
"django.template": {
"handlers": ["console"],
"level": "INFO",
"propagate": True,
},
"mozilla_django_oidc": {
"handlers": ["project"],
"level": "DEBUG",
},
"log_outgoing_requests": {
"handlers": (
["log_outgoing_requests", "save_outgoing_requests"]
Expand All @@ -324,6 +355,11 @@
"level": "DEBUG",
"propagate": True,
},
"django.db.backends": {
"handlers": ["console_db"] if LOG_QUERIES else [],
"level": "DEBUG",
"propagate": False,
},
},
}

Expand Down Expand Up @@ -454,6 +490,19 @@
"REMOTE_ADDR",
)

#
# DJANGO-CORS-MIDDLEWARE
#
CORS_ALLOW_ALL_ORIGINS = config("CORS_ALLOW_ALL_ORIGINS", default=False)
CORS_ALLOWED_ORIGINS = config("CORS_ALLOWED_ORIGINS", split=True, default=[])
CORS_ALLOWED_ORIGIN_REGEXES = config(
"CORS_ALLOWED_ORIGIN_REGEXES", split=True, default=[]
)
# Authorization is included in default_cors_headers
CORS_ALLOW_HEADERS = list(default_cors_headers) + config(
"CORS_EXTRA_ALLOW_HEADERS", split=True, default=[]
)

#
# RAVEN/SENTRY - error monitoring
#
Expand All @@ -472,17 +521,24 @@
send_default_pii=True,
)

#
# Elastic APM
ELASTIC_APM_SERVER_URL = os.getenv("ELASTIC_APM_SERVER_URL", None)
#
ELASTIC_APM_SERVER_URL = config("ELASTIC_APM_SERVER_URL", None)
ELASTIC_APM = {
"SERVICE_NAME": f"openklant {ENVIRONMENT}",
"SERVICE_NAME": f"Open Klant - {ENVIRONMENT}",
"SECRET_TOKEN": config("ELASTIC_APM_SECRET_TOKEN", "default"),
"SERVER_URL": ELASTIC_APM_SERVER_URL,
"TRANSACTION_SAMPLE_RATE": config("ELASTIC_APM_TRANSACTION_SAMPLE_RATE", 0.1),
}
if not ELASTIC_APM_SERVER_URL:
ELASTIC_APM["ENABLED"] = False
ELASTIC_APM["SERVER_URL"] = "http://localhost:8200"

else:
MIDDLEWARE = ["elasticapm.contrib.django.middleware.TracingMiddleware"] + MIDDLEWARE
INSTALLED_APPS = INSTALLED_APPS + [
"elasticapm.contrib.django",
]

#
# Mozilla Django OIDC DB settings
Expand All @@ -497,8 +553,6 @@
LOG_OUTGOING_REQUESTS_EMIT_BODY = True
LOG_OUTGOING_REQUESTS_DB_SAVE = config("LOG_OUTGOING_REQUESTS_DB_SAVE", default=False)
LOG_OUTGOING_REQUESTS_RESET_DB_SAVE_AFTER = None

# Custom settings
LOG_OUTGOING_REQUESTS_MAX_AGE = config(
"LOG_OUTGOING_REQUESTS_MAX_AGE", default=7
) # number of days
8 changes: 3 additions & 5 deletions src/openklant/conf/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@
"level": "DEBUG",
"propagate": True,
},
"django.db.backends": {
"handlers": ["django"],
"level": "DEBUG",
"propagate": False,
},
"performance": {
"handlers": ["console"],
"level": "INFO",
Expand All @@ -63,6 +58,9 @@
}
)

if not LOG_QUERIES:
LOGGING["loggers"]["django.db.backends"]["handlers"] = ["django"]

# in memory cache and django-axes don't get along.
# https://django-axes.readthedocs.io/en/latest/configuration.html#known-configuration-problems
CACHES = {
Expand Down
10 changes: 6 additions & 4 deletions src/openklant/conf/docker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os

os.environ.setdefault("DB_USER", os.getenv("DATABASE_USER", "postgres"))
os.environ.setdefault("DB_NAME", os.getenv("DATABASE_NAME", "postgres"))
os.environ.setdefault("DB_PASSWORD", os.getenv("DATABASE_PASSWORD", ""))
os.environ.setdefault("DB_HOST", os.getenv("DATABASE_HOST", "db"))
from .includes.environ import config

os.environ.setdefault("DB_USER", config("DATABASE_USER", "postgres"))
os.environ.setdefault("DB_NAME", config("DATABASE_NAME", "postgres"))
os.environ.setdefault("DB_PASSWORD", config("DATABASE_PASSWORD", ""))
os.environ.setdefault("DB_HOST", config("DATABASE_HOST", "db"))

os.environ.setdefault("ENVIRONMENT", "docker")
os.environ.setdefault("LOG_STDOUT", "yes")
Expand Down
12 changes: 0 additions & 12 deletions src/openklant/conf/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,3 @@
# Custom settings overrides
#
SHOW_ALERT = False

##############################
# #
# 3RD PARTY LIBRARY SETTINGS #
# #
##############################

# APM
MIDDLEWARE = ["elasticapm.contrib.django.middleware.TracingMiddleware"] + MIDDLEWARE
INSTALLED_APPS = INSTALLED_APPS + [
"elasticapm.contrib.django",
]
102 changes: 0 additions & 102 deletions src/openklant/templates/sniplates/form.html

This file was deleted.

Loading

0 comments on commit 00b8d28

Please sign in to comment.