Skip to content

Commit ead85f8

Browse files
mesudipSudip Bhattarai
authored andcommitted
Add endpoint for chaintip query
1 parent af5f758 commit ead85f8

8 files changed

Lines changed: 155 additions & 94 deletions

File tree

.ci/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ RUN bash -e /merge-root /layer1 \
1919

2020
FROM node:16 as ui-layer
2121
WORKDIR /app
22+
ENV NODE_OPTIONS=--max_old_space_size=6144
2223
COPY ./playground/package.json ./playground/package-lock.json ./
2324
RUN npm ci
2425
COPY ./playground .
@@ -30,4 +31,5 @@ COPY --from=layer1 / /
3031
WORKDIR /app
3132
COPY --from=ui-layer /app/dist/ .
3233
EXPOSE 8081
34+
HEALTHCHECK --interval=40s --timeout=10s --start-period=30s --retries=2 CMD [ "/bin/kuber" , "--healthcheck" ]
3335
ENTRYPOINT /bin/kuber

playground/package-lock.json

Lines changed: 35 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"start": "vite --host=0.0.0.0",
66
"dev": "vite",
7-
"build": "vue-tsc --noEmit && vite build",
7+
"build": "vite build",
88
"preview": "vite preview --port 5050",
99
"typecheck": "vue-tsc --noEmit"
1010
},

server/app/Main.hs

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DeriveDataTypeable #-}
22
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
33
{-# HLINT ignore "Use newtype instead of data" #-}
4+
{-# LANGUAGE TypeApplications #-}
45
module Main where
56

67

@@ -12,31 +13,91 @@ import Cardano.Kuber.Util (timestampToSlot)
1213
import Data.Text (stripStart)
1314
import Data.Data (Data)
1415
import Data.Typeable (Typeable)
15-
import System.Console.CmdArgs
1616
import Text.Read (readMaybe)
1717
import Data.String (IsString(..))
1818
import 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+
)
2069
main :: IO ()
2170
main = 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

server/kuber-server.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ executable kuber
9797
, cborg
9898
, http-types
9999
, http-media
100+
, http-conduit
100101
, wai-cors
101102
, cardano-binary
102103
, cardano-ledger-core
103104
, kuber-server
104-
, cmdargs >= 0.10.18
105+
, optparse-applicative

0 commit comments

Comments
 (0)