2017-12-29 11:01:02 +01:00
|
|
|
# Using a custom monad
|
|
|
|
|
2020-06-06 06:43:51 +02:00
|
|
|
In this section we will create an API for a book shelf without any backing DB storage.
|
2017-12-29 11:01:02 +01:00
|
|
|
We will keep state in memory and share it between requests using `Reader` monad and `STM`.
|
|
|
|
|
|
|
|
We start with a pretty standard set of imports and definition of the model:
|
|
|
|
|
|
|
|
``` haskell
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
|
|
|
|
import Control.Concurrent (forkIO, killThread)
|
2017-12-31 02:48:44 +01:00
|
|
|
import Control.Concurrent.STM.TVar (TVar, newTVar, readTVar,
|
2017-12-29 11:01:02 +01:00
|
|
|
writeTVar)
|
|
|
|
import Control.Exception (bracket)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import Control.Monad.STM (atomically)
|
|
|
|
import Control.Monad.Trans.Reader (ReaderT, ask, runReaderT)
|
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
|
|
|
import GHC.Generics (Generic)
|
|
|
|
import Network.HTTP.Client (defaultManagerSettings,
|
|
|
|
newManager)
|
|
|
|
import Network.Wai.Handler.Warp (run)
|
|
|
|
|
|
|
|
import Servant
|
|
|
|
import Servant.Client
|
|
|
|
|
|
|
|
newtype Book = Book String deriving (Show, Generic)
|
|
|
|
instance ToJSON Book
|
|
|
|
instance FromJSON Book
|
|
|
|
```
|
|
|
|
|
|
|
|
Now, let's define the API for book storage.
|
|
|
|
For the sake of simplicity we'll only have methods for getting all books and adding a new one.
|
|
|
|
|
|
|
|
``` haskell
|
|
|
|
type GetBooks = Get '[JSON] [Book]
|
|
|
|
type AddBook = ReqBody '[JSON] Book :> PostCreated '[JSON] Book
|
|
|
|
type BooksAPI = "books" :> (GetBooks :<|> AddBook)
|
|
|
|
|
|
|
|
api :: Proxy BooksAPI
|
|
|
|
api = Proxy
|
|
|
|
```
|
|
|
|
|
|
|
|
Next, we define the state and the monad to run our handlers
|
|
|
|
|
|
|
|
``` haskell
|
|
|
|
data State = State
|
|
|
|
{ books :: TVar [Book]
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppM = ReaderT State Handler
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that we can't use `State` monad here, because state will not be shared between requests.
|
|
|
|
|
|
|
|
We can now define handlers in terms of `AppM`...
|
|
|
|
|
|
|
|
```haskell
|
|
|
|
server :: ServerT BooksAPI AppM
|
|
|
|
server = getBooks :<|> addBook
|
|
|
|
where getBooks :: AppM [Book]
|
|
|
|
getBooks = do
|
|
|
|
State{books = p} <- ask
|
|
|
|
liftIO $ atomically $ readTVar p
|
|
|
|
|
|
|
|
addBook :: Book -> AppM Book
|
|
|
|
addBook book = do
|
|
|
|
State{books = p} <- ask
|
|
|
|
liftIO $ atomically $ readTVar p >>= writeTVar p . (book :)
|
|
|
|
return book
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
...and transform `AppM` to `Handler` by simply using `runReaderT`
|
|
|
|
|
|
|
|
``` haskell
|
|
|
|
nt :: State -> AppM a -> Handler a
|
|
|
|
nt s x = runReaderT x s
|
|
|
|
|
|
|
|
app :: State -> Application
|
|
|
|
app s = serve api $ hoistServer api (nt s) server
|
|
|
|
```
|
|
|
|
|
|
|
|
Finally, we end up with the following program
|
|
|
|
|
|
|
|
``` haskell
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
let port = 8080
|
|
|
|
mgr <- newManager defaultManagerSettings
|
|
|
|
initialBooks <- atomically $ newTVar []
|
|
|
|
let runApp = run port $ app $ State initialBooks
|
|
|
|
bracket (forkIO runApp) killThread $ \_ -> do
|
|
|
|
let getBooksClient :<|> addBookClient = client api
|
|
|
|
let printBooks = getBooksClient >>= liftIO . print
|
2017-12-31 02:48:44 +01:00
|
|
|
_ <- flip runClientM (mkClientEnv mgr (BaseUrl Http "localhost" port "")) $ do
|
2017-12-29 11:01:02 +01:00
|
|
|
_ <- printBooks
|
|
|
|
_ <- addBookClient $ Book "Harry Potter and the Order of the Phoenix"
|
|
|
|
_ <- printBooks
|
|
|
|
_ <- addBookClient $ Book "To Kill a Mockingbird"
|
|
|
|
_ <- printBooks
|
|
|
|
_ <- addBookClient $ Book "The Picture of Dorian Gray"
|
|
|
|
printBooks
|
|
|
|
return ()
|
|
|
|
```
|
|
|
|
|
|
|
|
When run, it outputs the following:
|
|
|
|
|
|
|
|
```
|
|
|
|
Running cookbook-using-custom-monad...
|
|
|
|
[]
|
|
|
|
[Book "Harry Potter and the Order of the Phoenix"]
|
|
|
|
[Book "To Kill a Mockingbird",Book "Harry Potter and the Order of the Phoenix"]
|
|
|
|
[Book "The Picture of Dorian Gray",Book "To Kill a Mockingbird",Book "Harry Potter and the Order of the Phoenix"]
|
2017-12-31 02:48:44 +01:00
|
|
|
```
|
2018-11-11 18:29:31 +01:00
|
|
|
|
|
|
|
To use `Raw` endpoints, look at the
|
|
|
|
[servant-rawm](http://hackage.haskell.org/package/servant-rawm) package.
|