BT

Building an F# Web Server with Freya

| by Pierre-Luc Maheu Follow 4 Followers on May 15, 2017. Estimated reading time: 1 minute |

Freya is an F# web framework focusing on HTTP primitives and concurency. It doesn't include interface constructs such as templating. Marcus Griep presented Freya at F# eXchange 2017, where he explained its core model. He also showed the different mechanisms available for performance and concurrency, such as Hopac and Kestrel integrations.

Freya supports different hosting configurations. It can run as a self-hosted application, started directly with dotnet run. It can also run on top of Kestrel. The .NET Framework and .NET Core are both supported.

For performance, Griep recommends to run Freya on Kestrel to benefit from the many optimizations of Kestrel. In the benchmark he shows, the classical "Hello World" takes 6 ms in ASP.NET Core, 13 ms with Freya on Hopac and 26 ms with Freya on F# Async.

Hopac, as Griep's benchmark shows, can improve performance significantly in comparison to F# Async. Hopac uses a cooperative model of threading, instead of preemptive. Cooperative scheduling leads to less context switching and thus more efficient CPU usage. However, it doesn't perform well with long running jobs, as they will run through completion and potentially starve other jobs waiting to be executed.

Freya's programming model is aimed towards providing a type safe abstraction of HTTP. The freya computation expression abstracts OWIN state. The following shows a basic example of getting a query string parameter and returning a result:

let name_ = Route.atom_ "name"
let name =
    freya {
        // Get the query string parameter "name"
        let! name = Freya.Optic.get name_

        match name with
        | Some name -> return name
        | None -> return "World" }

let sayHello =
    freya {
        let! name = name

        return Represent.text (sprintf "Hello, %s!" name) }

Freya machines are an abstraction of a decision tree. A decision represents part of an HTTP rule, such as "Is CORS header present?". The full tree contains hundreds of decisions and can be extended. It also features automatic optimization, pruning all the decisions that aren't relevant for the given configuration.

Freya machines can also be defined using computation expressions. Following the earlier example, this machine sets the conditions for responding with HelloWord.

let helloMachine =
    freyaMachine {
        methods [GET; HEAD; OPTIONS]
        handleOk sayHello }

The final piece to complete the example is binding a machine to a route:

let router =
    freyaRouter {
        resource "/hello{/name}" machine }

Written with StackEdit.

Rate this Article

Adoption Stage
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

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT