Skip to content

A native functional ASP.NET Core web framework for F# developers.

License

Notifications You must be signed in to change notification settings

hvester/Giraffe

 
 

Repository files navigation

Giraffe

Giraffe

A functional ASP.NET Core micro web framework for building rich web applications.

Read this blog post on functional ASP.NET Core for more information.

NuGet Info

Windows and Linux Builds

Build status

Windows Build history

Table of contents

About

Giraffe is an F# micro web framework for building rich web applications. It has been heavily inspired and is similar to Suave, but has been specifically designed with ASP.NET Core in mind and can be plugged into the ASP.NET Core pipeline via middleware. Giraffe applications are composed of so called HttpHandler functions which can be thought of a mixture of Suave's WebParts and ASP.NET Core's middleware.

If you'd like to learn more about the motivation of this project please read my blog post on functional ASP.NET Core (some code samples in this blog post might be outdated today).

Who is it for?

Giraffe is intended for developers who want to build rich web applications on top of ASP.NET Core in a functional first approach. ASP.NET Core is a powerful web platform which has support by Microsoft and a huge developer community behind it and Giraffe is aimed at F# developers who want to benefit from that eco system.

It is not designed to be a competing web product which can be run standalone like NancyFx or Suave, but rather a lean micro framework which aims to complement ASP.NET Core where it comes short for functional developers. The fundamental idea is to build on top of the strong foundation of ASP.NET Core and re-use existing ASP.NET Core building blocks so F# developers can benefit from both worlds.

You can think of Giraffe as the functional counter part of the ASP.NET Core MVC framework.

Getting Started

Using dotnet-new

The easiest way to get started with Giraffe is by installing the giraffe-template package, which adds a new template to your dotnet new command line tool:

dotnet new -i "giraffe-template::*"

Afterwards you can create a new Giraffe application by running dotnet new giraffe.

If you are using dotnet core 2.1.4, you will need to specify the language: dotnet new giraffe -lang F#

For more information about the Giraffe template please visit the official giraffe-template repository.

Doing it manually

Install the Giraffe NuGet package*:

PM> Install-Package Giraffe

*) If you haven't installed the ASP.NET Core NuGet package yet then you'll also need to add a package reference to Microsoft.AspNetCore.App:

PM> Install-Package Microsoft.AspNetCore.App

Alternatively you can also use the .NET CLI to add the packages:

dotnet add package Microsoft.AspNetCore.App
dotnet add package Giraffe

Next create a web application and plug it into the ASP.NET Core middleware:

open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.DependencyInjection
open Giraffe

let webApp =
    choose [
        route "/ping"   >=> text "pong"
        route "/"       >=> htmlFile "/pages/index.html" ]

type Startup() =
    member __.ConfigureServices (services : IServiceCollection) =
        // Register default Giraffe dependencies
        services.AddGiraffe() |> ignore

    member __.Configure (app : IApplicationBuilder)
                        (env : IHostingEnvironment)
                        (loggerFactory : ILoggerFactory) =
        // Add Giraffe to the ASP.NET Core pipeline
        app.UseGiraffe webApp

[<EntryPoint>]
let main _ =
    Host.CreateDefaultBuilder()
        .ConfigureWebHostDefaults(
            fun webHostBuilder ->
                webHostBuilder
                    .UseStartup<Startup>()
                    |> ignore)
        .Build()
        .Run()
    0

Instead of creating a Startup class you can also add Giraffe in a more functional way:

open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.DependencyInjection
open Giraffe

let webApp =
    choose [
        route "/ping"   >=> text "pong"
        route "/"       >=> htmlFile "/pages/index.html" ]

let configureApp (app : IApplicationBuilder) =
    // Add Giraffe to the ASP.NET Core pipeline
    app.UseGiraffe webApp

let configureServices (services : IServiceCollection) =
    // Add Giraffe dependencies
    services.AddGiraffe() |> ignore

[<EntryPoint>]
let main _ =
    Host.CreateDefaultBuilder()
        .ConfigureWebHostDefaults(
            fun webHostBuilder ->
                webHostBuilder
                    .Configure(configureApp)
                    .ConfigureServices(configureServices)
                    |> ignore)
        .Build()
        .Run()
    0

For more information please check the official Giraffe documentation.

Sample applications

Demo apps

There is a few sample applications which can be found in the samples GitHub repository. Please check the README.md there for further information.

Live apps

buildstats.info

The web service https://buildstats.info uses Giraffe to build rich SVG widgets for Git repositories. The application runs as a Docker container in the Google Container Engine (see CI-BuiltStats on GitHub for more information).

dusted.codes

My personal blog https://dusted.codes is also built with Giraffe and ASP.NET Core and all of the source code is published on GitHub for further reference.

More sample applications will be added in the future.

Benchmarks

