Skip to content

New template for WebApp application #53

@Tunduk

Description

@Tunduk

Problem

Current templates don't follow modern ASP.NET standarts. They are great way to showcase main ideas of Giraffe and different scenarios to use it but couldn't be used as everyday templates for work
Every time when I create a new project via dotnet new Giraffe I have to rewrite the code to follow modern ASP.NET style.

Suggestion

Create a new template looking similar to the default ASP.NET webapi template.
It should be available via dotnet new giraffe-webapi

Reasons to change?

It should help developers in their day-to-day work. Simple empty template for a new service
Following Microsoft guidelines probably is good.

Sketch

Here is the code. Of course, it should be formatted by guidelines but as quick sketch it should work.

Program.fs

module CheckGiraffeTemplate.App

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

// ---------------------------------
// Models
// ---------------------------------

type WeatherForecast =
    { Date: DateOnly
      TemperatureC: int
      Summary: string }

let summaries =
    [| "Freezing"
       "Bracing"
       "Chilly"
       "Cool"
       "Mild"
       "Warm"
       "Balmy"
       "Hot"
       "Sweltering"
       "Scorching" |]

// ---------------------------------
// Web app
// ---------------------------------

let weatherForecastHandler =
    let forecast =
        seq { 1..5 }
        |> Seq.map (fun index ->
            { Date = DateTime.Now.AddDays index |> DateOnly.FromDateTime
              TemperatureC = Random.Shared.Next(-20, 55)
              Summary = summaries[Random.Shared.Next(summaries.Length)] })

    json forecast

let endpoints = [ GET [ route "/weatherforecast" weatherForecastHandler ] ]

// ---------------------------------
// Config and Main
// ---------------------------------

[<EntryPoint>]
let main args =
    let builder = WebApplication.CreateBuilder args
    
    builder.Services.AddGiraffe() |> ignore

    let app = builder.Build()

    app.UseHttpsRedirection() |> ignore

    app.MapGiraffeEndpoints endpoints

    app.Run()

    0

fsproj

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <AssemblyName>CheckGiraffeTemplate</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Giraffe" Version="6.4.0" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>

Next steps

  1. Update current templates. Update dependencies and code
  2. Check another Microsoft templates and maybe recreate them too. Aspire probably is a good choise
  3. Add support for OpenAPI generation (I'll create another issue for that)

I would be happy to help with all this work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions