Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How to start creating a Giraffe web project and how to serve it?

+1
−0

Having some trouble with giraffe-template on Mac M1, so decided to set up a Giraffe project manually. Started following the Doing it manually section of the Giraffe README, but got stuck right away, and I also couldn't see mentioned anywhere how the project could be served.

For the record, the Giraffe docs are great. I'm new to .NET, so the parts I'm struggling with are the basics of .NET project management, F#, and ASP.NET Core - it would be unreasonable to expect these topics covered in there.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

I have to keep reminding myself that

a Giraffe project plugs into the ASP.NET Core pipeline or is itself an ASP.NET Core application

, so if I can't find answers to my questions in the Giraffe docs, then it is probably because it is an ASP.NET Core topic (or an F# / .NET / etc. one).

How to create and serve a Giraffe project

Steps 0. to 5. follow the Get started with F# with command-line tools (.NET | Microsoft Learn) article.

  1. (OPTIONAL) Create a new solution.

    dotnet new sln -o SampleSolution
    
  2. Enter the solution's directory.

    cd SampleSolution
    
  3. Create an empty ASP.NET Core project.

    dotnet new web -lang "F#" -o src/GiraffeWebExample
    

    INFO The available dotnet new templates are available on the links below. (Both seem to list them all, but not sure which one is more up-to-date.)

  4. (OPTIONAL) Add new project to solution.

    dotnet sln add src/GiraffeWebExample/GiraffeWebExample.fsproj
    
  5. Enter the project's directory.

    cd src/GiraffeWebExample/
    
  6. Install dependencies.

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

    NOTE I got a warning below when adding Giraffe, so just pasting it here for completeness' sake:

    /usr/local/share/dotnet/sdk/8.0.202/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.DefaultItems.Shared.targets(111,5):
    warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not
    necessary when targeting .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web
    is used, the shared framework will be referenced automatically. Otherwise,
    the PackageReference should be replaced with a FrameworkReference. 
    [/Users/toraritte/dev/shed/dotnet/giraffe/ByHand/src/ByHand/ByHand.fsproj]
    
  7. Add the "entry point".

    NOTE Still haven't figured out what other ways .NET has to set up a web project, but the EntryPoint attribute is covered in the [Microsoft Learn][F# Guide] Console Applications and Explicit Entry Points article.

    I chose to simply copy one of the sample codes from the Doing it manually section; I prefer the more functional approach, so here it is the second one:

    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
    
  8. Run / serve project.

    dotnet watch run
    

    INFO Started with the Get started with ASP.NET Core article in the ASP.NET Core docs.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »