From c3d052ad01570618ee5e4c1c65bbb7711c728bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 15 Jan 2024 11:57:36 +0200 Subject: [PATCH] add e11sync-caddy --- Makefile | 5 ++++- app/e11sync/settings.py | 4 ++-- flake.nix | 27 +++++++++++++++++++++++++++ pkgs/e11sync-caddy.nix | 22 ++++++++++++++++++++++ pkgs/e11sync.nix | 2 +- 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 pkgs/e11sync-caddy.nix diff --git a/Makefile b/Makefile index c050ef5..53d3434 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,10 @@ format: run: app/manage.py runserver -# "Like production". Run with "run-caddy". .PHONY: run-gunicorn run-gunicorn: nix run .#e11sync-gunicorn + +.PHONY: run-caddy +run-caddy: + nix run .#e11sync-caddy diff --git a/app/e11sync/settings.py b/app/e11sync/settings.py index 257421d..f4c552a 100644 --- a/app/e11sync/settings.py +++ b/app/e11sync/settings.py @@ -38,7 +38,7 @@ SECRET_KEY = _SECRET_KEY DEBUG = _DEBUG -ALLOWED_HOSTS = ["127.0.0.1"] +ALLOWED_HOSTS = ["127.0.0.1", "localhost"] INTERNAL_IPS = ["127.0.0.1"] @@ -140,7 +140,7 @@ COMPRESS_FILTERS = { } COMPRESS_PRECOMPILERS = ( - ('text/x-scss', 'sass {infile} {outfile}'), + ('text/x-scss', 'sass --no-source-map {infile} {outfile}'), ) DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/flake.nix b/flake.nix index 0c6b186..faa0cee 100644 --- a/flake.nix +++ b/flake.nix @@ -44,11 +44,16 @@ e11sync-static = pkgs.callPackage ./pkgs/e11sync-static.nix {}; + e11sync-caddy = pkgs.callPackage ./pkgs/e11sync-caddy.nix { + inherit e11sync-static; + }; + e11sync = pkgs.callPackage ./pkgs/e11sync.nix {inherit geoip-mmdb;}; in { packages = { inherit geoip-mmdb; inherit e11sync-static; + inherit e11sync-caddy; inherit e11sync; }; @@ -89,6 +94,28 @@ exec ${e11sync}/bin/e11sync-gunicorn ''); }; + e11sync-caddy = { + type = "app"; + name = "e11sync-caddy"; + program = let + caddyFile = pkgs.writeTextFile { + name = "Caddyfile"; + text = '' + { + http_port 8001 + auto_https off + debug + } + :8001 + + ${builtins.readFile "${e11sync-caddy}"} + ''; + }; + in + toString (pkgs.writeShellScript "wrapper" '' + exec ${pkgs.caddy}/bin/caddy run --config ${caddyFile} --adapter caddyfile + ''); + }; }; devShells.default = pkgs.mkShellNoCC { diff --git a/pkgs/e11sync-caddy.nix b/pkgs/e11sync-caddy.nix new file mode 100644 index 0000000..8acb190 --- /dev/null +++ b/pkgs/e11sync-caddy.nix @@ -0,0 +1,22 @@ +{ + writeTextFile, + e11sync-static, + gunicornPort ? 8002, +}: +writeTextFile { + name = "e11sync-caddy"; + text = '' + header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" + header /static/CACHE/* Cache-Control "public, max-age=31536000, immutable" + + route /static/* { + uri strip_prefix /static + file_server * { + root ${e11sync-static} + precompressed br gzip + } + } + + reverse_proxy http://127.0.0.1:${toString gunicornPort} + ''; +} diff --git a/pkgs/e11sync.nix b/pkgs/e11sync.nix index 932aaa0..48a70bc 100644 --- a/pkgs/e11sync.nix +++ b/pkgs/e11sync.nix @@ -6,7 +6,7 @@ python3Packages, libmaxminddb, dart-sass, - gunicornPort ? 8001, + gunicornPort ? 8002, database-path ? null, geoip-mmdb, }: