git.iko.soy/typeable/servant
on c98c7db help sign in
48 lines 1.5 kB history raw
1{-# LANGUAGE PolyKinds #-}
2{-# LANGUAGE TypeFamilies #-}
3{-# LANGUAGE TypeOperators #-}
4{-# LANGUAGE ScopedTypeVariables #-}
5module Servant.API.Sub where
7import Data.Proxy
8import Data.String.Conversions
9import GHC.TypeLits
10import Network.Wai
11import Servant.Client
12import Servant.Docs
13import Servant.Server
15-- | The contained API (second argument) can be found under @("/" ++ path)@
16-- (path being the first argument).
17data (path :: k) :> a = Proxy path :> a
18infixr 9 :>
20instance (KnownSymbol path, HasServer sublayout) => HasServer (path :> sublayout) where
21 type Server (path :> sublayout) = Server sublayout
22 route Proxy subserver request respond = case pathInfo request of
23 (first : rest)
24 | first == cs (symbolVal proxyPath)
25 -> route (Proxy :: Proxy sublayout) subserver request{
26 pathInfo = rest
27 } respond
28 _ -> respond Nothing
30 where proxyPath = Proxy :: Proxy path
32instance (KnownSymbol path, HasClient sublayout) => HasClient (path :> sublayout) where
33 type Client (path :> sublayout) = Client sublayout
35 clientWithRoute Proxy req =
36 clientWithRoute (Proxy :: Proxy sublayout) $
37 appendToPath p req
39 where p = symbolVal (Proxy :: Proxy path)
41instance (KnownSymbol path, HasDocs sublayout) => HasDocs (path :> sublayout) where
43 docsFor Proxy (endpoint, action) =
44 docsFor sublayoutP (endpoint', action)
46 where sublayoutP = Proxy :: Proxy sublayout
47 endpoint' = endpoint & path <>~ symbolVal pa
48 pa = Proxy :: Proxy path