From 50c34aa92214a3d78aae0a05bbc55599d1cefe52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 15 Jan 2024 16:14:46 +0200 Subject: [PATCH] replace gunicorn with uwsgi has an easier-to-work-with prometheus. --- Makefile | 12 +++++----- flake.nix | 23 +++++++++++-------- ...e11sync-caddy.nix => e11sync-frontend.nix} | 6 ++--- pkgs/e11sync.nix | 19 ++++++++------- 4 files changed, 33 insertions(+), 27 deletions(-) rename pkgs/{e11sync-caddy.nix => e11sync-frontend.nix} (76%) diff --git a/Makefile b/Makefile index 53d3434..c07c747 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ format: run: app/manage.py runserver -.PHONY: run-gunicorn -run-gunicorn: - nix run .#e11sync-gunicorn +.PHONY: run-backend +run-backend: + nix run .#e11sync-backend -.PHONY: run-caddy -run-caddy: - nix run .#e11sync-caddy +.PHONY: run-frontend +run-frontend: + nix run .#e11sync-frontend diff --git a/flake.nix b/flake.nix index faa0cee..9966aa5 100644 --- a/flake.nix +++ b/flake.nix @@ -44,16 +44,19 @@ e11sync-static = pkgs.callPackage ./pkgs/e11sync-static.nix {}; - e11sync-caddy = pkgs.callPackage ./pkgs/e11sync-caddy.nix { + e11sync-frontend = pkgs.callPackage ./pkgs/e11sync-frontend.nix { inherit e11sync-static; }; - e11sync = pkgs.callPackage ./pkgs/e11sync.nix {inherit geoip-mmdb;}; + e11sync = pkgs.callPackage ./pkgs/e11sync.nix { + inherit geoip-mmdb; + uwsgi = pkgs.uwsgi.override {plugins = ["python3"];}; + }; in { packages = { inherit geoip-mmdb; inherit e11sync-static; - inherit e11sync-caddy; + inherit e11sync-frontend; inherit e11sync; }; @@ -85,18 +88,18 @@ exec ${e11sync}/bin/e11sync ''); }; - # wsgi/gunicorn - e11sync-gunicorn = { + # uwsgi + e11sync-backend = { type = "app"; - name = "e11sync-gunicorn"; + name = "e11sync-backend"; program = toString (pkgs.writeShellScript "wrapper" '' export E11SYNC_DATABASE_PATH=$PWD/db.sqlite3 - exec ${e11sync}/bin/e11sync-gunicorn + exec ${e11sync}/bin/e11sync-backend ''); }; - e11sync-caddy = { + e11sync-frontend = { type = "app"; - name = "e11sync-caddy"; + name = "e11sync-frontend"; program = let caddyFile = pkgs.writeTextFile { name = "Caddyfile"; @@ -108,7 +111,7 @@ } :8001 - ${builtins.readFile "${e11sync-caddy}"} + ${builtins.readFile "${e11sync-frontend}"} ''; }; in diff --git a/pkgs/e11sync-caddy.nix b/pkgs/e11sync-frontend.nix similarity index 76% rename from pkgs/e11sync-caddy.nix rename to pkgs/e11sync-frontend.nix index beb0442..bc1549d 100644 --- a/pkgs/e11sync-caddy.nix +++ b/pkgs/e11sync-frontend.nix @@ -1,10 +1,10 @@ { writeTextFile, e11sync-static, - gunicornPort ? 8002, + backendPort ? 8002, }: writeTextFile { - name = "e11sync-caddy"; + name = "e11sync-frontend"; text = '' header Strict-Transport-Security "max-age=31536000" header /static/CACHE/* Cache-Control "public, max-age=31536000, immutable" @@ -17,6 +17,6 @@ writeTextFile { } } - reverse_proxy http://127.0.0.1:${toString gunicornPort} + reverse_proxy http://127.0.0.1:${toString backendPort} ''; } diff --git a/pkgs/e11sync.nix b/pkgs/e11sync.nix index 48a70bc..93f00c6 100644 --- a/pkgs/e11sync.nix +++ b/pkgs/e11sync.nix @@ -6,7 +6,8 @@ python3Packages, libmaxminddb, dart-sass, - gunicornPort ? 8002, + uwsgi, + backendPort ? 8002, database-path ? null, geoip-mmdb, }: @@ -24,20 +25,22 @@ stdenv.mkDerivation { buildPhase = ''mkdir -p $out''; installPhase = '' cp -r . $out/app - makeWrapper ${python3Packages.gunicorn}/bin/gunicorn $out/bin/e11sync-gunicorn \ + makeWrapper $out/app/manage.py $out/bin/e11sync \ + --set GEOIP_PATH "${geoip-mmdb}" + + makeWrapper ${uwsgi}/bin/uwsgi $out/bin/e11sync-backend \ --chdir $out/app \ - --set-default E11SYNC_HTTP_PORT ${toString gunicornPort} \ + --set-default E11SYNC_HTTP_PORT ${toString backendPort} \ + --add-flags "--plugin ${uwsgi}/lib/uwsgi/python3_plugin.so" \ + --add-flags "--http-socket 127.0.0.1:${toString backendPort}" \ + --add-flags "--wsgi-file e11sync/wsgi.py" \ + --add-flags --master \ ${lib.optionalString (database-path != null) '' --set E11SYNC_DATABASE_PATH "${database-path}" \ ''} \ --set E11SYNC_DEBUG "" \ --set E11SYNC_COMPRESS_OFFLINE 1 \ --set GEOIP_PATH "${geoip-mmdb}" \ - --add-flags --bind=127.0.0.1:\$E11SYNC_HTTP_PORT \ - --add-flags e11sync.wsgi - - makeWrapper $out/app/manage.py $out/bin/e11sync \ - --set GEOIP_PATH "${geoip-mmdb}" ''; passthru.tests.unit = runCommand "e11sync-test" {