]> www.average.org Git - pulsecounter.git/commitdiff
add primitive config to query.cgi
authorEugene Crosser <crosser@average.org>
Mon, 21 Dec 2015 17:41:19 +0000 (20:41 +0300)
committerEugene Crosser <crosser@average.org>
Mon, 21 Dec 2015 17:41:19 +0000 (20:41 +0300)
linux/query.cgi

index 3ebf5ce44bf7aa5c6e93f16e9fa1f53a913a8b61..0bab4e1bef48cc350720b3ca4ab89151b2685317 100644 (file)
@@ -1,4 +1,5 @@
 #!/bin/env runhaskell
 #!/bin/env runhaskell
+
 {-# LANGUAGE OverloadedStrings #-}
 
 module Main where
 {-# LANGUAGE OverloadedStrings #-}
 
 module Main where
@@ -15,9 +16,11 @@ main = runCGI $ handleErrors cgiMain
 
 cgiMain :: CGI CGIResult
 cgiMain = do
 
 cgiMain :: CGI CGIResult
 cgiMain = do
-  conn <- liftIO $ connect defaultConnectInfo { connectUser = "watermeter"
-                                              , connectPassword = "xxxxxxxx"
-                                              , connectDatabase = "watermeter"
+  conf <- liftIO $ readConf "/etc/watermeter.db"
+  conn <- liftIO $ connect defaultConnectInfo { connectHost = host conf
+                                             , connectUser = user conf
+                                              , connectPassword = pass conf
+                                              , connectDatabase = dbnm conf
                                               }
   today <- liftIO getClockTime
   let tomorrow = addToClockTime (noTimeDiff {tdDay = 1}) today
                                               }
   today <- liftIO getClockTime
   let tomorrow = addToClockTime (noTimeDiff {tdDay = 1}) today
@@ -58,3 +61,24 @@ cgiMain = do
 showjson :: [(Int, Double)] -> String
 showjson l = intercalate "," $ map (\(t, c) -> "[" ++ show t ++ "," ++ show (floor c) ++ "]") l
 
 showjson :: [(Int, Double)] -> String
 showjson l = intercalate "," $ map (\(t, c) -> "[" ++ show t ++ "," ++ show (floor c) ++ "]") l
 
+data Conf = Conf { host :: String
+                , user :: String
+                , pass :: String
+                , dbnm :: String
+                }
+
+readConf :: String -> IO Conf
+readConf fn =
+  readFile fn >>= return . (foldr parseLine (Conf "" "" "" "")) . lines
+  where
+    parseLine :: String -> Conf -> Conf
+    parseLine l sum =
+      case words l of
+        [k, v] ->
+          case k of
+            "host" -> sum { host = v }
+            "user" -> sum { user = v }
+            "password" -> sum { pass = v }
+            "database" -> sum { dbnm = v }
+            _ -> error $ "bad key in config line \"" ++ l ++ "\""
+        _ -> error $ "bad config line \"" ++ l ++ "\""