https://github.com/athanclark/wai-middleware-content-type
Route to different middlewares based on the incoming Accept header detected.
https://github.com/athanclark/wai-middleware-content-type
haskell haskell-warp http-server middleware mime-types
Last synced: 3 months ago
JSON representation
Route to different middlewares based on the incoming Accept header detected.
- Host: GitHub
- URL: https://github.com/athanclark/wai-middleware-content-type
- Owner: athanclark
- License: bsd-3-clause
- Created: 2015-09-22T21:12:56.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-09-28T09:25:02.000Z (over 2 years ago)
- Last Synced: 2025-01-28T17:44:34.041Z (about 1 year ago)
- Topics: haskell, haskell-warp, http-server, middleware, mime-types
- Language: Haskell
- Size: 84 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
wai-middleware-content-type
===========================
Route middlewares based on the incoming `Accept` HTTP header,
and other hints like the file extension (`foo.txt`) of the route
requested.
## Usage
This package provides many combinators for turning various data
types into the response you'd expect. For instance,
[blaze-html](https://hackage.haskell.org/package/blaze-html) gives
us _strictly_ `Html` data, right? We can be sure to only respond
with `Html`-compatible requests with our toolset:
```haskell
import Network.Wai.Middleware.ContentType
import Network.Wai.Trans
myMiddleware :: MiddlewareT (ReaderT Env m)
contentTypeRoutes :: Monad m =>
FileExtListenerT urlbase (Status -> ResponseHeaders -> Response) m ()
contentTypeRoutes = do
blaze myBlazeResponse
cassius myCassiusResponse
text myTextResponse
contentMiddleware :: Monad m => MiddlewareT m
contentMiddleware app req respond =
fileExtsToMiddleware $
mapResponse (\f -> f status200 []) contentTypeRoutes
```
This library was designed for use with [nested-routes](https://hackage.haskell.org/package/nested-routes),
but it's all good if you want to use it separately.