11{-# LANGUAGE DeriveDataTypeable #-}
22{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
33{-# HLINT ignore "Use newtype instead of data" #-}
4+ {-# LANGUAGE TypeApplications #-}
45module Main where
56
67
@@ -12,31 +13,91 @@ import Cardano.Kuber.Util (timestampToSlot)
1213import Data.Text (stripStart )
1314import Data.Data (Data )
1415import Data.Typeable (Typeable )
15- import System.Console.CmdArgs
1616import Text.Read (readMaybe )
1717import Data.String (IsString (.. ))
1818import System.IO
19+ import qualified Data.ByteString.Lazy.Char8 as L8
20+ import Network.HTTP.Simple (httpLBS , HttpException (HttpExceptionRequest ))
21+ import Network.HTTP.Client.Conduit (Response (responseStatus , responseBody ), HttpException (HttpExceptionRequest , InvalidUrlException ), Request (requestBody ))
22+ import System.Exit (exitFailure )
23+ import Network.HTTP.Types (status200 )
24+ import Control.Exception (try , catch )
25+ import Data.Function ((&) )
1926
27+
28+ import Options.Applicative
29+ import Data.Semigroup ((<>) )
30+
31+ data KuberConfig = KuberConfig
32+ { host :: Maybe String
33+ , port :: Int
34+ , healthCheckUrl :: String
35+ , healthCheck :: Bool
36+ }
37+
38+ sample :: Parser KuberConfig
39+ sample = KuberConfig
40+ <$> option auto(
41+ long " host"
42+ <> short ' H'
43+ <> metavar " IP-Address"
44+ <> help " IP Address to bind to"
45+ <> showDefaultWith (const " Listen on all available intefaces" )
46+ <> value Nothing
47+ )
48+ <*> option auto
49+ ( long " port"
50+ <> short ' p'
51+ <> help " Port to listen on"
52+ <> showDefault
53+ <> value 8081 )
54+ <*> option auto
55+ ( long " url"
56+ <> help " Url for health-check operation"
57+ <> showDefaultWith (const " http://127.0.0.1:8081/api/v1/chaintip" )
58+ <> value " http://127.0.0.1:8081/api/v1/chaintip"
59+ <> metavar " URL" )
60+ <*> switch (
61+ long " healthcheck"
62+ <> help " Perform health-check request on kuber server"
63+ )
64+
65+ opts = info (sample <**> helper)
66+ ( fullDesc
67+ <> progDesc " Kuber Server"
68+ )
2069main :: IO ()
2170main = do
22- hSetBuffering stdout LineBuffering
23- dcinfo <- chainInfoFromEnv >>= withDetails
24- Modes port hostStr <- cmdArgs $ modes [
25- Modes {
26- port = 8081 &= typ " Port" ,
27- host = " *" &= typ " Host"
28- }
29- ]&= program " kuber"
30-
31- let settings = setPort port defaultSettings
32- host = setHost (fromString hostStr) settings
33- putStrLn $ " Starting server on port " ++ show port ++ " ..."
34- runSettings host $ app dcinfo
35- run port $ app dcinfo
36-
37- data Modes =
38- Modes {
39- port :: Int ,
40- host :: String
41- }
42- deriving (Show , Data , Typeable )
71+ KuberConfig hostStr port healthCheckUrl doHealthCheck <- execParser opts
72+
73+ if doHealthCheck
74+ then
75+ performRequest healthCheckUrl
76+
77+ else do
78+ dcinfo <- chainInfoFromEnv >>= withDetails
79+
80+ let settings = setPort port defaultSettings
81+ let settings2 = (case hostStr of
82+ Nothing -> settings
83+ Just s -> setHost (fromString s) settings )
84+ putStrLn $ " Starting server on port " ++ show port ++ " ..."
85+ runSettings settings2 $ app dcinfo
86+ run port $ app dcinfo
87+
88+ performRequest :: String -> IO ()
89+ performRequest url = do
90+ res <- catch (httpLBS (fromString url)) exceptionHandler
91+ if responseStatus res /= status200
92+ then do
93+ putStr $ " Response " ++ show (responseStatus res) ++ " : "
94+ L8. putStr $ responseBody res
95+ exitFailure
96+ else L8. putStr $ responseBody res
97+ where
98+ exceptionHandler :: HttpException -> IO a
99+ exceptionHandler ex = do
100+ case ex of
101+ HttpExceptionRequest re hec -> putStr (url ++ " : " ++ show hec)
102+ InvalidUrlException s str -> putStr $ str ++ " : " ++ s
103+ exitFailure
0 commit comments