hswaw/sound: add spejsiot-api

Change-Id: If40cdbeadb570d8c33b4c950482c01f4b2bbd1c4
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1446
Reviewed-by: vuko <vuko@hackerspace.pl>
diff --git a/hswaw/machines/sound.waw.hackerspace.pl/configuration.nix b/hswaw/machines/sound.waw.hackerspace.pl/configuration.nix
index 8c0116b..d1a9b4c 100644
--- a/hswaw/machines/sound.waw.hackerspace.pl/configuration.nix
+++ b/hswaw/machines/sound.waw.hackerspace.pl/configuration.nix
@@ -31,6 +31,7 @@
   imports =
     [
       ./hardware-configuration.nix
+			./spejsiot-api.nix
     ];
 
   boot.loader.systemd-boot.enable = true;
@@ -146,6 +147,30 @@
             proxyPass = "http://localhost:8123";
           };
         };
+        "spejsiot.waw.hackerspace.pl" = {
+          serverAliases = ["default_server"];
+          listen = [
+            {
+              addr = networks.lan.ipv4;
+              port = 80;
+              ssl = false;
+            }
+          ];
+          locations."/" = {
+            # TODO copied from iot, are all headers needed?
+            extraConfig = ''
+              proxy_set_header Upgrade $http_upgrade;
+              proxy_set_header Connection $http_connection;
+              proxy_set_header Host $host;
+              proxy_set_header X-Real-IP $remote_addr;
+              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+              proxy_set_header X-Forwarded-Host $host:$server_port;
+              proxy_set_header X-Forwarded-Server $host;
+              proxy_set_header X-Forwarded-Proto $scheme;
+            '';
+            proxyPass = "http://localhost:5100";
+          };
+        };
       };
     };
 
diff --git a/hswaw/machines/sound.waw.hackerspace.pl/spejsiot-api.nix b/hswaw/machines/sound.waw.hackerspace.pl/spejsiot-api.nix
new file mode 100644
index 0000000..217d250
--- /dev/null
+++ b/hswaw/machines/sound.waw.hackerspace.pl/spejsiot-api.nix
@@ -0,0 +1,40 @@
+{ pkgs, workspace, ... }:
+
+let
+  python = pkgs.python3.withPackages (pp: [ pp.jinja2 pp.paho-mqtt pp.flask ]);
+  src = pkgs.fetchgit {
+    url = "https://code.hackerspace.pl/informatic/spejsiot-api";
+    rev = "d5bcb860738df0c9702c2e6f6954cfa08e3c7153";
+    sha256 = "sha256-1xZ6RKl/AkMYvyFDcANQiSY3dJPQitaKjLl9ijrAHOs=";
+  };
+	run = pkgs.writeShellScriptBin "spejsiot-api" ''
+		cd ${src}
+		exec ${python}/bin/python3 main.py
+	'';
+
+  name = "spejsiot-api";
+  user = name;
+  group = name;
+  socket_dir = "/run/${name}/";
+in {
+  users.users."${user}" = {
+    group = "${group}";
+    isSystemUser = true;
+  };
+  users.groups."${group}" = { };
+
+  systemd.services."${name}" = {
+    description = "Hackerspace Checkinator";
+    wantedBy = [ "multi-user.target" ];
+    after = [ "mosquitto.service" ];
+
+    serviceConfig.User = "${user}";
+    serviceConfig.Type = "simple";
+
+    serviceConfig.Restart = "always";
+    serviceConfig.RestartSec = "30";
+
+    serviceConfig.ExecStart = "${run}/bin/spejsiot-api";
+
+  };
+}