Skip to content

Commit

Permalink
Implement intellifactory#6 Add OpenApi Middleware for SwaggerUI support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtank committed Sep 14, 2021
1 parent c25be12 commit 66dfb67
Show file tree
Hide file tree
Showing 10 changed files with 1,617 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Sitelets.Test/Giraffe/GiraffeTest.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FSharp.SystemTextJson" Version="0.17.4" />
<PackageReference Include="Giraffe" Version="5.0.0-rc-6" />
<PackageReference Include="Giraffe.ViewEngine" Version="1.3.*" />
<PackageReference Include="Ply" Version="0.3.*" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.1" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
</ItemGroup>

Expand Down
34 changes: 29 additions & 5 deletions Sitelets.Test/Giraffe/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ open Microsoft.AspNetCore.Mvc
open Microsoft.AspNetCore.Routing
open Microsoft.AspNetCore.Mvc.Abstractions
open System.Collections.Generic
open System.Text.Json
open System.Text.Json.Serialization

// ---------------------------------
// Models
Expand Down Expand Up @@ -68,7 +70,7 @@ let indexHandler (name : string) =
// ---------------------------------

type Endpoint =
| [<EndPoint "GET /echo">] Str of string
| [<EndPoint "GET /echo">] Str of msg:string
| [<EndPoint "GET /actionresult">] ActionResult
| [<EndPoint "GET /task/str">] Task1
| [<EndPoint "GET /task/actionresult">] Task2
Expand Down Expand Up @@ -126,32 +128,54 @@ let errorHandler (ex : Exception) (logger : ILogger) =
// ---------------------------------
// Config and Main
// ---------------------------------
let servers = [| "http://localhost:5000"; "https://localhost:5001" |]

let configureCors (builder : CorsPolicyBuilder) =
builder
.WithOrigins(
"http://localhost:5000",
"https://localhost:5001")
.WithOrigins(servers)
.AllowAnyMethod()
.AllowAnyHeader()
|> ignore

let serializerOptions =
let options = JsonSerializerOptions (PropertyNamingPolicy=JsonNamingPolicy.CamelCase)
options.Converters.Add( JsonStringEnumConverter())
options.Converters.Add(
JsonFSharpConverter(
JsonUnionEncoding.ExternalTag
||| JsonUnionEncoding.NamedFields
||| JsonUnionEncoding.UnwrapFieldlessTags
||| JsonUnionEncoding.UnwrapOption))
options

let configureApp (app : IApplicationBuilder) =
let env = app.ApplicationServices.GetService<IWebHostEnvironment>()
(match env.IsDevelopment() with
| true ->
app.UseDeveloperExceptionPage()
| false ->
app .UseGiraffeErrorHandler(errorHandler)
app
.UseGiraffeErrorHandler(errorHandler)
.UseHttpsRedirection())
.UseRouting()
.UseEndpoints(fun e ->
e.UseOpenApi(typeof<Endpoint>, {
Version = "1.0.0.0"
Title = "Sample Giraffe Test"
ServerUrls = servers
SerializerOptions = serializerOptions
}))
.UseCors(configureCors)
.UseSwaggerUI(fun c -> c.SwaggerEndpoint("/swagger.json", "My API V1"))
.UseStaticFiles()
.UseGiraffe(webApp)

let configureServices (services : IServiceCollection) =
services.AddCors() |> ignore
services.AddGiraffe() |> ignore
services.AddMvc() |> ignore
services.AddSingleton<Json.ISerializer>(SystemTextJson.Serializer(serializerOptions)) |> ignore


let configureLogging (builder : ILoggingBuilder) =
builder.AddConsole()
Expand Down
Loading

0 comments on commit 66dfb67

Please sign in to comment.