Browse Source

🐦[FSharp] Falco - v2.0.3 (#6118)

* falco v1.2.3

* Server header, removed meta tag from head

* inline json message to meet spec

* [FSharp] - Falco upgraded to v2.0.0

* static mapper for datareader

* json closure for recomputation

* fortune feature reorg

* 🐦[FSharp] Falco - v2.0.3

* 🐦[FSharp] Falco - v2.0.3

* net5.0

* code collapse

Co-authored-by: pimbrouwers <[email protected]>
pim 4 years ago
parent
commit
a1b8828f6c

+ 6 - 3
frameworks/FSharp/falco/src/App/App.fsproj

@@ -2,21 +2,24 @@
 
   <PropertyGroup>
     <TargetFramework>net5.0</TargetFramework>
+    <DebugType>portable</DebugType>
+    <AssemblyName>App</AssemblyName>
+    <OutputType>Exe</OutputType>
+    <EnableDefaultContentItems>false</EnableDefaultContentItems>
   </PropertyGroup>
 
   <ItemGroup>
     <Compile Include="UI.fs" />
-    <Compile Include="Value.fs" />
     <Compile Include="Fortune.fs" />
     <Compile Include="Server.fs" />
     <Compile Include="Program.fs" />
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Update="FSharp.Core" Version="5.0.0-beta.20417.1" />
+    <PackageReference Update="FSharp.Core" Version="5.0.0" />
     <PackageReference Include="Donald" Version="3.0.*" />
     <PackageReference Include="Falco" Version="2.0.*" />    
-    <PackageReference Include="Npgsql" Version="5.0.0-alpha1" />
+    <PackageReference Include="Npgsql" Version="4.1.5" />
   </ItemGroup>
 
 </Project>

+ 2 - 1
frameworks/FSharp/falco/src/App/Fortune.fs

@@ -11,7 +11,8 @@ type FortuneModel =
        message : string
    }
 
-   static member fromDataReader (rd : IDataReader) =
+module FortuneModel =
+   let fromDataReader (rd : IDataReader) =
        {
            id = rd.GetInt32("id")
            message = rd.GetString("message")

+ 11 - 10
frameworks/FSharp/falco/src/App/Program.fs

@@ -1,23 +1,24 @@
-module App.Program
+module Program
 
-open System.Data
 open Falco
-open Npgsql
+open App
 
 [<Literal>]
-let ConnectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=1024;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3"
+let connectionString = "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=1024;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3"
 
-let connectionFactory =     
-    fun () -> new NpgsqlConnection(ConnectionString) :> IDbConnection
+[<Literal>]
+let defaultMsg = "Hello, World!"
+
+type JsonModel = { message : string }
 
 [<EntryPoint>]
-let main args =    
+let main args =        
     Host.startWebHost 
         args        
-        (Server.buildServer connectionFactory)
+        (Server.configure connectionString)
         [
-            get "/plaintext"  Value.handlePlainText
-            get "/json"       Value.handleJson
+            get "/plaintext"  (Response.ofPlainText defaultMsg)
+            get "/json"       (Response.ofJson { message = defaultMsg })
             get "/fortunes"   Fortune.handleIndex
         ]    
     0

+ 30 - 26
frameworks/FSharp/falco/src/App/Server.fs

@@ -1,43 +1,47 @@
 module App.Server
 
-open System.Threading.Tasks
+open System.Data
 open Donald
 open Falco
+open Falco.Host
 open Microsoft.AspNetCore.Builder
 open Microsoft.AspNetCore.Hosting
 open Microsoft.Extensions.DependencyInjection
 open Microsoft.Extensions.Logging
+open Npgsql
 
-let buildServer (connectionFactory : DbConnectionFactory) : Host.ConfigureWebHost =
-    let configureLogging (log : ILoggingBuilder) =
-        log.ClearProviders()
-        |> ignore
+type ConnectionString = string
+type ConfigureLogging = ILoggingBuilder -> unit
+type ConfigureServices = DbConnectionFactory -> IServiceCollection -> unit
+type ConfigureApp = HttpEndpoint list -> IApplicationBuilder -> unit
+type ConfigureServer = ConnectionString -> ConfigureWebHost
 
-    let configureServices 
-        (connectionFactory : DbConnectionFactory) 
-        (services : IServiceCollection) =
-        services.AddRouting() 
+let configure : ConfigureServer =
+    let configureLogging : ConfigureLogging =
+        fun log ->
+            log.ClearProviders()
+            |> ignore
+
+    let configureServices : ConfigureServices =
+        fun connectionFactory services ->
+            services
+                .AddRouting() 
                 .AddSingleton<DbConnectionFactory>(connectionFactory)
-        |> ignore
+            |> ignore
 
-    let configure 
-        (routes : HttpEndpoint list) 
-        (app : IApplicationBuilder) =      
-        let handleNotFound : HttpHandler =
-            fun ctx ->
-                Response.withStatusCode 404 ctx |> ignore
-                Task.CompletedTask
-            
-        app.UseRouting()
-           .UseHttpEndPoints(routes)       
-           .UseNotFoundHandler(handleNotFound)
-            |> ignore 
+    let configure : ConfigureApp =         
+        fun endpoints app ->
+            app.UseRouting()
+               .UseHttpEndPoints(endpoints)       
+               |> ignore 
 
-    fun (routes : HttpEndpoint list)
-        (webHost : IWebHostBuilder) ->
+    fun connectionString endpoints webHost ->    
+        let connectionFactory =     
+            fun () -> new NpgsqlConnection(connectionString) :> IDbConnection
+    
         webHost
             .UseKestrel()
             .ConfigureLogging(configureLogging)
             .ConfigureServices(configureServices connectionFactory)
-            .Configure(configure routes)                   
-            |> ignore
+            .Configure(configure endpoints)
+            |> ignore

+ 0 - 15
frameworks/FSharp/falco/src/App/Value.fs

@@ -1,15 +0,0 @@
-module App.Value
-
-open Falco 
-
-type JsonOutputModel = { message : string }
-
-let defaultMsg = "Hello, World!"
-
-let handleJson : HttpHandler =
-    fun ctx ->
-        let output = { message = defaultMsg }        
-        Response.ofJson output ctx
-        
-let handlePlainText : HttpHandler =        
-    Response.ofPlainText defaultMsg