{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

module Admin.Component.Health
  ( HealthAPI
  , HealthComponent
  , health
  , serveHealth
  , Control.Concurrent.Async.async
  , Control.Concurrent.Async.Async
  ) where

import Admin.Components
import Control.Concurrent.Async (Async, async)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.ApplicationState
import Data.Version (makeVersion)
import Servant.API

type HealthAPI = "status" :> Get '[ JSON] ApplicationState

type HealthComponent = Component "health" HealthAPI

health :: (Show a) => Async a -> HealthComponent
health :: Async a -> HealthComponent
health Async a
as = Component :: forall (name :: Symbol) api.
Server api -> Version -> Component name api
Component {server :: Server HealthAPI
server = Async a -> Handler ApplicationState
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
Async a -> m ApplicationState
serveHealth Async a
as, version :: Version
version = [Int] -> Version
makeVersion [Int
1]}

serveHealth :: (MonadIO m, Show a) => Async a -> m ApplicationState
serveHealth :: Async a -> m ApplicationState
serveHealth Async a
as = IO ApplicationState -> m ApplicationState
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ApplicationState -> m ApplicationState)
-> IO ApplicationState -> m ApplicationState
forall a b. (a -> b) -> a -> b
$ Async a -> IO ApplicationState
forall a. Show a => Async a -> IO ApplicationState
stateOf Async a
as