Browse Source

FSharp giraffe (#3859)

Dmitry Kushnir 7 years ago
parent
commit
2669ae461f

+ 1 - 0
.travis.yml

@@ -28,6 +28,7 @@ env:
      - "TESTLANG=Dart"
      - "TESTLANG=Dart"
      - "TESTLANG=Elixir"
      - "TESTLANG=Elixir"
      - "TESTLANG=Erlang"
      - "TESTLANG=Erlang"
+     - "TESTLANG=FSharp"
      - "TESTLANG=Go"
      - "TESTLANG=Go"
      - "TESTLANG=Groovy"
      - "TESTLANG=Groovy"
      - "TESTDIR=Haskell/snap"
      - "TESTDIR=Haskell/snap"

+ 38 - 0
frameworks/FSharp/giraffe/.gitignore

@@ -0,0 +1,38 @@
+[Oo]bj/
+[Bb]in/
+TestResults/
+.nuget/
+*.sln.ide/
+_ReSharper.*/
+.idea/
+packages/
+artifacts/
+PublishProfiles/
+.vs/
+*.user
+*.suo
+*.cache
+*.docstates
+_ReSharper.*
+nuget.exe
+*net45.csproj
+*net451.csproj
+*k10.csproj
+*.psess
+*.vsp
+*.pidb
+*.userprefs
+*DS_Store
+*.ncrunchsolution
+*.*sdf
+*.ipch
+*.swp
+*~
+.build/
+.testPublish/
+launchSettings.json
+BenchmarkDotNet.Artifacts/
+BDN.Generated/
+binaries/
+global.json
+*.sln

+ 27 - 0
frameworks/FSharp/giraffe/README.md

@@ -0,0 +1,27 @@
+# girrafe Tests on Linux
+This includes tests for plaintext and json serialization.
+
+## Infrastructure Software Versions
+
+**Language**
+
+* F# 4.1
+
+**Platforms**
+
+* .NET Core (Windows and Linux)
+
+**Web Servers**
+
+* [Kestrel](https://github.com/aspnet/KestrelHttpServer)
+
+**Web Stack**
+
+* [giraffe](https://github.com/giraffe-fsharp/Giraffe)
+* ASP.NET Core
+
+## Paths & Source for Tests
+
+* [Plaintext](src/App/Programs.fs): "/plaintext"
+* [JSON serialization](src/App/Programs.fs): "/json"
+* [JSON serialization via utf8json lib](src/App/Programs.fs): "/jsonutf8"

+ 66 - 0
frameworks/FSharp/giraffe/benchmark_config.json

@@ -0,0 +1,66 @@
+{
+  "framework": "giraffe",
+  "tests": [
+    {
+      "default": {
+        "plaintext_url": "/plaintext",
+        "json_url": "/json",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "fullstack",
+        "database": "None",
+        "framework": "giraffe",
+        "language": "F#",
+        "orm": "Raw",
+        "platform": ".NET",
+        "flavor": "CoreCLR",
+        "webserver": "Kestrel",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Giraffe",
+        "notes": "",
+        "versus": "aspcore"
+      }
+    },
+    {
+      "utf8json": {
+        "json_url": "/json",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "fullstack",
+        "database": "None",
+        "framework": "giraffe",
+        "language": "F#",
+        "orm": "Raw",
+        "platform": ".NET",
+        "flavor": "CoreCLR",
+        "webserver": "Kestrel",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Giraffe",
+        "notes": "",
+        "versus": "aspcore"
+      }
+    },
+    {
+      "utf8plaintext": {
+        "plaintext_url": "/plaintext",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "fullstack",
+        "database": "None",
+        "framework": "giraffe",
+        "language": "F#",
+        "orm": "Raw",
+        "platform": ".NET",
+        "flavor": "CoreCLR",
+        "webserver": "Kestrel",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Giraffe",
+        "notes": "",
+        "versus": "aspcore"
+      }
+    }
+  ]
+}

+ 12 - 0
frameworks/FSharp/giraffe/giraffe-utf8json.dockerfile

@@ -0,0 +1,12 @@
+FROM microsoft/dotnet:2.1-sdk-stretch AS build
+WORKDIR /app
+COPY src/App .
+RUN dotnet publish -c Release -o out
+
+FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
+ENV ASPNETCORE_URLS http://+:8080
+ENV COMPlus_ReadyToRun 0
+WORKDIR /app
+COPY --from=build /app/out ./
+
+ENTRYPOINT ["dotnet", "App.dll"]

+ 12 - 0
frameworks/FSharp/giraffe/giraffe-utf8plaintext.dockerfile

@@ -0,0 +1,12 @@
+FROM microsoft/dotnet:2.1-sdk-stretch AS build
+WORKDIR /app
+COPY src/App .
+RUN dotnet publish -c Release -o out
+
+FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
+ENV ASPNETCORE_URLS http://+:8080
+ENV COMPlus_ReadyToRun 0
+WORKDIR /app
+COPY --from=build /app/out ./
+
+ENTRYPOINT ["dotnet", "App.dll"]

+ 12 - 0
frameworks/FSharp/giraffe/giraffe.dockerfile

@@ -0,0 +1,12 @@
+FROM microsoft/dotnet:2.1-sdk-stretch AS build
+WORKDIR /app
+COPY src/App .
+RUN dotnet publish -c Release -o out
+
+FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
+ENV ASPNETCORE_URLS http://+:8080
+ENV COMPlus_ReadyToRun 0
+WORKDIR /app
+COPY --from=build /app/out ./
+
+ENTRYPOINT ["dotnet", "App.dll", "stock"]

+ 27 - 0
frameworks/FSharp/giraffe/src/App/App.fsproj

@@ -0,0 +1,27 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+  <PropertyGroup>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <DebugType>portable</DebugType>
+    <AssemblyName>App</AssemblyName>
+    <OutputType>Exe</OutputType>
+    <EnableDefaultContentItems>false</EnableDefaultContentItems>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.0" />
+    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.0" />
+    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
+    <PackageReference Include="Giraffe" Version="1.1.0" />
+    <PackageReference Include="Utf8Json" Version="1.3.7" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <Compile Include="Program.fs" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Update="FSharp.Core" Version="4.5.0" />
+  </ItemGroup>
+
+</Project>

+ 62 - 0
frameworks/FSharp/giraffe/src/App/Program.fs

@@ -0,0 +1,62 @@
+module App.App
+
+open Microsoft.AspNetCore.Hosting
+open Giraffe
+
+[<CLIMutable>] 
+type JsonMessage = { message : string }
+
+[<CLIMutable>][<Struct>] 
+type JsonStructMessage = { message : string }
+
+type Implementation = Stock | Custom
+
+module Routes =
+
+    let stock : HttpHandler list = 
+        [ route "/plaintext" >=> text "Hello, World!"
+          route "/json" >=> json { JsonMessage.message = "Hello, World!" } ]
+
+    let custom : HttpHandler list = 
+        let inline contentLength (x:int32) = new System.Nullable<int64>( int64 x )
+
+        let json data : HttpHandler =
+            let bytes = Utf8Json.JsonSerializer.Serialize(data)
+            fun _ ctx ->
+                ctx.Response.ContentLength <- contentLength ( bytes.Length )
+                ctx.Response.ContentType <- "application/json"
+                ctx.Response.StatusCode <- 200
+                ctx.WriteBytesAsync bytes
+
+        let bytes = System.Text.Encoding.UTF8.GetBytes "Hello, World!"
+        let text : HttpHandler = 
+            fun _ ctx ->
+                ctx.Response.ContentLength <- contentLength ( bytes.Length )
+                ctx.Response.ContentType <- "text/plain"
+                ctx.Response.StatusCode <- 200
+                ctx.WriteBytesAsync bytes
+    
+        [ route "/plaintext" >=> text
+          route "/json" >=> json { JsonStructMessage.message = "Hello, World!" } ]
+
+let webApp implementation = 
+    match implementation with
+    | Stock -> GET >=> choose Routes.stock
+    | Custom -> GET >=> choose Routes.custom
+
+[<EntryPoint>]
+let main args = 
+    let implementation = 
+        match args with
+        | [| "stock" |]  -> Stock
+        | _ -> Custom
+
+    printfn "Running with %A implementation" implementation
+
+    WebHostBuilder()
+        .UseKestrel()
+        .Configure(fun app -> app.UseGiraffe (webApp implementation))
+        .ConfigureServices(fun services -> services.AddGiraffe() |> ignore)
+        .Build()
+        .Run()
+    0