Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
[CAD-2074] Health check endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksaric committed Oct 22, 2020
1 parent c0d9e1b commit 93a29b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Data.IORef (newIORef)
import Data.Swagger (Info (..), Swagger (..))
import Data.Time (UTCTime, addUTCTime,
getCurrentTime, nominalDay)
import Data.Version (showVersion)

import Network.Wai.Handler.Warp (defaultSettings, runSettings,
setBeforeMainLoop, setPort)
Expand All @@ -43,38 +44,47 @@ import Servant.Swagger
import DB
import Types

import Paths_smash (version)


-- |For api versioning.
type APIVersion = "v1"

-- | Shortcut for common api result types.
type ApiRes verb a = verb '[JSON] (ApiResult DBFail a)

-- The basic auth.
type BasicAuthURL = BasicAuth "smash" User

-- GET api/v1/status
type HealthStatusAPI = "api" :> APIVersion :> "status" :> ApiRes Get HealthStatus

-- GET api/v1/metadata/{hash}
type OfflineMetadataAPI = "api" :> "v1" :> "metadata" :> Capture "id" PoolId :> Capture "hash" PoolMetadataHash :> Get '[JSON] (Headers '[Header "Cache" Text] (ApiResult DBFail PoolMetadataWrapped))
type OfflineMetadataAPI = "api" :> APIVersion :> "metadata" :> Capture "id" PoolId :> Capture "hash" PoolMetadataHash :> Get '[JSON] (Headers '[Header "Cache" Text] (ApiResult DBFail PoolMetadataWrapped))

-- GET api/v1/delisted
type DelistedPoolsAPI = "api" :> "v1" :> "delisted" :> ApiRes Get [PoolId]
type DelistedPoolsAPI = "api" :> APIVersion :> "delisted" :> ApiRes Get [PoolId]

-- GET api/v1/errors
type FetchPoolErrorAPI = "api" :> "v1" :> "errors" :> Capture "poolId" PoolId :> QueryParam "fromDate" TimeStringFormat :> ApiRes Get [PoolFetchError]
type FetchPoolErrorAPI = "api" :> APIVersion :> "errors" :> Capture "poolId" PoolId :> QueryParam "fromDate" TimeStringFormat :> ApiRes Get [PoolFetchError]

#ifdef DISABLE_BASIC_AUTH
-- POST api/v1/delist
type DelistPoolAPI = "api" :> "v1" :> "delist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
type DelistPoolAPI = "api" :> APIVersion :> "delist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId

type EnlistPoolAPI = "api" :> "v1" :> "enlist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
type EnlistPoolAPI = "api" :> APIVersion :> "enlist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
#else
type DelistPoolAPI = BasicAuthURL :> "api" :> "v1" :> "delist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
type DelistPoolAPI = BasicAuthURL :> "api" :> APIVersion :> "delist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId

type EnlistPoolAPI = BasicAuthURL :> "api" :> "v1" :> "enlist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
type EnlistPoolAPI = BasicAuthURL :> "api" :> APIVersion :> "enlist" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
#endif

type RetiredPoolsAPI = "api" :> "v1" :> "retired" :> ApiRes Get [PoolId]
type RetiredPoolsAPI = "api" :> APIVersion :> "retired" :> ApiRes Get [PoolId]


-- The full API.
type SmashAPI = OfflineMetadataAPI
:<|> HealthStatusAPI
:<|> DelistedPoolsAPI
:<|> DelistPoolAPI
:<|> EnlistPoolAPI
Expand All @@ -83,7 +93,7 @@ type SmashAPI = OfflineMetadataAPI
#ifdef TESTING_MODE
:<|> RetirePoolAPI

type RetirePoolAPI = "api" :> "v1" :> "retired" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
type RetirePoolAPI = "api" :> APIVersion :> "retired" :> ReqBody '[JSON] PoolId :> ApiRes Patch PoolId
#endif


Expand Down Expand Up @@ -238,6 +248,7 @@ server :: Configuration -> DataLayer -> Server API
server configuration dataLayer
= return todoSwagger
:<|> getPoolOfflineMetadata dataLayer
:<|> getHealthStatus
:<|> getDelistedPools dataLayer
:<|> delistPool dataLayer
:<|> enlistPool dataLayer
Expand Down Expand Up @@ -284,6 +295,13 @@ getPoolOfflineMetadata dataLayer poolId poolHash = fmap (addHeader "always") . c
then return . ApiResult . Right $ PoolMetadataWrapped poolMetadata
else throwIO err404

-- |Simple health status, there are ideas for improvement.
getHealthStatus :: Handler (ApiResult DBFail HealthStatus)
getHealthStatus = return . ApiResult . Right $
HealthStatus
{ hsStatus = "OK"
, hsVersion = toS $ showVersion version
}

-- |Get all delisted pools
getDelistedPools :: DataLayer -> Handler (ApiResult DBFail [PoolId])
Expand Down
24 changes: 24 additions & 0 deletions src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Types
, createPoolOfflineMetadata
, examplePoolOfflineMetadata
-- * Configuration
, HealthStatus (..)
, Configuration (..)
, defaultConfiguration
-- * API
Expand Down Expand Up @@ -332,6 +333,29 @@ instance FromHttpApiData TimeStringFormat where
instance ToParamSchema TimeStringFormat where
toParamSchema _ = mempty

-- |The data for returning the health check for SMASH.
data HealthStatus = HealthStatus
{ hsStatus :: !Text
, hsVersion :: !Text
} deriving (Eq, Show, Generic)

instance ToJSON HealthStatus where
toJSON (HealthStatus hsStatus' hsVersion') =
object
[ "status" .= hsStatus'
, "version" .= hsVersion'
]

instance FromJSON HealthStatus where
parseJSON = withObject "healthStatus" $ \o -> do
status <- o .: "status"
version <- o .: "version"

return $ HealthStatus
{ hsStatus = status
, hsVersion = version
}

-- We need a "conversion" layer between custom DB types and the rest of the
-- codebase se we can have a clean separation and replace them at any point.
-- The natural place to have this conversion is in the types.
Expand Down

0 comments on commit 93a29b4

Please sign in to comment.