1
Fork 0

replace gunicorn with uwsgi

has an easier-to-work-with prometheus.
This commit is contained in:
Motiejus Jakštys 2024-01-15 16:14:46 +02:00
parent b983a2f59a
commit 50c34aa922
4 changed files with 33 additions and 27 deletions

View File

@ -11,10 +11,10 @@ format:
run: run:
app/manage.py runserver app/manage.py runserver
.PHONY: run-gunicorn .PHONY: run-backend
run-gunicorn: run-backend:
nix run .#e11sync-gunicorn nix run .#e11sync-backend
.PHONY: run-caddy .PHONY: run-frontend
run-caddy: run-frontend:
nix run .#e11sync-caddy nix run .#e11sync-frontend

View File

@ -44,16 +44,19 @@
e11sync-static = pkgs.callPackage ./pkgs/e11sync-static.nix {}; 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; 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 { in {
packages = { packages = {
inherit geoip-mmdb; inherit geoip-mmdb;
inherit e11sync-static; inherit e11sync-static;
inherit e11sync-caddy; inherit e11sync-frontend;
inherit e11sync; inherit e11sync;
}; };
@ -85,18 +88,18 @@
exec ${e11sync}/bin/e11sync exec ${e11sync}/bin/e11sync
''); '');
}; };
# wsgi/gunicorn # uwsgi
e11sync-gunicorn = { e11sync-backend = {
type = "app"; type = "app";
name = "e11sync-gunicorn"; name = "e11sync-backend";
program = toString (pkgs.writeShellScript "wrapper" '' program = toString (pkgs.writeShellScript "wrapper" ''
export E11SYNC_DATABASE_PATH=$PWD/db.sqlite3 export E11SYNC_DATABASE_PATH=$PWD/db.sqlite3
exec ${e11sync}/bin/e11sync-gunicorn exec ${e11sync}/bin/e11sync-backend
''); '');
}; };
e11sync-caddy = { e11sync-frontend = {
type = "app"; type = "app";
name = "e11sync-caddy"; name = "e11sync-frontend";
program = let program = let
caddyFile = pkgs.writeTextFile { caddyFile = pkgs.writeTextFile {
name = "Caddyfile"; name = "Caddyfile";
@ -108,7 +111,7 @@
} }
:8001 :8001
${builtins.readFile "${e11sync-caddy}"} ${builtins.readFile "${e11sync-frontend}"}
''; '';
}; };
in in

View File

@ -1,10 +1,10 @@
{ {
writeTextFile, writeTextFile,
e11sync-static, e11sync-static,
gunicornPort ? 8002, backendPort ? 8002,
}: }:
writeTextFile { writeTextFile {
name = "e11sync-caddy"; name = "e11sync-frontend";
text = '' text = ''
header Strict-Transport-Security "max-age=31536000" header Strict-Transport-Security "max-age=31536000"
header /static/CACHE/* Cache-Control "public, max-age=31536000, immutable" 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}
''; '';
} }

View File

@ -6,7 +6,8 @@
python3Packages, python3Packages,
libmaxminddb, libmaxminddb,
dart-sass, dart-sass,
gunicornPort ? 8002, uwsgi,
backendPort ? 8002,
database-path ? null, database-path ? null,
geoip-mmdb, geoip-mmdb,
}: }:
@ -24,20 +25,22 @@ stdenv.mkDerivation {
buildPhase = ''mkdir -p $out''; buildPhase = ''mkdir -p $out'';
installPhase = '' installPhase = ''
cp -r . $out/app 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 \ --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) '' ${lib.optionalString (database-path != null) ''
--set E11SYNC_DATABASE_PATH "${database-path}" \ --set E11SYNC_DATABASE_PATH "${database-path}" \
''} \ ''} \
--set E11SYNC_DEBUG "" \ --set E11SYNC_DEBUG "" \
--set E11SYNC_COMPRESS_OFFLINE 1 \ --set E11SYNC_COMPRESS_OFFLINE 1 \
--set GEOIP_PATH "${geoip-mmdb}" \ --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 = passthru.tests.unit =
runCommand "e11sync-test" { runCommand "e11sync-test" {