@@ -9,12 +9,16 @@ import Prelude.Compat
10import Data.Function
10import Data.Function
12import Data.Map
14import Data.Map
14import qualified Data.Map as M
16import qualified Data.Map as M
15import Data.Text
17import Data.Text
17import qualified Data.Text as T
19import qualified Data.Text as T
18import Network.Wai
22import Network.Wai
19 (Response, pathInfo)
23 (Response, pathInfo)
20import Servant.Server.Internal.ErrorFormatter
24import Servant.Server.Internal.ErrorFormatter
@@ -24,6 +28,18 @@ import Servant.Server.Internal.ServerError
25type Router env = Router' env RoutingApplication
29type Router env = Router' env RoutingApplication
31+data CaptureHint = CaptureHint
32+ { captureName :: Text
33+ , captureType :: TypeRep
37+toCaptureTag :: CaptureHint -> Text
38+toCaptureTag hint = captureName hint <> "::" <> (T.pack . show) (captureType hint)
40+toCaptureTags :: [CaptureHint] -> Text
41+toCaptureTags hints = "<" <> T.intercalate "|" (map toCaptureTag hints) <> ">"
27-- | Internal representation of a router.
43-- | Internal representation of a router.
29-- The first argument describes an environment type that is
45-- The first argument describes an environment type that is
@@ -36,10 +52,10 @@ data Router' env a =
36 -- ^ the map contains routers for subpaths (first path component used
52 -- ^ the map contains routers for subpaths (first path component used
37 -- for lookup and removed afterwards), the list contains handlers
53 -- for lookup and removed afterwards), the list contains handlers
38 -- for the empty path, to be tried in order
54 -- for the empty path, to be tried in order
39− | CaptureRouter (Router' (Text, env) a)
55+ | CaptureRouter [CaptureHint] (Router' (Text, env) a)
40 -- ^ first path component is passed to the child router in its
56 -- ^ first path component is passed to the child router in its
41 -- environment and removed afterwards
57 -- environment and removed afterwards
42− | CaptureAllRouter (Router' ([Text], env) a)
58+ | CaptureAllRouter [CaptureHint] (Router' ([Text], env) a)
43 -- ^ all path components are passed to the child router in its
59 -- ^ all path components are passed to the child router in its
44 -- environment and are removed afterwards
60 -- environment and are removed afterwards
45 | RawRouter (env -> a)
61 | RawRouter (env -> a)
@@ -69,8 +85,8 @@ leafRouter l = StaticRouter M.empty [l]
69choice :: Router' env a -> Router' env a -> Router' env a
85choice :: Router' env a -> Router' env a -> Router' env a
70choice (StaticRouter table1 ls1) (StaticRouter table2 ls2) =
86choice (StaticRouter table1 ls1) (StaticRouter table2 ls2) =
71 StaticRouter (M.unionWith choice table1 table2) (ls1 ++ ls2)
87 StaticRouter (M.unionWith choice table1 table2) (ls1 ++ ls2)
72−choice (CaptureRouter router1) (CaptureRouter router2) =
88+choice (CaptureRouter hints1 router1) (CaptureRouter hints2 router2) =
73− CaptureRouter (choice router1 router2)
89+ CaptureRouter (nub $ hints1 ++ hints2) (choice router1 router2)
74choice router1 (Choice router2 router3) = Choice (choice router1 router2) router3
90choice router1 (Choice router2 router3) = Choice (choice router1 router2) router3
75choice router1 router2 = Choice router1 router2
91choice router1 router2 = Choice router1 router2
@@ -84,7 +100,7 @@ choice router1 router2 = Choice router1 router2
85data RouterStructure =
101data RouterStructure =
86 StaticRouterStructure (Map Text RouterStructure) Int
102 StaticRouterStructure (Map Text RouterStructure) Int
87− | CaptureRouterStructure RouterStructure
103+ | CaptureRouterStructure [CaptureHint] RouterStructure
88 | RawRouterStructure
104 | RawRouterStructure
89 | ChoiceStructure RouterStructure RouterStructure
105 | ChoiceStructure RouterStructure RouterStructure
90 deriving (Eq, Show)
106 deriving (Eq, Show)
@@ -98,11 +114,11 @@ data RouterStructure =
98routerStructure :: Router' env a -> RouterStructure
114routerStructure :: Router' env a -> RouterStructure
99routerStructure (StaticRouter m ls) =
115routerStructure (StaticRouter m ls) =
100 StaticRouterStructure (fmap routerStructure m) (length ls)
116 StaticRouterStructure (fmap routerStructure m) (length ls)
101−routerStructure (CaptureRouter router) =
117+routerStructure (CaptureRouter hints router) =
102− CaptureRouterStructure $
118+ CaptureRouterStructure hints $
103 routerStructure router
119 routerStructure router
104−routerStructure (CaptureAllRouter router) =
120+routerStructure (CaptureAllRouter hints router) =
105− CaptureRouterStructure $
121+ CaptureRouterStructure hints $
106 routerStructure router
122 routerStructure router
107routerStructure (RawRouter _) =
123routerStructure (RawRouter _) =
108 RawRouterStructure
124 RawRouterStructure
@@ -114,8 +130,8 @@ routerStructure (Choice r1 r2) =
114-- | Compare the structure of two routers.
130-- | Compare the structure of two routers.
116sameStructure :: Router' env a -> Router' env b -> Bool
132sameStructure :: Router' env a -> Router' env b -> Bool
117−sameStructure r1 r2 =
133+sameStructure router1 router2 =
118− routerStructure r1 == routerStructure r2
134+ routerStructure router1 == routerStructure router2
120-- | Provide a textual representation of the
136-- | Provide a textual representation of the
121-- structure of a router.
137-- structure of a router.
@@ -126,7 +142,8 @@ routerLayout router =
127 mkRouterLayout :: Bool -> RouterStructure -> [Text]
143 mkRouterLayout :: Bool -> RouterStructure -> [Text]
128 mkRouterLayout c (StaticRouterStructure m n) = mkSubTrees c (M.toList m) n
144 mkRouterLayout c (StaticRouterStructure m n) = mkSubTrees c (M.toList m) n
129− mkRouterLayout c (CaptureRouterStructure r) = mkSubTree c "<capture>" (mkRouterLayout False r)
145+ mkRouterLayout c (CaptureRouterStructure hints r) =
146+ mkSubTree c (toCaptureTags hints) (mkRouterLayout False r)
130 mkRouterLayout c RawRouterStructure =
147 mkRouterLayout c RawRouterStructure =
131 if c then ["├─ <raw>"] else ["└─ <raw>"]
148 if c then ["├─ <raw>"] else ["└─ <raw>"]
132 mkRouterLayout c (ChoiceStructure r1 r2) =
149 mkRouterLayout c (ChoiceStructure r1 r2) =
@@ -169,7 +186,7 @@ runRouterEnv fmt router env request respond =
169 -> let request' = request { pathInfo = rest }
186 -> let request' = request { pathInfo = rest }
170 in runRouterEnv fmt router' env request' respond
187 in runRouterEnv fmt router' env request' respond
171 _ -> respond $ Fail $ fmt request
188 _ -> respond $ Fail $ fmt request
172− CaptureRouter router' ->
189+ CaptureRouter _ router' ->
173 case pathInfo request of
190 case pathInfo request of
174 [] -> respond $ Fail $ fmt request
191 [] -> respond $ Fail $ fmt request
175 -- This case is to handle trailing slashes.
192 -- This case is to handle trailing slashes.
@@ -177,7 +194,7 @@ runRouterEnv fmt router env request respond =
177 first : rest
194 first : rest
178 -> let request' = request { pathInfo = rest }
195 -> let request' = request { pathInfo = rest }
179 in runRouterEnv fmt router' (first, env) request' respond
196 in runRouterEnv fmt router' (first, env) request' respond
180− CaptureAllRouter router' ->
197+ CaptureAllRouter _ router' ->
181 let segments = pathInfo request
198 let segments = pathInfo request
182 request' = request { pathInfo = [] }
199 request' = request { pathInfo = [] }
183 in runRouterEnv fmt router' (segments, env) request' respond
200 in runRouterEnv fmt router' (segments, env) request' respond