on eff31f7 help sign in
49 lines 1.6 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.Common.Req
13import Servant.Docs
14import Servant.Server
16-- | The contained API (second argument) can be found under @("/" ++ path)@
17-- (path being the first argument).
18data (path :: k) :> a = Proxy path :> a
19infixr 9 :>
21instance (KnownSymbol path, HasServer sublayout) => HasServer (path :> sublayout) where
22 type Server (path :> sublayout) = Server sublayout
23 route Proxy subserver request respond = case pathInfo request of
24 (first : rest)
25 | first == cs (symbolVal proxyPath)
26 -> route (Proxy :: Proxy sublayout) subserver request{
27 pathInfo = rest
28 } respond
29 _ -> respond $ failWith NotFound
31 where proxyPath = Proxy :: Proxy path
33instance (KnownSymbol path, HasClient sublayout) => HasClient (path :> sublayout) where
34 type Client (path :> sublayout) = Client sublayout
36 clientWithRoute Proxy req =
37 clientWithRoute (Proxy :: Proxy sublayout) $
38 appendToPath p req
40 where p = symbolVal (Proxy :: Proxy path)
42instance (KnownSymbol path, HasDocs sublayout) => HasDocs (path :> sublayout) where
44 docsFor Proxy (endpoint, action) =
45 docsFor sublayoutP (endpoint', action)
47 where sublayoutP = Proxy :: Proxy sublayout
48 endpoint' = endpoint & path <>~ symbolVal pa
49 pa = Proxy :: Proxy path