From e7af4737c191097b9f5054aff1b3945f9f2e5ac9 Mon Sep 17 00:00:00 2001 From: eyjhb Date: Thu, 5 Dec 2024 17:02:18 +0100 Subject: [PATCH] wger: added redis, cache/compressed/combined, nginx serves static --- machines/gerd/services/wger/default.nix | 27 +++++++++++++--- .../gerd/services/wger/wgerpkg/default.nix | 32 ++++++++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/machines/gerd/services/wger/default.nix b/machines/gerd/services/wger/default.nix index f46d595..cf3e762 100644 --- a/machines/gerd/services/wger/default.nix +++ b/machines/gerd/services/wger/default.nix @@ -22,7 +22,7 @@ let # to create a CACHE folder/file in the CWD. # and if I fix that, then static content no # longer wants to load. - DEBUG = true; + DEBUG = false; DATABASES.default = { ENGINE = "django.db.backends.postgresql"; NAME = "wger"; @@ -43,7 +43,6 @@ let MEDIA_ROOT = "${statedir}/media"; MEDIA_URL = "/media/"; - # EMAIL EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"; EMAIL_HOST = config.mine.shared.settings.mail.domain_smtp; @@ -54,6 +53,12 @@ let EMAIL_FROM_ADDRESS = wger_settings.EMAIL_FROM; EMAIL_PAGE_DOMAIN = SITE_URL; + # Cache - Redis + DJANGO_CACHE_BACKEND = "django_redis.cache.RedisCache"; + DJANGO_CACHE_LOCATION = "${config.services.redis.servers.wger.unixSocket}"; + DJANGO_CACHE_TIMEOUT = 1296000; # in seconds - 60*60*24*15, 15 Days + DJANGO_CACHE_CLIENT_CLASS = "django_redis.client.DefaultClient"; + # setup allowed hosts CSRF_TRUSTED_ORIGINS = [ "https://${svc_domain}" ]; ALLOWED_HOSTS = [ svc_domain ]; @@ -62,6 +67,11 @@ let RECAPTCHA_PUBLIC_KEY = ""; RECAPTCHA_PRIVATE_KEY = ""; USE_RECAPTCHA = false; + + # does not work + STATIC_ROOT = "${wgerpkgs}/share/static"; + COMPRESS_ROOT = STATIC_ROOT; + COMPRESS_OFFLINE = true; }; wger_settings_file = pkgs.writeText "settings.json" (builtins.toJSON wger_settings); @@ -105,9 +115,10 @@ in { serviceConfig = { EnvironmentFile = config.age.secrets.wger-env.path; - # ensure it does not try to create `/CACHE` + Restart = "on-failure"; + RestartSec = "5s"; + PrivateTmp = "yes"; - WorkingDirectory = "/tmp"; User = "wger"; Group = "wger"; @@ -149,6 +160,11 @@ in { }]; }; + # setup redis + services.redis.servers.wger = { + enable = true; + user = wger_user; + }; # setup users users.users."${wger_user}"= { @@ -170,7 +186,8 @@ in { proxyPass = "http://localhost:${builtins.toString port}"; }; - locations."/static".proxyPass = "http://localhost:${builtins.toString port}"; + # locations."/static".proxyPass = "http://localhost:${builtins.toString port}"; + locations."/static".root = "${wgerpkgs}/share"; locations."/media".proxyPass = "http://localhost:${builtins.toString port}"; locations."/api".proxyPass = "http://localhost:${builtins.toString port}"; }; diff --git a/machines/gerd/services/wger/wgerpkg/default.nix b/machines/gerd/services/wger/wgerpkg/default.nix index 0148e84..bb2438d 100644 --- a/machines/gerd/services/wger/wgerpkg/default.nix +++ b/machines/gerd/services/wger/wgerpkg/default.nix @@ -3,6 +3,7 @@ python3, fetchFromGitHub, callPackage, + writeText, }: let @@ -79,8 +80,37 @@ in python3.pkgs.buildPythonApplication rec { cp manage.py wger/manage.py ''; + # fixup compressed files + postBuild = let + staticSettings = writeText "static_settings.py" '' + DEBUG = False + STATIC_ROOT = os.environ["static"] + COMPRESS_OFFLINE = True + # So we don't need postgres dependencies + DATABASES = {} + ''; + in '' + # copy over static yarn things + # cp -a ${frontend}/static/yarn $out/${python3.sitePackages}/wger/core/static + cp -a ${frontend}/static/yarn wger/core/static + + python3 -m wger.tasks create-settings -s $PWD/tmp_settings.py + cat ${staticSettings} >> $PWD/tmp_settings.py + mkdir tmpstatic + pushd tmpstatic + + static=. WGER_SETTINGS=../tmp_settings.py python3 ../manage.py collectstatic --no-input + static=. WGER_SETTINGS=../tmp_settings.py python3 ../manage.py compress --force + + popd + ''; + postInstall = '' - cp -a ${frontend}/static/yarn $out/${python3.sitePackages}/wger/core/static + rm -rf $out/${python3.sitePackages}/wger/core/static + cp -a tmpstatic $out/${python3.sitePackages}/wger/core/static + + mkdir $out/share + cp -a $out/${python3.sitePackages}/wger/core/static $out/share ''; pythonImportsCheck = [