about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-08-23T11·03+0100
committerVincent Ambo <tazjin@google.com>2019-08-23T11·03+0100
commitbe074c60850a8bd6197e627c131d73e34d8640be (patch)
tree9d79a8e7e86807144f598cc509cdcfbc70a117b3
parentfb930e4db76fa71349dfeea78bdf34fb45b574c1 (diff)
refactor(tazblog): Move blog configuration to envvars r/55
The port and resource directory are now specified via environment
variables and a wrapper script is created by Nix that sets the
resource path and so on correctly.
-rw-r--r--services/tazblog/blog/Main.hs39
-rw-r--r--services/tazblog/default.nix7
2 files changed, 24 insertions, 22 deletions
diff --git a/services/tazblog/blog/Main.hs b/services/tazblog/blog/Main.hs
index 246f1325e7fc..2842d1ee1b72 100644
--- a/services/tazblog/blog/Main.hs
+++ b/services/tazblog/blog/Main.hs
@@ -1,28 +1,25 @@
 -- | Main module for the blog's web server
 module Main where
 
-import           Control.Applicative (pure, (<*>))
-import           Control.Exception   (bracket)
-import           Data.Word           (Word16)
-import           Locales             (version)
-import           Network             (HostName, PortID (..))
-import           Options
-import           Server
+import Control.Applicative ((<$>), (<*>))
+import Locales (version)
+import Server (runBlog)
+import System.Environment (getEnv)
 
-data MainOptions = MainOptions {
-  blogPort    :: Int,
-  resourceDir :: String
-}
+data MainOptions
+  = MainOptions
+      { blogPort :: Int,
+        resourceDir :: String
+        }
 
-instance Options MainOptions where
-  defineOptions = pure MainOptions
-    <*> simpleOption "blogPort" 8000
-        "Port to serve the blog on. Default is 8000."
-    <*> simpleOption "resourceDir" "/opt/tazblog/static"
-        "Resources folder location."
+readOpts :: IO MainOptions
+readOpts =
+  MainOptions
+    <$> (fmap read $ getEnv "PORT")
+    <*> getEnv "RESOURCE_DIR"
 
-main :: IO()
+main :: IO ()
 main = do
-    putStrLn ("TazBlog " ++ version ++ " in Haskell starting")
-    runCommand $ \opts _ ->
-      runBlog (blogPort opts) (resourceDir opts)
+  putStrLn ("TazBlog " ++ version ++ " in Haskell starting")
+  opts <- readOpts
+  runBlog (blogPort opts) (resourceDir opts)
diff --git a/services/tazblog/default.nix b/services/tazblog/default.nix
index 3999f2b278cd..b1a1f95587bc 100644
--- a/services/tazblog/default.nix
+++ b/services/tazblog/default.nix
@@ -4,4 +4,9 @@
 
 { pkgs ? import <nixpkgs> {} }:
 
-pkgs.haskell.packages.ghc865.callPackage ./tazblog.nix {}
+let tazblog = pkgs.haskell.packages.ghc865.callPackage ./tazblog.nix {};
+in pkgs.writeShellScriptBin "tazblog" ''
+  export PORT=8000
+  export RESOURCE_DIR=${./static}
+  exec ${tazblog}/bin/tazblog
+''