Skip to content

Commit

Permalink
Add API type for http monitoring local-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 29, 2024
1 parent e42e113 commit eb60e31
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeOperators #-}

module Cardano.Wallet.Launch.Cluster.Monitoring.Http.API
( API
, ApiT (..)
)
where

import Prelude

import Cardano.Wallet.Launch.Cluster.Monitoring.Phase
( History (..)
)
import Control.Monitoring.Tracing
( MonitorState (..)
)
import Data.Aeson
( FromJSON (..)
, ToJSON
, Value (..)
, object
, toJSON
, withObject
, (.:)
, (.=)
)
import Servant
( Post
, PostNoContent
)
import Servant.API
( Get
, JSON
, (:<|>)
, (:>)
)

-- | The API for the monitoring server
type API =
"ready" :> Get '[JSON] Bool
:<|> "step" :> PostNoContent
:<|> "switch" :> Post '[JSON] (ApiT MonitorState)
:<|> "observe" :> Get '[JSON] (ApiT (History, MonitorState))

-- | A newtype wrapper to avoid orphan instances
newtype ApiT a = ApiT a
deriving newtype (Eq, Show)

instance ToJSON (ApiT MonitorState) where
toJSON = \case
ApiT Wait -> String "waiting"
ApiT Step -> String "stepping"
ApiT Run -> String "running"

instance FromJSON (ApiT MonitorState) where
parseJSON = \case
String "waiting" -> pure $ ApiT Wait
String "stepping" -> pure $ ApiT Step
String "running" -> pure $ ApiT Run
_ -> fail "Invalid state"

instance ToJSON (ApiT (History, MonitorState)) where
toJSON (ApiT (History{history}, state)) =
object
[ "phases" .= history
, "state" .= ApiT state
]

instance FromJSON (ApiT (History, MonitorState)) where
parseJSON = withObject "ApiT (History, MonitorState)" $ \o -> do
history <- o .: "phases"
ApiT state <- o .: "state"
pure $ ApiT (History{history}, state)
3 changes: 3 additions & 0 deletions lib/local-cluster/local-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ library
Cardano.Wallet.Launch.Cluster.KeyRegistration
Cardano.Wallet.Launch.Cluster.Logging
Cardano.Wallet.Launch.Cluster.MonetaryPolicyScript
Cardano.Wallet.Launch.Cluster.Monitoring.Http.API
Cardano.Wallet.Launch.Cluster.Monitoring.Phase
Cardano.Wallet.Launch.Cluster.Node.GenNodeConfig
Cardano.Wallet.Launch.Cluster.Node.GenTopology
Expand Down Expand Up @@ -120,8 +121,10 @@ library
, profunctors
, resourcet
, retry
, servant
, servant-client
, stm
, servant-server
, tagged
, temporary
, temporary-extra
Expand Down

0 comments on commit eb60e31

Please sign in to comment.