BT

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

by Pierre-Luc Maheu on Jan 27, 2016 |

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

Relevance
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
General Feedback
Bugs
Advertising
Editorial
Marketing
InfoQ.com and all content copyright © 2006-2016 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT