From 0c3e2c500d181c673f0563e6f5bee49729f49b73 Mon Sep 17 00:00:00 2001
From: stuebinm
Date: Fri, 10 Jun 2022 21:56:50 +0200
Subject: add tracktrain test deployment

---
 chaski/configuration.nix           |  2 +-
 chaski/services/VaaS/.gitignore    |  1 -
 chaski/services/VaaS/CHANGELOG.md  |  5 ----
 chaski/services/VaaS/app/Main.hs   | 58 --------------------------------------
 chaski/services/VaaS/default.nix   | 30 --------------------
 chaski/services/VaaS/haskell.cabal | 35 -----------------------
 chaski/services/VaaS/index.html    | 46 ------------------------------
 chaski/services/gtfs.nix           | 38 -------------------------
 chaski/services/tracktrain.nix     | 30 ++++++++++++++++++++
 flake.lock                         |  4 +--
 10 files changed, 33 insertions(+), 216 deletions(-)
 delete mode 100644 chaski/services/VaaS/.gitignore
 delete mode 100644 chaski/services/VaaS/CHANGELOG.md
 delete mode 100644 chaski/services/VaaS/app/Main.hs
 delete mode 100644 chaski/services/VaaS/default.nix
 delete mode 100644 chaski/services/VaaS/haskell.cabal
 delete mode 100644 chaski/services/VaaS/index.html
 delete mode 100644 chaski/services/gtfs.nix
 create mode 100644 chaski/services/tracktrain.nix

diff --git a/chaski/configuration.nix b/chaski/configuration.nix
index 4c9ef7e..88d617b 100644
--- a/chaski/configuration.nix
+++ b/chaski/configuration.nix
@@ -13,7 +13,7 @@
       ./services/uplcg.nix
       ./services/woitb.nix
       ./services/geolocation.nix
-      ./services/gtfs.nix
+      ./services/tracktrain.nix
     ];
 
   networking.firewall.allowedTCPPorts = [ 80 443 ];
