Skip to content

Commit 8da966f

Browse files
authored
Merge pull request #1455 from bChiquet/Document-Raw's-behaviour
Document Raw's behaviour when composing APIs
2 parents bf160cc + 8b93af3 commit 8da966f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

doc/tutorial/ApiType.lhs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,30 @@ One example for this is if you want to serve a directory of static files along
389389
with the rest of your API. But you can plug in everything that is an
390390
`Application`, e.g. a whole web application written in any of the web
391391
frameworks that support `wai`.
392+
393+
Be mindful! The `servant-server`'s router works by pattern-matching the
394+
different routes that are composed using `:<|>`. `Raw`, as an escape hatch,
395+
matches any route that hasn't been matched by previous patterns. Therefore,
396+
any subsequent route will be silently ignored.
397+
398+
``` haskell
399+
type UserAPI14 = Raw
400+
:<|> "users" :> Get '[JSON] [User]
401+
-- In this situation, the /users endpoint
402+
-- will not be reachable because the Raw
403+
-- endpoint matches requests before
404+
```
405+
A simple way to avoid this pitfall is to either use `Raw` as the last
406+
definition, or to always have it under a static path.
407+
408+
``` haskell
409+
type UserAPI15 = "files" :> Raw
410+
-- The raw endpoint is under the /files
411+
-- static path, so it won't match /users.
412+
:<|> "users" :> Get '[JSON] [User]
413+
414+
type UserAPI16 = "users" :> Get '[JSON] [User]
415+
:<|> Raw
416+
-- The Raw endpoint is matched last, so
417+
-- it won't overlap another endpoint.
418+
```

0 commit comments

Comments
 (0)