{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}

module Admin.Server
  ( adminApp
  , serveAdmin
  ) where

import Admin.API
import Admin.Components
import Admin.Server.Middlewares (middlewares)
import Data.ByteString.Char8 (ByteString)
import Data.Version
import Servant

adminApp :: Components a names api => a -> [ByteString] -> Application
adminApp :: a -> [ByteString] -> Application
adminApp a
c =
  let p :: Proxy (AdminAPI api)
p = (forall api. Proxy (AdminAPI api)
forall k (t :: k). Proxy t
Proxy :: Proxy (AdminAPI api))
   in a -> Proxy (AdminAPI api) -> [ByteString] -> Application
forall a (names :: [Symbol]) api.
Components a names api =>
a -> Proxy (AdminAPI api) -> [ByteString] -> Application
adminApp' a
c Proxy (AdminAPI api)
forall api. Proxy (AdminAPI api)
p

adminApp' ::
     Components a names api
  => a
  -> Proxy (AdminAPI api)
  -> [ByteString]
  -> Application
adminApp' :: a -> Proxy (AdminAPI api) -> [ByteString] -> Application
adminApp' a
c Proxy (AdminAPI api)
proxy [ByteString]
tokens = [ByteString] -> Application -> Application
withMiddlewares [ByteString]
tokens (Application -> Application) -> Application -> Application
forall a b. (a -> b) -> a -> b
$ Proxy (AdminAPI api) -> Server (AdminAPI api) -> Application
forall api.
HasServer api '[] =>
Proxy api -> Server api -> Application
serve Proxy (AdminAPI api)
proxy (Server (AdminAPI api) -> Application)
-> Server (AdminAPI api) -> Application
forall a b. (a -> b) -> a -> b
$ a -> Server (AdminAPI api)
forall a (names :: [Symbol]) api.
Components a names api =>
a -> Server (AdminAPI api)
serveAdmin a
c

withMiddlewares :: [ByteString] -> Application -> Application
withMiddlewares :: [ByteString] -> Application -> Application
withMiddlewares = [ByteString] -> Application -> Application
middlewares

serveAdmin :: Components a names api => a -> Server (AdminAPI api)
serveAdmin :: a -> Server (AdminAPI api)
serveAdmin a
c = Handler NoContent :<|> Handler Version
handleRoot (Handler NoContent :<|> Handler Version)
-> (Handler [ComponentDescription] :<|> ServerT api Handler)
-> (Handler NoContent :<|> Handler Version)
   :<|> (Handler [ComponentDescription] :<|> ServerT api Handler)
forall a b. a -> b -> a :<|> b
:<|> a -> Server (ComponentsAPI api)
forall a (names :: [Symbol]) apis.
Components a names apis =>
a -> Server (ComponentsAPI apis)
serveComponents a
c

serveComponents :: Components a names apis => a -> Server (ComponentsAPI apis)
serveComponents :: a -> Server (ComponentsAPI apis)
serveComponents a
c = [ComponentDescription] -> Handler [ComponentDescription]
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> [ComponentDescription]
forall a (names :: [Symbol]) apis.
Components a names apis =>
a -> [ComponentDescription]
describe a
c) Handler [ComponentDescription]
-> ServerT apis Handler
-> Handler [ComponentDescription] :<|> ServerT apis Handler
forall a b. a -> b -> a :<|> b
:<|> a -> ServerT apis Handler
forall a (names :: [Symbol]) apis.
Components a names apis =>
a -> Server apis
serveAll' a
c

handleRoot :: Handler NoContent :<|> Handler Version
handleRoot = NoContent -> Handler NoContent
forall (m :: * -> *) a. Monad m => a -> m a
return NoContent
NoContent Handler NoContent
-> Handler Version -> Handler NoContent :<|> Handler Version
forall a b. a -> b -> a :<|> b
:<|> Version -> Handler Version
forall (m :: * -> *) a. Monad m => a -> m a
return ([Int] -> Version
makeVersion [Int
1])