Giraffe icon indicating copy to clipboard operation
Giraffe copied to clipboard

Documentation around minimal usage of Hello World in Giraffe

Open wallymathieu opened this issue 4 years ago • 12 comments

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#.

wallymathieu avatar Nov 10 '21 07:11 wallymathieu

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

dustinmoris avatar Nov 14 '21 22:11 dustinmoris

+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()

slang25 avatar Nov 15 '21 00:11 slang25

@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 😄

wallymathieu avatar Nov 15 '21 06:11 wallymathieu

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!

klcantrellsep avatar Aug 26 '23 02:08 klcantrellsep

Send a PR and I’ll merge it!

TheAngryByrd avatar Aug 26 '23 17:08 TheAngryByrd

Any thoughts on where to put the documentation? I've added some PRs related to minimal API code on the samples repository.

wallymathieu avatar Aug 31 '23 05:08 wallymathieu

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.

  1. The object to build your routes upon
  2. Creating routes and outputting data
  3. 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.

TheAngryByrd avatar Aug 31 '23 13:08 TheAngryByrd

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.

wallymathieu avatar Aug 31 '23 13:08 wallymathieu

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.

wallymathieu avatar Aug 31 '23 14:08 wallymathieu

I think GETs at what I was thinking in my HEAD. Would you PUT that in the PR?

TheAngryByrd avatar Aug 31 '23 14:08 TheAngryByrd

I've updated the PR as can be seen here: https://github.com/giraffe-fsharp/samples/pull/2/files#diff-46281e1e531ca72cf9a54a4cb96c069c25d7bfdacb7a3a9c907d116c7beab52d

wallymathieu avatar Aug 31 '23 14:08 wallymathieu

Since the minimal hosting API is relatively new, I had to upgrade to net7 as well: https://github.com/giraffe-fsharp/samples/pull/3

wallymathieu avatar Aug 31 '23 14:08 wallymathieu