Skip to content

giraffe-fsharp/Giraffe.TokenRouter

Repository files navigation

Giraffe.TokenRouter

Giraffe

Alternative routing API for Giraffe web applications which is aimed at maximum performance.

NuGet Info

Windows Linux
Windows Build status Linux Build status
Windows Build history Linux Build history

Table of contents

Documentation

The Giraffe.TokenRouter module adds alternative HttpHandler functions to route incoming HTTP requests through a basic Radix Tree. Several routing handlers (e.g.: routef and subRoute) have been overridden in such a way that path matching and value parsing are significantly faster than using the basic choose function.

This implementation assumes that additional memory and compilation time is not an issue. If speed and performance of parsing and path matching is required then the Giraffe.TokenRouter is the preferred option.

router

The base of all routing decisions is a router function instead of the default choose function when using the Giraffe.TokenRouter module.

The router HttpHandler takes two arguments, a HttpHandler to execute when no route can be matched (typical 404 Not Found handler) and secondly a list of all routing functions.

Example:

Defining a basic router and routes

let notFound = setStatusCode 404 >=> text "Not found"
let app =
    router notFound [
        route "/"       (text "index")
        route "/about"  (text "about")
    ]

routing functions

When using the Giraffe.TokenRouter module the main routing functions have been slightly overridden to match the alternative (speed improved) implementation.

The route and routef handlers work the exact same way as before, except that the continuation handler needs to be enclosed in parentheses or captured by the <| or => operators.

The http handlers GET, POST, PUT and DELETE are functions which take a list of nested http handler functions similar to before.

The subRoute handler has been altered in order to accept an additional parameter of child routing functions. All child routing functions will presume that the given sub path has been prepended.

Example:

Defining a basic router and routes

let notFound = setStatusCode 404 >=> text "Not found"
let app =
    router notFound [
        route "/"       (text "index")
        route "/about"  (text "about")
        routef "parsing/%s/%i" (fun (s,i) -> text (sprintf "Recieved %s & %i" s i))
        subRoute "/api" [
            GET [
                route "/"       (text "api index")
                route "/about"  (text "api about")
                subRoute "/v2" [
                    route "/"       (text "api v2 index")
                    route "/about"  (text "api v2 about")
                ]
            ]

        ]
    ]

Nightly builds and NuGet feed

All official Giraffe packages are published to the official and public NuGet feed.

Unofficial builds (such as pre-release builds from the develop branch and pull requests) produce unofficial pre-release NuGet packages which can be pulled from the project's public NuGet feed on AppVeyor:

https://ci.appveyor.com/nuget/giraffe-tokenrouter

If you add this source to your NuGet CLI or project settings then you can pull unofficial NuGet packages for quick feature testing or urgent hot fixes.

More information

For more information about Giraffe, how to set up a development environment, contribution guidelines and more please visit the main documentation page.

License

Apache 2.0