40 lines
1.1 KiB
Haskell
40 lines
1.1 KiB
Haskell
|
{-# 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
|
||
|
route Proxy subserver request = case pathInfo request of
|
||
|
(first : rest)
|
||
|
| first == cs (symbolVal proxyPath)
|
||
|
-> route (Proxy :: Proxy sublayout) subserver request{
|
||
|
pathInfo = rest
|
||
|
}
|
||
|
_ -> return Nothing
|
||
|
|
||
|
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)
|
||
|
|