|
| 1 | +# Using generics |
| 2 | + |
| 3 | +```haskell |
| 4 | +{-# LANGUAGE DataKinds #-} |
| 5 | +{-# LANGUAGE DeriveGeneric #-} |
| 6 | +{-# LANGUAGE RankNTypes #-} |
| 7 | +{-# LANGUAGE TypeOperators #-} |
| 8 | +module Main (main, api, getLink, routesLinks, cliGet) where |
| 9 | +
|
| 10 | +import Control.Exception (throwIO) |
| 11 | +import Data.Proxy (Proxy (..)) |
| 12 | +import Network.Wai.Handler.Warp (run) |
| 13 | +import System.Environment (getArgs) |
| 14 | +
|
| 15 | +import Servant |
| 16 | +import Servant.Client |
| 17 | +
|
| 18 | +import Servant.API.Generic |
| 19 | +import Servant.Client.Generic |
| 20 | +import Servant.Server.Generic |
| 21 | +``` |
| 22 | +
|
| 23 | +The usage is simple, if you only need a collection of routes. |
| 24 | +First you define a record with field types prefixed by a parameter `route`: |
| 25 | +
|
| 26 | +```haskell |
| 27 | +data Routes route = Routes |
| 28 | + { _get :: route :- Capture "id" Int :> Get '[JSON] String |
| 29 | + , _put :: route :- ReqBody '[JSON] Int :> Put '[JSON] Bool |
| 30 | + } |
| 31 | + deriving (Generic) |
| 32 | +``` |
| 33 | +
|
| 34 | +Then we'll use this data type to define API, links, server and client. |
| 35 | +
|
| 36 | +## API |
| 37 | +
|
| 38 | +You can get a `Proxy` of the API using `genericApi`: |
| 39 | +
|
| 40 | +```haskell |
| 41 | +api :: Proxy (ToServantApi Routes) |
| 42 | +api = genericApi (Proxy :: Proxy Routes) |
| 43 | +``` |
| 44 | +
|
| 45 | +It's recommented to use `genericApi` function, as then you'll get |
| 46 | +better error message, for example if you forget to `derive Generic`. |
| 47 | +
|
| 48 | +## Links |
| 49 | +
|
| 50 | +The clear advantage of record-based generics approach, is that |
| 51 | +we can get safe links very conviently. We don't need to define endpoint types, |
| 52 | +as field accessors work as proxies: |
| 53 | +
|
| 54 | +```haskell |
| 55 | +getLink :: Int -> Link |
| 56 | +getLink = fieldLink _get |
| 57 | +``` |
| 58 | +
|
| 59 | +We can also get all links at once, as a record: |
| 60 | +
|
| 61 | +```haskell |
| 62 | +routesLinks :: Routes (AsLink Link) |
| 63 | +routesLinks = allFieldLinks |
| 64 | +``` |
| 65 | +
|
| 66 | +## Client |
| 67 | +
|
| 68 | +Even more power starts to show when we generate a record of client functions. |
| 69 | +Here we use `genericClientHoist` function, which let us simultaneously |
| 70 | +hoist the monad, in this case from `ClientM` to `IO`. |
| 71 | +
|
| 72 | +```haskell |
| 73 | +cliRoutes :: Routes (AsClientT IO) |
| 74 | +cliRoutes = genericClientHoist |
| 75 | + (\x -> runClientM x env >>= either throwIO return) |
| 76 | + where |
| 77 | + env = error "undefined environment" |
| 78 | +
|
| 79 | +cliGet :: Int -> IO String |
| 80 | +cliGet = _get cliRoutes |
| 81 | +``` |
| 82 | +
|
| 83 | +## Server |
| 84 | +
|
| 85 | +Finally, probably the most handy usage: we can convert record of handlers into |
| 86 | +the server implementation: |
| 87 | +
|
| 88 | +```haskell |
| 89 | +record :: Routes AsServer |
| 90 | +record = Routes |
| 91 | + { _get = return . show |
| 92 | + , _put = return . odd |
| 93 | + } |
| 94 | +
|
| 95 | +app :: Application |
| 96 | +app = genericServe record |
| 97 | +
|
| 98 | +main :: IO () |
| 99 | +main = do |
| 100 | + args <- getArgs |
| 101 | + case args of |
| 102 | + ("run":_) -> do |
| 103 | + putStrLn "Starting cookbook-generic at http://localhost:8000" |
| 104 | + run 8000 app |
| 105 | + _ -> putStrLn "To run, pass 'run' argument: cabal new-run cookbook-generic run" |
| 106 | +``` |
0 commit comments