Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- serve :: ServeOptions -> (Socket -> IO ()) -> IO ()
- data ServeOptions
- port :: PortNumber -> ServeOptions
Serve
:: 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 |
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
.
:: PortNumber | The port number that your server will listen on |
-> ServeOptions |
Particulars
This library ought to be mostly suitable for real applications, but keep in mind that some of the details were chosen primarily with Sockets and Pipes exercises in mind.
The server prints some messages to the standard error stream. It gives an initial success/failure message when setting up the listening socket, and it displays any exceptions thrown from your connection handler. We provide no means of customizing or redirecting this output.
When the server is brought to a stop by an asynchronous exception, all of the threads it has spawned to handle connections are immediately stopped as well. This avoids any potentially confusing behavior that might arise if old threads could continue running after you stop and restart a server in GHCi.
We set the SO_REUSEADDR
flag on on the listening socket. This has some
security-related ramifications, but it lets you restart a server quickly. By
default, the OS would require some amount of time to elapse before a
previously-used port is allowed to be bound again.
When possible, the listening socket is bound to an IPv6 address with IPv4 also
enabled. This allows us to run an HTTP server that can be reached at both the
IPv4 address http://127.0.0.1:8000
and the IPv6 adddress http://[::1]:8000
.
Alternatives
Some other packages offer different choices and configuration options: