feat(django-apps): Finish the daphne-specific setup

This commit is contained in:
Tom Hubrecht 2025-01-16 18:50:44 +01:00
parent 03541914ae
commit a7b34850f4
Signed by: thubrecht
SSH key fingerprint: SHA256:r+nK/SIcWlJ0zFZJGHtlAoRwq1Rm+WcKAm5ADYMoQPc

View file

@ -264,6 +264,7 @@ in
ps:
[ (config.django ps) ]
++ (optional (config.application.type != "daphne") ps.gunicorn)
++ (optional (config.application.type == "daphne") ps.daphne)
++ (optional (config.application.type == "asgi") ps.uvicorn)
++ (optional (config.dbType == "postgresql") ps.psycopg)
++ (config.dependencies ps)
@ -547,6 +548,7 @@ in
virtualHosts = mapAttrs' (
name:
{
application,
domain,
nginx,
serveMedia,
@ -555,7 +557,11 @@ in
nameValuePair domain (
recursiveUpdate {
locations = {
"/".proxyPass = "http://unix:/run/django-apps/${name}.sock";
"/".proxyPass =
if application.type == "daphne" then
"http://unix:/run/django-apps/${name}/socket"
else
"http://unix:/run/django-apps/${name}.sock";
"/static/".root = "/run/django-apps/${name}";
"/media/".root = mkIf serveMedia "/run/django-apps/${name}";
};
@ -600,7 +606,7 @@ in
SocketUser = config'.services.nginx.user;
};
}
) cfg.sites;
) (filterAttrs (_: { application, ... }: application.type != "daphne") cfg.sites);
mounts = concatLists (
mapAttrsToList (
@ -704,9 +710,15 @@ in
mkdir -p ${config.mediaDirectory} ${config.staticDirectory}
'';
requires = [ "dj-${name}.socket" ];
requires = optional (config.application.type != "daphne") "dj-${name}.socket";
wantedBy = [ "multi-user.target" ];
script = mkIf (config.application.type == "daphne") ''
cd source && ${getExe' config.djangoEnv "daphne"} \
-u /run/django-apps/${name}/socket \
${config.application.module}.asgi:application
'';
serviceConfig = {
inherit
Group
@ -719,31 +731,23 @@ in
;
DynamicUser = true;
ExecStart = escapeSystemdExecArgs (
if (config.application.type == "daphne") then
ExecStart = mkIf (config.application.type != "daphne") (
escapeSystemdExecArgs (
[
(getExe' config.djangoEnv "daphne")
"-u"
"/run/django-apps/${name}.sock"
"${config.application.module}.asgi:${config.application.channelLayer}"
(getExe' config.djangoEnv "gunicorn")
"--workers"
config.application.workers
"--bind"
"unix:/run/django-apps/${name}.sock"
"--pythonpath"
"source"
]
else
(
[
(getExe' config.djangoEnv "gunicorn")
"--workers"
config.application.workers
"--bind"
"unix:/run/django-apps/${name}.sock"
"--pythonpath"
"source"
]
++ (optionals (config.application.type == "asgi") [
"--worker-class"
"uvicorn.workers.UvicornWorker"
])
++ [ "${config.application.module}.${config.application.type}" ]
)
++ (optionals (config.application.type == "asgi") [
"--worker-class"
"uvicorn.workers.UvicornWorker"
])
++ [ "${config.application.module}.${config.application.type}" ]
)
);
ExecReload = "${getExe' pkgs.coreutils "kill"} -s HUP $MAINPID";
KillMode = "mixed";