代码之家  ›  专栏  ›  技术社区  ›  Natalie Perret

如何使F#长颈鹿默认路径指向/健康?

  •  0
  • Natalie Perret  · 技术社区  · 5 年前

    我有以下几点 Program.fs

    let webApp = choose [ setStatusCode 404 >=> text "Not Found" ]
    
    let errorHandler (ex : Exception) (logger : ILogger) =
        logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
        clearResponse >=> setStatusCode 500 >=> text ex.Message
    
    let configureApp (app : IApplicationBuilder) =
        app.UseHealthChecks(PathString("/health")) |> ignore
        let env = app.ApplicationServices.GetService<IHostingEnvironment>()
        (match env.IsDevelopment() with
        | true  -> app.UseDeveloperExceptionPage()
        | false -> app.UseGiraffeErrorHandler errorHandler)
            .UseHttpsRedirection()
            .UseStaticFiles()
            .UseGiraffe(webApp)
    
    let configureServices (services : IServiceCollection) =
        services.AddHealthChecks() |> ignore
        services.AddCors()    |> ignore
        services.AddGiraffe() |> ignore
    
    let configureLogging (builder : ILoggingBuilder) =
        builder.AddFilter(fun l -> l.Equals LogLevel.Error)
               .AddConsole()
               .AddDebug() |> ignore
    
    [<EntryPoint>]
    let main _ =
        let contentRoot = Directory.GetCurrentDirectory()
        let webRoot     = Path.Combine(contentRoot, "WebRoot")
        WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(contentRoot)
            .UseIISIntegration()
            .UseWebRoot(webRoot)
            .Configure(Action<IApplicationBuilder> configureApp)
            .ConfigureServices(configureServices)
            .ConfigureLogging(configureLogging)
            .Build()
            .Run()
        0
    

    我希望默认端点以健康检查为目标(即。 /health

    0 回复  |  直到 5 年前
        1
  •  1
  •   Natalie Perret    5 年前

    我看了评论中的文章: https://github.com/giraffe-fsharp/Giraffe/blob/master/DOCUMENTATION.md#redirection

    结果是 choose :

    let webApp = choose [
                    GET >=>
                        choose [
                            route "/" >=> redirectTo false "/health"
                        ]
                    setStatusCode 404 >=> text "Not Found" ]
    
    推荐文章