Giraffe
Giraffe copied to clipboard
Documentation around minimal usage of Hello World in Giraffe
Is there a way to use Giraffe in the same style as seen with Suave:
#r "nuget: Suave, 2.6.1"
open Suave
startWebServer defaultConfig (Successful.OK "Hello World!")
? Especially since we have seen marketing around minimal APIs for C#.
Unfortunately not but I think Saturn, which is a wrapper around Giraffe has a similar experience:
open Saturn
open Giraffe
let app = application {
use_router (text "Hello World from Saturn")
}
run app
+1 to what Dustin has said, what you are asking for is one of the goals Saturn has.
Here is what a minimal hosting bootstrapping for Giraffe might look like (works with .NET 6 & Giraffe 6.0.0-alpha-2):
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Giraffe
let builder = WebApplication.CreateBuilder()
builder.Services.AddGiraffe() |> ignore
let app = builder.Build()
app.UseGiraffe(text "Hello World!")
app.Run()
@dustinmoris the example that @slang25 has looks pretty minimal. It would be really nice to have it documented since it shows what you can do with Giraffe 😄
I discovered this issue trying to get Giraffe set up with .NET 7. It was super helpful to see how to do it here, but it'd be even better if it was documented in the readme or docs. P.S. thanks for the great framework!
Send a PR and I’ll merge it!
Any thoughts on where to put the documentation? I've added some PRs related to minimal API code on the samples repository.
After some reflection, I'm not sure the example provided above is as comparable to the minimal api docs link provided above.
For example:
var app = WebApplication.Create(args);
app.MapGet("/", () => "Hello World!");
app.Run();
Shows three things.
- The object to build your routes upon
- Creating routes and outputting data
- Starting the listening process
By contrast:
let builder = WebApplication.CreateBuilder()
builder.Services.AddGiraffe() |> ignore
let app = builder.Build()
app.UseGiraffe(text "Hello World!")
app.Run()
It does, 1 and 3, but doesn't do number 2. Given this example, how would I add an additional post route? We do have this basic starting example, it's not as cute as the minimal api example, and probably could be trimmed up based on the example provided but I think also showing how routes get created and being able to add them easily is a better route than playing code golf.
I agree that the example you show is better. Perhaps the reason why you have MapGet as a thing directly is because that is a route declaration.
I've updated the sample to be:
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Giraffe
let builder = WebApplication.CreateBuilder()
builder.Services.AddGiraffe() |> ignore
let app = builder.Build()
let webApp =
choose [
route "/ping" >=> text "pong"
route "/" >=> text "Hello World" ]
app.UseGiraffe(webApp)
app.Run()
since that is more in line with the intent of MapGet.
I think GETs at what I was thinking in my HEAD. Would you PUT that in the PR?
I've updated the PR as can be seen here: https://github.com/giraffe-fsharp/samples/pull/2/files#diff-46281e1e531ca72cf9a54a4cb96c069c25d7bfdacb7a3a9c907d116c7beab52d
Since the minimal hosting API is relatively new, I had to upgrade to net7 as well: https://github.com/giraffe-fsharp/samples/pull/3