Skip to content

Commit f6bb2e5

Browse files
committed
Move custom stuff into custom middleware
1 parent 1b27732 commit f6bb2e5

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

fsharp-backend/src/BwdServer/BwdServer.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</PropertyGroup>
1818
<ItemGroup>
1919
<None Include="paket.references" />
20+
<Compile Include="Compression.fs" />
2021
<Compile Include="Server.fs" />
2122
</ItemGroup>
2223
<ItemGroup>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module BwdServer.Compression
2+
3+
open FSharp.Control.Tasks
4+
open System.Threading.Tasks
5+
6+
open System
7+
open Microsoft.AspNetCore
8+
open Microsoft.AspNetCore.Builder
9+
open Microsoft.AspNetCore.Hosting
10+
open Microsoft.AspNetCore.Http
11+
open Microsoft.AspNetCore.Http.Extensions
12+
open Microsoft.AspNetCore.Http.Abstractions
13+
open Microsoft.Extensions.Logging
14+
open Microsoft.Extensions.Hosting
15+
open Microsoft.Extensions.DependencyInjection
16+
open Microsoft.AspNetCore.ResponseCompression
17+
open Microsoft.Extensions.DependencyInjection.Extensions
18+
19+
type CustomCompressionProvider(services, options) =
20+
inherit ResponseCompressionProvider(services, options)
21+
override this.ShouldCompressResponse(ctx : HttpContext) : bool =
22+
// Compress responses unless they're too small
23+
let default_ = base.ShouldCompressResponse ctx
24+
let tooSmall =
25+
// This was the setting we had in the ocaml nginx
26+
if ctx.Response.ContentLength.HasValue then
27+
ctx.Response.ContentLength.Value < 1024
28+
else
29+
false
30+
default_ && not tooSmall
31+
32+
let configureServices (services : IServiceCollection) : IServiceCollection =
33+
let configureOptions (options : ResponseCompressionOptions) : unit =
34+
// CLEANUP: This is set to the same values as we used in nginx for the ocaml
35+
// bwdserver. By default, .net also had a few others: text/javascript,
36+
// application/xml, text/xml, text/json, application/wasm. They aren't that
37+
// interesting to us right now.
38+
options.MimeTypes <-
39+
[ "text/html"
40+
"text/plain"
41+
"text/css"
42+
"application/javascript"
43+
"application/json" ]
44+
services.Configure(configureOptions) |> ignore<IServiceCollection>
45+
services.TryAddSingleton<IResponseCompressionProvider, CustomCompressionProvider>()
46+
services
47+
48+
let addToApp (app : IApplicationBuilder) : IApplicationBuilder =
49+
// FSTODO do we need to do anything to use our custom provider with the default middleware?
50+
// https://github.com/dotnet/aspnetcore/tree/c85baf8db0c72ae8e68643029d514b2e737c9fae/src/Middleware/ResponseCompression/src
51+
app.UseResponseCompression()

fsharp-backend/src/BwdServer/Server.fs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -472,47 +472,15 @@ let configureApp (healthCheckPort : int) (app : IApplicationBuilder) =
472472
(person, metadata)
473473

474474
Rollbar.AspNet.addRollbarToApp app rollbarCtxToMetadata None
475-
|> fun app -> app.UseResponseCompression()
475+
|> Compression.addToApp
476476
|> fun app -> app.UseRouting()
477477
// must go after UseRouting
478478
|> Kubernetes.configureApp healthCheckPort
479479
|> fun app -> app.Run(RequestDelegate handler)
480480

481-
type CustomCompressionProvider(services, options) =
482-
inherit ResponseCompressionProvider(services, options)
483-
override this.ShouldCompressResponse(ctx : HttpContext) : bool =
484-
// Compress responses unless they're too small
485-
let default_ = base.ShouldCompressResponse ctx
486-
let tooSmall =
487-
// This was the setting we had in the ocaml nginx
488-
if ctx.Response.ContentLength.HasValue then
489-
ctx.Response.ContentLength.Value < 1024
490-
else
491-
false
492-
default_ && not tooSmall
493-
494-
let configureResponseCompression
495-
(services : IServiceCollection)
496-
: IServiceCollection =
497-
let configureOptions (options : ResponseCompressionOptions) : unit =
498-
// CLEANUP: This is set to the same values as we used in nginx for the ocaml
499-
// bwdserver. By default, .net also has a few others: text/javascript,
500-
// application/xml, text/xml, text/json, application/wasm. They aren't that
501-
// interesting to us right now.
502-
options.MimeTypes <-
503-
[ "text/html"
504-
"text/plain"
505-
"text/css"
506-
"application/javascript"
507-
"application/json" ]
508-
services.Configure(configureOptions) |> ignore<IServiceCollection>
509-
services.TryAddSingleton<IResponseCompressionProvider, CustomCompressionProvider>()
510-
services
511-
512-
513481
let configureServices (services : IServiceCollection) : unit =
514482
services
515-
|> configureResponseCompression
483+
|> Compression.configureServices
516484
|> Kubernetes.configureServices [ LibBackend.Init.legacyServerCheck ]
517485
|> Rollbar.AspNet.addRollbarToServices
518486
|> Telemetry.AspNet.addTelemetryToServices "BwdServer" Telemetry.TraceDBQueries

0 commit comments

Comments
 (0)