BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Release 1.0 of Suave, a Web Server and Development Library for F#

Release 1.0 of Suave, a Web Server and Development Library for F#

This item in japanese

Suave 1.0 was recently released, bringing a new web development library to .NET. Suave packs a light, fully async web server and a semantic model to describe HTTP processing pipelines. Suave runs on multiple platforms and operating systems, including Windows, OSX, Linux, .NET and Mono. While it could be used from any .NET language, Suave combinators and types are designed to be used from F#.

The programming model of Suave is by using combinators, creating a bigger function with smaller ones. Henrik Feldt, maintainer of Suave, explains:

Combinators are higher order functions that can be composed in different ways to achieve different effects and produce HTTP responses.

Suave have combinators like OK "Hello" and notFound and many other low level HTTP constructs which give you great power to program any kind of web services. In Suave we operate over the realm of functions with type WebPart where type WebPart = HttpContext -async<HttpContext option>.

Having the type option type in there give us try success/fail semantics and the possibility of encoding different routes.

We use a function called choose that takes a list of WebParts and feeds each one consecutively an HttpContext until one of the WebParts returns Some httpContext; inside the httpContext is encoded the response the server will sent to the HTTP client.

We also use an operator >=> to compose WebParts and encode routings.

The example below shows the classic "Hello World!" with Suave. This web application responds to all requests it receives with "Hello World!".

open Suave
startWebServer defaultConfig (Successful.OK "Hello World!")

The next example shows how to match routes and return a 404 if none of the routes matches:

let app : WebApp =
   choose[
     path "/foo" >=> OK "foo"
     path "/bar" >=> OK "bar"
     notFound "no handlers found."
   ]
startWebServer defaultConfig app

Suave is an open source project, available on GitHub. Documentation and examples are shown on Suave's official website. InfoQ also has a more extensive interview with Henrik Feldt, where he explains into more details what is Suave and how to use it.

Rate this Article

Adoption
Style

BT