|
| 1 | +# Host Configuration |
| 2 | + |
| 3 | +As your app becomes more complex, you'll inevitably need to reach for some additional host configuration. This is where `Microsoft.AspNetCore.Builder` comes in, which contains many useful extensions for configuring the server (ex: static files, authentication, authorization etc.). |
| 4 | + |
| 5 | +Most of the extension methods have existed since the early days of ASP.NET Core and operate against `IApplicationBuilder`. But more recent version of ASP.NET Core have introduced a new `WebApplication` type that implements `IApplicationBuilder` and provides some additional functionality, most notably endpoint configuration. This dichotomy makes pipelining next to impossible. In C# you don't feel the sting of this as much because of `void` returns. But in F# this results in an excess amount of `|> ignore` calls. |
| 6 | + |
| 7 | +Let's take the hero code from the [Getting Started](get-started.md) page and add some static file middleware to it: |
| 8 | + |
| 9 | +```fsharp |
| 10 | +module Program |
| 11 | +
|
| 12 | +open Falco |
| 13 | +open Falco.Routing |
| 14 | +open Microsoft.AspNetCore.Builder |
| 15 | +
|
| 16 | +let wapp = WebApplication.Create() |
| 17 | +
|
| 18 | +wapp.UseRouting() |
| 19 | + .UseDefaultFiles() // you might innocently think this is fine |
| 20 | + .UseStaticFiles() // and so is this |
| 21 | + // but uknowingly, the underlying type has changed |
| 22 | + .UseFalco([ |
| 23 | + get "/" (Response.ofPlainText "Hello World!") |
| 24 | + ]) |
| 25 | + .Run(Response.ofPlainText "Not found") |
| 26 | + // ^-- this is no longer starts up our application |
| 27 | +
|
| 28 | +// one way to fix this: |
| 29 | +wapp.UseRouting() |> ignore |
| 30 | +wapp.UseDefaultFiles().UseStaticFiles() |> ignore |
| 31 | +
|
| 32 | +wapp.UseFalco([ |
| 33 | + get "/" (Response.ofPlainText "Hello World!") |
| 34 | + ]) |
| 35 | + .Run(Response.ofPlainText "Not found") |
| 36 | +
|
| 37 | +// but we can do better |
| 38 | +``` |
| 39 | + |
| 40 | +To salve this, Falco comes with a several shims. The most important of these are `WebApplication.Use` and `WebApplication.UseIf` which allow you to compose a pipeline entirely driven by `WebApplication` while at the same time taking advantage of the existing ASP.NET Core extensions. |
| 41 | + |
| 42 | +The optional, but recommended way to take advantage of these is to utilize the static methods that server as the underpinning to the various extension methods available. The code below will attempt to highlight this more clearly: |
| 43 | + |
| 44 | +```fsharp |
| 45 | +// consider |
| 46 | +wapp.UseRouting() |
| 47 | + .Use(fun (appl : IApplicationBuilder) -> |
| 48 | + appl.UseDefaultFiles() |
| 49 | + .UseStaticFiles()) |
| 50 | + .UseFalco([ |
| 51 | + get "/" (Response.ofPlainText "Hello World!") |
| 52 | + ]) |
| 53 | + .Run(Response.ofPlainText "Not found") |
| 54 | +
|
| 55 | +// or better yet |
| 56 | +wapp.UseRouting() |
| 57 | + .Use(DefaultFilesExtensions.UseDefaultFiles) |
| 58 | + .Use(StaticFileExtensions.UseStaticFiles) |
| 59 | + // ^-- most IApplicationBuilder extensions are available as static methods similar to this |
| 60 | + .UseFalco([ |
| 61 | + get "/" (Response.ofPlainText "Hello World!") |
| 62 | + ]) |
| 63 | + .Run(Response.ofPlainText "Not found") |
| 64 | +``` |
| 65 | + |
| 66 | +[Next: Routing](routing.md) |
0 commit comments