chore(bbuddy): websocket screen service

This commit is contained in:
arcnmx 2024-03-22 11:01:42 -07:00
parent dc0c84c0a0
commit c51fdb847b
2 changed files with 48 additions and 8 deletions

View file

@ -42,6 +42,13 @@ in {
default = [ "127.0.0.1" "::1" ]; default = [ "127.0.0.1" "::1" ];
}; };
}; };
screen = {
enable = mkEnableOption "websocket server";
websocketPort = mkOption {
type = port;
default = 47631;
};
};
redis = { redis = {
enable = mkEnableOption "redis cache"; enable = mkEnableOption "redis cache";
server = mkOption { server = mkOption {
@ -77,7 +84,7 @@ in {
bbuddyConfig.services.barcodebuddy = { bbuddyConfig.services.barcodebuddy = {
settings = let settings = let
defaults = mapOptionDefaults { defaults = mapOptionDefaults {
PORT_WEBSOCKET_SERVER = 47631; ${if cfg.screen.enable then "PORT_WEBSOCKET_SERVER" else null} = cfg.screen.websocketPort;
SEARCH_ENGINE = "https://google.com/search?q="; SEARCH_ENGINE = "https://google.com/search?q=";
${if cfg.reverseProxy.enable then "TRUSTED_PROXIES" else null} = cfg.reverseProxy.trustedAddresses; ${if cfg.reverseProxy.enable then "TRUSTED_PROXIES" else null} = cfg.reverseProxy.trustedAddresses;
DISABLE_AUTHENTICATION = false; DISABLE_AUTHENTICATION = false;
@ -133,7 +140,9 @@ in {
all.pdo_sqlite all.pdo_sqlite
all.sockets all.sockets
all.gettext all.gettext
] ++ optional cfg.redis.enable all.redis); all.session
all.redis
]);
settings = mapOptionDefaults { settings = mapOptionDefaults {
"pm.max_children" = 10; "pm.max_children" = 10;
@ -157,6 +166,9 @@ in {
virtualHosts."${cfg.hostName}" = { virtualHosts."${cfg.hostName}" = {
root = "${cfg.package}"; root = "${cfg.package}";
locations = { locations = {
"/api/".extraConfig = ''
try_files $uri /api/index.php$is_args$query_string;
'';
"~ \\.php$".extraConfig = cfg.nginxPhpConfig; "~ \\.php$".extraConfig = cfg.nginxPhpConfig;
}; };
extraConfig = '' extraConfig = ''
@ -164,5 +176,18 @@ in {
''; '';
}; };
}; };
conf.systemd.services.bbuddy-websocket = mkIf cfg.screen.enable {
wantedBy = [ "multi-user.target" ];
environment = mapAttrs' toEnvPair cfg.settings;
unitConfig = {
Description = "Run websocket server for barcodebuddy screen feature";
};
serviceConfig = {
ExecStart = "${config.services.phpfpm.pools.barcodebuddy.phpPackage}/bin/php ${cfg.package}/wsserver.php";
Restart = "on-failure";
StandardOutput = "null";
User = "barcodebuddy";
};
};
in mkMerge [ bbuddyConfig (mkIf cfg.enable conf) ]; in mkMerge [ bbuddyConfig (mkIf cfg.enable conf) ];
} }

View file

@ -1,5 +1,5 @@
{config, lib, ...}: let {config, lib, ...}: let
inherit (lib.modules) mkDefault; inherit (lib.modules) mkIf mkMerge mkAfter mkDefault;
inherit (config.services) nginx; inherit (config.services) nginx;
cfg = config.services.barcodebuddy; cfg = config.services.barcodebuddy;
in { in {
@ -11,11 +11,23 @@ in {
EXTERNAL_GROCY_URL = "https://grocy.${config.networking.domain}"; EXTERNAL_GROCY_URL = "https://grocy.${config.networking.domain}";
DISABLE_AUTHENTICATION = true; DISABLE_AUTHENTICATION = true;
}; };
nginxPhpConfig = '' nginxPhpConfig = mkMerge [
include ${config.sops.secrets.barcodebuddy-fastcgi-params.path}; ''
''; include ${config.sops.secrets.barcodebuddy-fastcgi-params.path};
''
(mkIf nginx.virtualHosts.barcodebuddy.proxied.enabled (mkAfter ''
set $bbuddy_https "";
if ($x_scheme = https) {
set $bbuddy_https 1;
}
fastcgi_param HTTPS $bbuddy_https if_not_empty;
fastcgi_param REQUEST_SCHEME $x_scheme;
fastcgi_param HTTP_HOST $x_forwarded_host;
''))
];
}; };
config.services.nginx.virtualHosts.barcodebuddy = { config.services.nginx.virtualHosts.barcodebuddy = mkIf cfg.enable {
proxied.xvars.enable = true;
vouch = { vouch = {
enable = true; enable = true;
requireAuth = false; requireAuth = false;
@ -31,7 +43,10 @@ in {
}; };
}; };
}; };
config.sops.secrets.barcodebuddy-fastcgi-params = { config.users.users.barcodebuddy = mkIf cfg.enable {
uid = 912;
};
config.sops.secrets.barcodebuddy-fastcgi-params = mkIf cfg.enable {
sopsFile = mkDefault ./secrets/barcodebuddy.yaml; sopsFile = mkDefault ./secrets/barcodebuddy.yaml;
owner = mkDefault nginx.user; owner = mkDefault nginx.user;
}; };