sockets-and-pipes-0.1: Support for the Sockets and Pipes book
Safe HaskellNone
LanguageHaskell2010

SocketsAndPipes.Serve

Synopsis

Serve

serve Source #

Arguments

:: ServeOptions 
-> (Socket -> IO ())

What to do each time a new client connects to your server. These actions run concurrently in separate threads.

-> IO ()

Perpetually listens for incoming connections and runs the (Socket -> IO ()) function each time a new client opens a connection.

Example

Suppose you have a function that reads an HTTP request from a socket and then writes an HTTP response back to the socket.

handleHttpRequest :: Socket -> IO ()

Then you might start a server by running the following in GHCi:

λ> import SocketsAndPipes.Serve
λ> serve (port 8000) handleHttpRequest

And while the server is still running, test it on the command line like so:

$ curl http://localhost:8000

Options

data ServeOptions Source #

The first argument to serve.

port Source #

Arguments

:: PortNumber

The port number that your server will listen on

-> ServeOptions 

Alternatives

The serve function here is somewhat narrowly tailored to fit our purposes in Sockets and Pipes. Some other packages have more expansive offerings: