Exercise solution: Introducing I/O

Exercise

In this lesson, we wrote functionServer:

functionServer :: (Request -> Response) -> IO ()
functionServer f =
    server $ \socket ->
      do
        lbs <- LSocket.getContents socket

        case (LP.parse requestParser lbs) of

            LP.Fail _remainingInput ctxs msg ->
                putStrLn (showParseError ctxs msg)

            LP.Done _remainingInput request ->
                let
                    response = f request

                    responseBytes =
                        BSB.toLazyByteString (encodeResponse response)
                in
                    LSocket.sendAll socket responseBytes

Modify functionServer so that the request handler can do I/O. We’ve given you the start of it below.

ioServer :: (Request -> IO Response) -> IO ()
ioServer f =
    server $ \socket ->
      do
        lbs <- LSocket.getContents socket

        case (LP.parse requestParser lbs) of

            LP.Fail _remainingInput ctxs msg ->
                putStrLn (showParseError ctxs msg)

            LP.Done _remainingInput request -> _

Solution

Sign up for access to the full page, plus the complete archive and all the latest content.