-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API type for http monitoring local-cluster
- Loading branch information
Showing
3 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
lib/local-cluster/lib/Cardano/Wallet/Launch/Cluster/Monitoring/Http/API.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{-# 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 {unApiT :: 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters