From 0e67adfcd6d152915c066e9cced68eb5938d0ceb Mon Sep 17 00:00:00 2001 From: efim Date: Tue, 27 Jun 2023 15:26:43 +0000 Subject: [PATCH] feat(11): adding reverse proxy to module --- 11-single-price-grid-component/default.nix | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/11-single-price-grid-component/default.nix b/11-single-price-grid-component/default.nix index a7d05f4..d0050f8 100644 --- a/11-single-price-grid-component/default.nix +++ b/11-single-price-grid-component/default.nix @@ -43,6 +43,12 @@ let default = "localhost"; description = "Host to bind to."; }; + + useNginx = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Whether to use Nginx to proxy requests."; + }; }; config = lib.mkIf cfg.enable { users.groups.price-grid-app-group = { }; @@ -51,7 +57,9 @@ let group = "price-grid-app-group"; }; - systemd.services.price-grid-app = { + systemd.services.price-grid-app = + let serverHost = if cfg.useNginx then "localhost" else cfg.host; + in { description = "My Java Service"; wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; @@ -59,13 +67,22 @@ let startLimitBurst = 10; serviceConfig = { ExecStart = - "${pkgs.jdk}/bin/java -jar ${package}/bin/priceGridApp.jar -p ${toString cfg.port} --host ${cfg.host}"; + "${pkgs.jdk}/bin/java -jar ${package}/bin/priceGridApp.jar -p ${ + toString cfg.port + } --host ${serverHost}"; WorkingDirectory = "${package}/bin"; Restart = "on-failure"; User = "price-grid-app-user"; Group = "price-grid-app-group"; }; }; + + services.nginx = lib.mkIf cfg.useNginx { + recommendedTlsSettings = true; + virtualHosts.${cfg.host} = { + locations."/".proxyPass = "http://localhost:${cfg.port}"; + }; + }; }; }; in {