Giraffe is part of the TechEmpower Web Framework Benchmarks and will be listed in the official results page in the upcoming Round 17 for the first time.

Unofficial test results are currently available on the TFB Status page.

As of today Giraffe competes in the Plaintext, JSON and Fortunes categories and has been doing pretty well so far, even outperforming ASP.NET Core MVC in Plaintext and JSON at the time of writing.

The latest implementation which is being used for the benchmark tests can be seen inside the TechEmpower repository.

Giraffe is also featured in Jimmy Byrd's dotnet-web-benchmarks where we've run earlier performance tests.

Building and developing

Giraffe is built with the latest .NET Core SDK.

You can either install Visual Studio 2017 which comes with the latest SDK or manually download and install the .NET SDK 2.1.

After installation you should be able to run the ./build.ps1 script to successfully build, test and package the library.

The build script supports the following flags:

  • -Release will build Giraffe with the Release configuration
  • -ExcludeTests will skip the test project. This is useful to quickly validate any build errors.
  • -Pack will create a NuGet package for Giraffe.
  • -ClearOnly will delete all old build artifacts and not build anything itself.

Examples:

Only build the Giraffe project in Debug mode:

PS > ./build.ps1

Build the Giraffe project in Release mode:

PS > ./build.ps1 -Release

Build the Giraffe project in Debug mode and skip running the tests project:

PS > ./build.ps1 -ExcludeTests

Build and test all projects, use the Release build configuration and create all NuGet packages:

PS > ./build.ps1 -Release -Pack

Building on Linux or macOS

In order to successfully run the build script on Linux or macOS you will have to install PowerShell for Linux or Mac.

Additionally you will also have to install the latest version of Mono in order to target full framework monikers during the build steps.

Development environment

Currently the best way to work with F# on .NET Core is to use Visual Studio Code with the Ionide extension. Intellisense and debugging is supported with the latest versions of both.

Contributing

Help and feedback is always welcome and pull requests get accepted.

TL;DR

  • First open an issue to discuss your changes
  • After your change has been formally approved please submit your PR against the develop branch
  • Please follow the code convention by examining existing code
  • Add/modify the README.md as required
  • Add/modify unit tests as required
  • Please document your changes in the upcoming release notes in RELEASE_NOTES.md
  • PRs can only be approved and merged when all checks succeed (builds on Windows and Linux)

Discuss your change first

When contributing to this repository, please first discuss the change you wish to make via an open issue before submitting a pull request. For new feature requests please describe your idea in more detail and how it could benefit other users as well.

Please be aware that Giraffe strictly aims to remain as light as possible while providing generic functionality for building functional web applications. New feature work must be applicable to a broader user base and if this requirement cannot be sufficiently met then a pull request might get rejected. In the case of doubt the maintainer might rather reject a potentially useful feature than adding one too many. This measure is to protect the repository from feature bloat and shall not be taken personally.

Code conventions

When making changes please use existing code as a guideline for coding style and documentation. For example add spaces when creating tuples ((a,b) --> (a, b)), annotating variable types (str:string --> str : string) or other language constructs.

Examples:

let someHttpHandler:HttpHandler =
    fun (ctx:HttpContext) next -> task {
        // Some work
    }

should be:

let someHttpHandler : HttpHandler =
    fun (ctx : HttpContext) (next : HttpFunc) ->
        task {
            // Some work
        }

Keep documentation and unit tests up to date

If you intend to add or change an existing HttpHandler then please update the README.md file to reflect these changes there as well. If applicable unit tests must be added or updated and the project must successfully build before a pull request can be accepted.

Submit a pull request against develop

The develop branch is the main and only branch which should be used for all pull requests. A merge into develop means that your changes are scheduled to go live with the very next release, which could happen any time from the same day up to a couple weeks (depending on priorities and urgency).

Only pull requests which pass all build checks and comply with the general coding guidelines can be approved.

If you have any further questions please let me know.

You can file an issue on GitHub or contact me via https://dusted.codes/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

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.

Please be aware that unofficial builds have not gone through the scrutiny of official releases and their usage is on your own risk.

Blog posts

Blog posts by author

Community blog posts

If you have blogged about Giraffe, demonstrating a useful topic or some other tips or tricks then please feel free to submit a pull request and add your article to this list as a reference for other Giraffe users. Thank you!

Videos

License

Apache 2.0

Contact and Slack Channel

If you have any further questions feel free to reach out to me via any of the mentioned social media on https://dusted.codes/about or join the #giraffe Slack channel in the Functional Programming Slack Team. Please use this link to request an invitation to the Functional Programming Slack Team if you don't have an account registered yet.

Support

If you've got value from any of the content which I have created, but pull requests are not your thing, then I would also very much appreciate your support by buying me a coffee. Thank you!

Buy Me A Coffee

About

A native functional ASP.NET Core web framework for F# developers.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • F# 95.7%
  • PowerShell 4.3%