2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
module Servant.API.Sub where
|
|
|
|
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.String.Conversions
|
|
|
|
import GHC.TypeLits
|
|
|
|
import Network.Wai
|
|
|
|
import Servant.Client
|
|
|
|
import Servant.Server
|
|
|
|
|
|
|
|
-- | The contained API (second argument) can be found under @("/" ++ path)@
|
|
|
|
-- (path being the first argument).
|
|
|
|
data (path :: k) :> a = Proxy path :> a
|
|
|
|
infixr 9 :>
|
|
|
|
|
|
|
|
instance (KnownSymbol path, HasServer sublayout) => HasServer (path :> sublayout) where
|
|
|
|
type Server (path :> sublayout) = Server sublayout
|
2014-10-27 11:24:20 +01:00
|
|
|
route Proxy subserver globalPathInfo request respond = case pathInfo request of
|
2014-10-25 01:27:39 +02:00
|
|
|
(first : rest)
|
|
|
|
| first == cs (symbolVal proxyPath)
|
2014-10-27 11:24:20 +01:00
|
|
|
-> route (Proxy :: Proxy sublayout) subserver globalPathInfo request{
|
2014-10-25 01:27:39 +02:00
|
|
|
pathInfo = rest
|
2014-10-27 11:24:20 +01:00
|
|
|
} respond
|
|
|
|
_ -> respond Nothing
|
2014-10-25 01:27:39 +02:00
|
|
|
|
|
|
|
where proxyPath = Proxy :: Proxy path
|
|
|
|
|
|
|
|
instance (KnownSymbol path, HasClient sublayout) => HasClient (path :> sublayout) where
|
|
|
|
type Client (path :> sublayout) = Client sublayout
|
|
|
|
|
|
|
|
clientWithRoute Proxy req =
|
|
|
|
clientWithRoute (Proxy :: Proxy sublayout) $
|
|
|
|
appendToPath p req
|
|
|
|
|
|
|
|
where p = symbolVal (Proxy :: Proxy path)
|
|
|
|
|