diff --git a/chaski/services/VaaS/.gitignore b/chaski/services/VaaS/.gitignore
deleted file mode 100644
index b5e3679..0000000
--- a/chaski/services/VaaS/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-dist-newstyle/*
diff --git a/chaski/services/VaaS/CHANGELOG.md b/chaski/services/VaaS/CHANGELOG.md
deleted file mode 100644
index 500a0d0..0000000
--- a/chaski/services/VaaS/CHANGELOG.md
+++ /dev/null
@@ -1,5 +0,0 @@
-# Revision history for haskell
-
-## 0.1.0.0 -- YYYY-mm-dd
-
-* First version. Released on an unsuspecting world.
diff --git a/chaski/services/VaaS/app/Main.hs b/chaski/services/VaaS/app/Main.hs
deleted file mode 100644
index b5697d7..0000000
--- a/chaski/services/VaaS/app/Main.hs
+++ /dev/null
@@ -1,58 +0,0 @@
-{-# LANGUAGE LambdaCase        #-}
-{-# LANGUAGE OverloadedStrings #-}
-
-module Main where
-
-import           Control.Exception        (try)
-import           Control.Exception.Base   (handle)
-import qualified Data.ByteString          as BS
-import           Data.ByteString.Base32
-import qualified Data.ByteString.Char8    as C8
-import qualified Data.ByteString.Lazy     as LB
-import           Data.Maybe               (mapMaybe)
-import           Network.HTTP.Client      (httpLbs, newManager, parseRequest,
-                                           responseBody)
-import           Network.HTTP.Client.TLS
-import           Network.HTTP.Types
-import           Network.Wai
-import           Network.Wai.Handler.Warp (run)
-import qualified System.Environment       as SE
-import           System.Process
-
-simpleResponse = responseLBS status200 [("Content-Type", "text/plain")]
-
-simpleError = responseLBS status400 [("Content-Type", "text/plain")]
-
-serveFile filename = do
-  content <- LB.readFile filename
-  pure $ responseLBS status200 [("Content-Type", "text/html")] content
-
-app :: FilePath -> FilePath -> Application
-app validator index req respond =
-    case requestMethod req of
-      "GET" -> case pathInfo req of
-        [] -> serveFile index >>= respond
-        ["validate"] -> do
-          let gtfsuri = head $ mapMaybe (\case { ("gtfs",a) -> Just a; _ -> Nothing }) $ queryString req
-          putStrLn $ "uri is " <> show gtfsuri
-          case gtfsuri of
-            Just uri -> do
-              man <- newManager tlsManagerSettings
-              request <- parseRequest $ C8.unpack uri
-              gtfs <- httpLbs request man
-              let filename = "/tmp/" <> C8.unpack (encodeBase32' uri) <> ".zip"
-              LB.writeFile filename (responseBody gtfs)
-              readProcessWithExitCode "python" [validator,"-n", filename, "--output", "/tmp/gtfs-validated.html"] ""
-
-              serveFile "/tmp/gtfs-validated.html" >>= respond
-
-            Nothing -> respond $ simpleError "missing gtfs parameter"
-        _ -> respond $ simpleError "unknown path"
-      _ -> respond $ simpleError "invalid reqeust method"
-
-main :: IO ()
-main = do
-  args <- SE.getArgs
-  let validator = head args
-  putStrLn "http://localhost:7000/"
-  run 7000 $ app validator (args!!1)
diff --git a/chaski/services/VaaS/default.nix b/chaski/services/VaaS/default.nix
deleted file mode 100644
index 427270c..0000000
--- a/chaski/services/VaaS/default.nix
+++ /dev/null
@@ -1,30 +0,0 @@
-{ pkgs, compiler ? "default", doBenchmark ? false }:
-
-let
-
-  inherit pkgs;
-
-  f = { mkDerivation, base, base32, bytestring, http-client
-      , http-client-tls, http-types, lib, process, wai, warp
-      }:
-      mkDerivation {
-        pname = "VaaS";
-        version = "0.1.0.0";
-        src = ./.;
-        isLibrary = false;
-        isExecutable = true;
-        executableHaskellDepends = [
-          base base32 bytestring http-client http-client-tls http-types
-          process wai warp
-        ];
-        license = "unknown";
-        hydraPlatforms = lib.platforms.none;
-      };
-
-  haskellPackages = if compiler == "default"
-                       then pkgs.haskellPackages
-                       else pkgs.haskell.packages.${compiler};
-
-  variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;
-in
-  variant (haskellPackages.callPackage f {})
diff --git a/chaski/services/VaaS/haskell.cabal b/chaski/services/VaaS/haskell.cabal
deleted file mode 100644
index 262b65f..0000000
--- a/chaski/services/VaaS/haskell.cabal
+++ /dev/null
@@ -1,35 +0,0 @@
-cabal-version:      2.4
-name:               VaaS
-version:            0.1.0.0
-
--- A short (one-line) description of the package.
--- synopsis:
-
--- A longer description of the package.
--- description:
-
--- A URL where users can report bugs.
--- bug-reports:
-
--- The license under which the package is released.
--- license:
-author:             stuebinm
-maintainer:         stuebinm@disroot.org
-
--- A copyright notice.
--- copyright:
--- category:
-extra-source-files: CHANGELOG.md
-
-executable VaaS
-    main-is:          Main.hs
-
-    -- Modules included in this executable, other than Main.
-    -- other-modules:
-
-    -- LANGUAGE extensions used by modules in this package.
-    -- other-extensions:
-    build-depends:    base ^>=4.14.1.0, wai, warp, http-types, bytestring, process,
-                      http-client, http-client-tls, base32
-    hs-source-dirs:   app
-    default-language: Haskell2010
diff --git a/chaski/services/VaaS/index.html b/chaski/services/VaaS/index.html
deleted file mode 100644
index a7f59e0..0000000
--- a/chaski/services/VaaS/index.html
+++ /dev/null
@@ -1,46 +0,0 @@
-<!doctype html>
-<html class="no-js" lang="">
-    <head>
-        <meta charset="utf-8">
-        <meta http-equiv="x-ua-compatible" content="ie=edge">
-        <title>GTFS Validator</title>
-        <meta name="description" content="">
-        <meta name="viewport" content="width=device-width, initial-scale=1">
-
-        <link rel="apple-touch-icon" href="/apple-touch-icon.png">
-        <!-- Place favicon.ico in the root directory -->
-
-    </head>
-    <body>
-        <!--[if lt IE 8]>
-            <p class="browserupgrade">
-            You are using an <strong>outdated</strong> browser. Please
-            <a href="http://browsehappy.com/">upgrade your browser</a> to improve
-            your experience.
-            </p>
-        <![endif]-->
-
-        <h1>GTFS Validator</h1>
-        <p>
-            This runs the validator contained in the
-            <a href="https://github.com/google/transitfeed">transitfeed
-                git repository
-            </a>.
-        </p>
-
-        <p>Paste the url to your GTFS zip below</p>
-
-        <input id="url">
-        <button id="submit">Validate</button>
-
-        <script>
-         let submit = document.getElementById("submit");
-         let url = document.getElementById("url");
-
-         submit.onclick = () => {
-             window.location =
-                 "/validate?gtfs=" + url.value
-         }
-        </script>
-    </body>
-</html>
diff --git a/chaski/services/gtfs.nix b/chaski/services/gtfs.nix
deleted file mode 100644
index 61837a1..0000000
--- a/chaski/services/gtfs.nix
+++ /dev/null
@@ -1,38 +0,0 @@
-{ config, lib, pkgs, ... }:
-
-let
-  vaas = import ./VaaS/default.nix {inherit pkgs; };
-
-  transitfeed = pkgs.fetchFromGitHub {
-    owner = "google";
-    repo = "transitfeed";
-    rev = "d727e97cb66ac2ca2d699a382ea1d449ee26c2a1";
-    sha256 = "0kmcmdja6h7gzvi40c9qfzxh6qwv5184g2rgpqx5rhj4ql9ini3h";
-  };
-
-  index = pkgs.copyPathToStore ./VaaS/index.html;
-in
-{
-  systemd.services.vaas = {
-    enable = true;
-    wantedBy = [ "multi-user.target" ];
-    after = [ "network.target" ];
-    description = "GTFS feed validator as a service";
-    path = [ pkgs.python2 pkgs.python2Packages.pytz pkgs.python2Packages.enum34 ];
-    serviceConfig = {
-      ExecStart = "${vaas}/bin/VaaS ${transitfeed}/feedvalidator.py ${index}";
-      Restart = "always";
-      PrivateTmp = "true";
-      PrivateDevices = "true";
-      ProtectSystem = "strict";
-      ReadWritePaths = [ "/tmp" ];
-    };
-
-  };
-
-  services.nginx.virtualHosts."gtfs.stuebinm.eu" = {
-    enableACME = true;
-    forceSSL = true;
-    locations."/".proxyPass = "http://localhost:7000";
-  };
-}
diff --git a/chaski/services/tracktrain.nix b/chaski/services/tracktrain.nix
new file mode 100644
index 0000000..6f9acb9
--- /dev/null
+++ b/chaski/services/tracktrain.nix
@@ -0,0 +1,30 @@
+{ config, lib, pkgs, inputs, ... }:
+
+{
+  services.nginx.virtualHosts."tracktrain.stuebinm.eu" = {
+    locations."/" = {
+      proxyPass = "http://localhost:4000";
+      proxyWebsockets = true;
+    };
+    enableACME = true;
+    forceSSL = true;
+  };
+
+  networking.firewall.allowedTCPPorts = [ 443 ];
+
+  systemd.services.tracktrain = {
+    enable = true;
+
+    description = "tracks trains, hopefully";
+    wantedBy = [ "multi-user.target" ];
+    after = [ "network.target" ];
+    serviceConfig.Type = "simple";
+    path = [ pkgs.wget ];
+    script = ''
+      cd /tmp
+      wget "https://ilztalbahn.eu/wp-content/uploads/2020/07/gtfs.zip"
+      ${import inputs.tracktrain {nixpkgs = pkgs;}}/bin/haskell-gtfs
+    '';
+    startAt = "daily";
+  };
+}
diff --git a/flake.lock b/flake.lock
index b5dffd6..441ee32 100644
--- a/flake.lock
+++ b/flake.lock
@@ -285,9 +285,9 @@
       "flake": false,
       "locked": {
         "lastModified": 1641709913,
-        "narHash": "sha256-zIc6Or0n2klhycD71d0T+FaB4asAevK5XkpnVQviO3A=",
+        "narHash": "sha256-n3c6wOmlKL7X7ALLAnQYGyGNlVUrDVpUFY2tBQRV4Vs=",
         "ref": "main",
-        "rev": "973f086e1882dfaa48f40a91b5f8c679076b6fb6",
+        "rev": "2784c11ea13cad29d0407bcb64fde7cef5e9c360",
         "revCount": 5,
         "type": "git",
         "url": "https://stuebinm.eu/git/tracktrain"
-- 
cgit v1.2.3