Browse Source

FSharp/zebra instantiate and serialize JSON obj on each req (#5760)

Nate 5 years ago
parent
commit
d54294cef1
1 changed files with 24 additions and 24 deletions
  1. 24 24
      frameworks/FSharp/zebra/src/App/Program.fs

+ 24 - 24
frameworks/FSharp/zebra/src/App/Program.fs

@@ -15,7 +15,7 @@ open Dapper
 open System.Collections.Generic
 
 
-[<CLIMutable>][<Struct>] 
+[<CLIMutable>][<Struct>]
 type JsonStructMessage = { message : string }
 
  [<CLIMutable>]
@@ -26,11 +26,11 @@ let ConnectionString = "Server=tfb-database;Database=hello_world;User Id=benchma
 
 
 module Simple =
-    let textFn<'T> (text:string) = 
+    let textFn<'T> (text:string) =
         let bytes =         System.Text.Encoding.UTF8.GetBytes(text)
         let contentLength = Microsoft.Extensions.Primitives.StringValues(bytes.Length.ToString())
         let contentType =   Microsoft.Extensions.Primitives.StringValues "text/plain"
-        fun (x:State<'T>) -> 
+        fun (x:State<'T>) ->
             x.HttpContext.Response.Headers.["Content-Length"] <- contentLength
             x.HttpContext.Response.Headers.["Content-Type"] <- contentType
             let t = x.HttpContext.Response.Body.WriteAsync(bytes,0,bytes.Length)
@@ -38,11 +38,11 @@ module Simple =
             x.CurrentState <- MachineState.Complete
             awt.OnCompleted x.Continue
 
-    let inline jsonFn<'T> (value: ^a) =
-        let bytes =         Utf8Json.JsonSerializer.Serialize< ^a>(value)
-        let contentLength = Microsoft.Extensions.Primitives.StringValues(bytes.Length.ToString())
+    let inline jsonFn<'T> =
         let contentType =   Microsoft.Extensions.Primitives.StringValues "application/json"
-        fun (x:State<'T>) -> 
+        fun (x:State<'T>) ->
+            let bytes =         Utf8Json.JsonSerializer.Serialize< ^a>({ message = "Hello, World!" })
+            let contentLength = Microsoft.Extensions.Primitives.StringValues(bytes.Length.ToString())
             x.HttpContext.Response.Headers.["Content-Length"] <- contentLength
             x.HttpContext.Response.Headers.["Content-Type"] <- contentType
             let t = x.HttpContext.Response.Body.WriteAsync(bytes,0,bytes.Length)
@@ -51,14 +51,14 @@ module Simple =
             awt.OnCompleted x.Continue
 
 module View =
-     
+
     let fortuneView =
         html [] [
             head [] [
                 title []  [ rawText "Fortunes" ]
-            ]        
+            ]
             body [] [
-                table [] [ 
+                table [] [
                     tr [] [
                         th [] [ rawText "id" ]
                         th [] [ rawText "message" ]
@@ -74,29 +74,29 @@ module View =
         ] |> compileDoc
 
     let extra = { id = 0; message = "Additional fortune added at request time." }
-    let FortuneComparer = { new IComparer<Fortune> with 
+    let FortuneComparer = { new IComparer<Fortune> with
         member self.Compare(a,b) = String.CompareOrdinal(a.message, b.message)
 }
 
 [<EntryPoint>]
-let main args = 
+let main args =
 
-    // Defualt implimentation        
+    // Defualt implimentation
     let fallback : Zapp<_> = (fun ctx -> ctx {
         text "Url Not Found"
-        status 404        
+        status 404
     })
 
-    let webapp = 
+    let webapp =
         router [
             get "/plaintext" => fun ctx -> ctx { text "Hello, World!" }
             get "/json"      => fun ctx -> ctx { json {JsonStructMessage.message = "Hello, World!"} }
-            get "/fortunes"  => fun ctx -> ctx { 
+            get "/fortunes"  => fun ctx -> ctx {
                 use conn = new NpgsqlConnection(ConnectionString)
-                
+
                 let! (data : Fortune seq) = conn.QueryAsync<Fortune>("SELECT id, message FROM fortune")
-                
-                let fortunes = 
+
+                let fortunes =
                     let xs = data.AsList()
                     xs.Add View.extra
                     xs.Sort View.FortuneComparer
@@ -108,7 +108,7 @@ let main args =
 
     // Simple implimentation
     let plaintextPrint = Simple.textFn "Hello, World!"
-    let jsonPrint = Simple.jsonFn<_> {JsonStructMessage.message = "Hello, World!"}
+    let jsonPrint = Simple.jsonFn<_>
     let notFound = Simple.textFn "Not Found"
 
     let inline simpleApp (ctx:State<_>) =
@@ -120,16 +120,16 @@ let main args =
     // Config to used based on console arg
     let config : Action<Builder.IApplicationBuilder> =
             match args with
-            | [|"simple"|] -> 
+            | [|"simple"|] ->
                 printfn "Using Simple Config..."
                 Action<Builder.IApplicationBuilder>( fun app -> app.UseZebraSimpleMiddleware<int>(0,simpleApp) |> ignore )
-            | _            -> 
+            | _            ->
                 printfn "Using Stock Config..."
                 Action<Builder.IApplicationBuilder>( fun app -> app.UseZebraMiddleware<int>(0,fallback,webapp) |> ignore )
-        
+
     WebHostBuilder()
         .UseKestrel()
         .Configure(config)
         .Build()
         .Run()
-    0
+    0