Giraffe back-end for SAFE stack?

Created on 2 Dec 2017  Â·  9Comments  Â·  Source: giraffe-fsharp/Giraffe

Would like to follow upon any progress with getting a sample implementation of Giraffe+SAFE stack?
There was a comment from @dustinmoris on this issue for a working sample:
https://github.com/SAFE-Stack/SAFE-BookStore/issues/196

Getting SAFE to work with Giraffe seems tough. Maybe because I am still a newb to all of this.

help wanted samples

All 9 comments

Giraffe works nicely with safe. Have it working. But no time at the moment
to up the bookstore

Am 02.12.2017 16:07 schrieb "boyedarat" notifications@github.com:

Would like to follow upon any progress with getting a sample
implementation of Giraffe+SAFE stack?
There was a comment from @dustinmoris https://github.com/dustinmoris on
this issue for a working sample:
SAFE-Stack/SAFE-BookStore#196
https://github.com/SAFE-Stack/SAFE-BookStore/issues/196

Getting SAFE to work with Giraffe seems tough. Maybe because I am still a
newb to all of this.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/dustinmoris/Giraffe/issues/161, or mute the thread
https://github.com/notifications/unsubscribe-auth/AADgNG7dZB14DWLGUH5sBGs1jyjIypx1ks5s8WfFgaJpZM4QzVlD
.

@boyedarat Haven't had time to make any progress yet, sorry. It's still on the todo list though. Any help on this would be more than welcome as well. Meanwhile maybe @forki can assist you with answering any specific questions you might have?

Sure

Am 03.12.2017 22:27 schrieb "Dustin Moris Gorski" <[email protected]

:

@boyedarat https://github.com/boyedarat Haven't had time to make any
progress yet, sorry. It's still on the todo list though. Any help on this
would be more than welcome as well. Meanwhile maybe @forki
https://github.com/forki can assist you with answering any specific
questions you might have?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dustinmoris/Giraffe/issues/161#issuecomment-348816015,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADgNM1oqEScBile99P-wnZB4GmiX2Ryks5s8xI4gaJpZM4QzVlD
.

@forki Could you share the parts where you replaced Suave with Giraffe? Not familiar with which parts of Suave matches with Giraffe. I am not familiar with Paket either.

Suave and giraffe are the Webservers they replace each other completely

Am 07.12.2017 21:50 schrieb "boyedarat" notifications@github.com:

@forki https://github.com/forki Could you share the parts where you
replaced Suave with Giraffe? Not familiar with which parts of Suave matches
with Giraffe. I am not familiar with Paket either.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/giraffe-fsharp/Giraffe/issues/161#issuecomment-350090321,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADgNGzy_6J17pHF6smEoive9CkhLdWcks5s-E-MgaJpZM4QzVlD
.

@forki what is the equivalent for "startWebServer config webPart"?

@boyedarat the "equivalent" of the Suave "startWebServer" function in Giraffe would be an instance of the ASP.NET Core WebHostBuilder class with the use of the Configure extension method or a custom Startup class. Something like

WebHostBuilder().Configure(fun builder -> builder.UseGiraffe(...your handlers...)).UseKestrel().Build().Run()

This usage of the ASP.NET Core hosting and startup functionality with Giraffe is somewhat covered in the "Doing it manually" part of the Giraffe readme and can also be seen at work in the SampleApp Program.fs file.

As I'm sure you can already tell, there is some additional thought that has to be put into how to integrate/replace the ASP.NET Core host builder code with the SAFE-Bookstore Server entry code (what is in the Program.fs and WebServer.fs files). But hopefully this information can help you along.

Translated the server code the best I could, compiles fine, but I can't get the web server started. Here is the error that I got:
[HPM] Error occurred while trying to proxy request /api/init from localhost:8080 to http://localhost:8085 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

Here is the converted code for the server module:

open System
open System.IO

open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Cors.Infrastructure
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http

open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection

open Giraffe

open Shared


let webRoot = Path.Combine("..","Client") |> Path.GetFullPath

let getInitCounter () : Async<Counter> = async { return 42 }

let webApp =
  choose [
    route "/api/init" >=> fun (next : HttpFunc) (ctx : HttpContext) ->
      task {
        let! counter = getInitCounter()
        return! Successful.OK (string counter) next ctx
      }
    route "/" >=> htmlFile webRoot
    setStatusCode 404 >=> text "Not Found"]

let configureCors (builder : CorsPolicyBuilder) =
  builder.WithOrigins("http://localhost:8085").AllowAnyMethod().AllowAnyHeader() |> ignore


let errorHandler (ex : Exception) (logger : ILogger) =
    logger.LogError(EventId(), ex, "An unhandled exception has occurred while executing the request.")
    clearResponse >=> setStatusCode 500 >=> text ex.Message

let configureApp (app : IApplicationBuilder) =
  app.UseCors(configureCors)
    .UseGiraffeErrorHandler(errorHandler)
    .UseStaticFiles()
    .UseGiraffe(webApp)

let configureLogging (builder : ILoggingBuilder) =
  let filter (l : LogLevel) = l.Equals LogLevel.Error
  builder.AddFilter(filter).AddConsole().AddDebug() |> ignore

let configureServices (services : IServiceCollection) =
    services.AddCors() |> ignore

[<EntryPoint>]
let main _ =
  WebHostBuilder()
    .UseKestrel()
    .UseIISIntegration()
    .UseWebRoot(webRoot)
    .Configure(Action<IApplicationBuilder> configureApp)
    .ConfigureServices(configureServices)
    .ConfigureLogging(configureLogging)
    .Build()
    .Run()
  0

Source: https://github.com/SAFE-Stack/SAFE-template/blob/master/Content/src/Server/Program.fs

I am going to close this as there is already an issue open for this in the SAFE-Stack repo and @forki has produced the first working version of "GAFE" in this pull request: https://github.com/SAFE-Stack/SAFE-BookStore/pull/255

Was this page helpful?
0 / 5 - 0 ratings

Related issues

padzikm picture padzikm  Â·  6Comments

dustinmoris picture dustinmoris  Â·  6Comments

Dzoukr picture Dzoukr  Â·  4Comments

hravnx picture hravnx  Â·  5Comments

Disco-Dave picture Disco-Dave  Â·  4Comments