Skip to content

Commit f4d2d84

Browse files
committed
Improvements
1 parent 8332004 commit f4d2d84

File tree

8 files changed

+83
-107
lines changed

8 files changed

+83
-107
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ jobs:
3737
uses: actions/checkout@v2
3838

3939
# Build the Docker image
40-
- name: Build
40+
- name: Build Docker image
4141
run: |
4242
PATTERN="refs/tags/v"
4343
SUB=""
4444
TAG="${GITHUB_REF/$PATTERN/$SUB}"
45-
docker build -t "$PROJECT"/"$IMAGE":"$TAG" -f src/Giraffe.Website/Dockerfile .
45+
docker build --build-arg version=$TAG -t "$PROJECT"/"$IMAGE":"$TAG" -f src/Giraffe.Website/Dockerfile .
4646
4747
# Auth with Docker Hub
4848
- name: Login to Docker Hub
@@ -52,7 +52,7 @@ jobs:
5252
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
5353

5454
# Push the Docker image to Docker Hub
55-
- name: Publish
55+
- name: Publish Docker image
5656
run: |
5757
PATTERN="refs/tags/v"
5858
SUB=""

RELEASE_NOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release Notes
22
=============
33

4+
## 1.4.0
5+
6+
- Fixed versioning
7+
- Smaller Docker image
8+
- Toggle for HTTPS redirection
9+
410
## 1.3.0
511

612
Updated project to .NET 5 and latest NuGet dependencies.

src/Giraffe.Website/Common.fs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,24 @@ module NetworkExtensions =
231231
Threading.Tasks.Task.CompletedTask
232232
| false -> next.Invoke())
233233

234-
member this.UseHttpsRedirection (domainName : string) =
235-
this.Use(
236-
fun ctx next ->
237-
let host = ctx.Request.Host.Host
238-
// Only HTTPS redirect for the chosen domain:
239-
let mustUseHttps =
240-
host = domainName
241-
|| host.EndsWith ("." + domainName)
242-
// Otherwise prevent the HTTP redirection middleware
243-
// to redirect by force setting the scheme to https:
244-
if not mustUseHttps then
245-
ctx.Request.Scheme <- "https"
246-
next.Invoke())
247-
.UseHttpsRedirection()
234+
member this.UseHttpsRedirection (isEnabled : bool, domainName : string) =
235+
match isEnabled with
236+
| true ->
237+
this.Use(
238+
fun ctx next ->
239+
let host = ctx.Request.Host.Host
240+
// Only HTTPS redirect for the chosen domain:
241+
let mustUseHttps =
242+
host = domainName
243+
|| host.EndsWith ("." + domainName)
244+
// Otherwise prevent the HTTP redirection middleware
245+
// to redirect by force setting the scheme to https:
246+
if not mustUseHttps then
247+
ctx.Request.Scheme <- "https"
248+
ctx.Request.IsHttps <- true
249+
next.Invoke())
250+
.UseHttpsRedirection()
251+
| false -> this
248252

249253
// ---------------------------------
250254
// Logging
@@ -265,7 +269,7 @@ module Logging =
265269
summary.[category].Keys
266270
|> Seq.toList
267271
|> List.map(fun k -> k.Length)
268-
|> List.sortByDescending (fun l -> l)
272+
|> List.sortByDescending (id)
269273
|> List.head
270274
|> max len
271275
) 0

src/Giraffe.Website/Dockerfile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
1+
FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
2+
3+
ARG version=0.0.0-undefined
4+
25
WORKDIR /app
36

47
# Copy everything and build
58
COPY src/ ./
6-
RUN dotnet publish Giraffe.Website/Giraffe.Website.fsproj -c Release -o published
9+
RUN dotnet publish /p:Version=$version Giraffe.Website/Giraffe.Website.fsproj -c Release -o published
710

811
# Build runtime image
9-
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
12+
FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine AS runtime
13+
1014
WORKDIR /app
1115
COPY --from=build /app/published .
12-
ENV ASPNETCORE_URLS http://+:8080
13-
EXPOSE 8080
1416
ENTRYPOINT ["dotnet", "Giraffe.Website.dll"]

src/Giraffe.Website/DotEnv.fs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Giraffe.Website/Env.fs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ module Env =
1414
let LOG_LEVEL = "LOG_LEVEL"
1515
let SENTRY_DSN = "SENTRY_DSN"
1616
let DOMAIN_NAME = "DOMAIN_NAME"
17-
let GOOGLE_ANALYTICS_KEY = "GOOGLE_ANALYTICS_KEY"
18-
let GOOGLE_ANALYTICS_VIEWID = "GOOGLE_ANALYTICS_VIEWID"
17+
let FORCE_HTTPS = "FORCE_HTTPS"
1918
let ENABLE_REQUEST_LOGGING = "ENABLE_REQUEST_LOGGING"
2019
let ENABLE_ERROR_ENDPOINT = "ENABLE_ERROR_ENDPOINT"
2120
let PROXY_COUNT = "PROXY_COUNT"
@@ -67,21 +66,17 @@ module Env =
6766
Keys.DOMAIN_NAME
6867
"giraffe.wiki"
6968

69+
let forceHttps =
70+
Config.typedEnvironmentVarOrDefault
71+
None
72+
Keys.FORCE_HTTPS
73+
false
74+
7075
let baseUrl =
7176
match isProduction with
7277
| true -> sprintf "https://%s" domainName
7378
| false -> "http://localhost:5000"
7479

75-
let googleAnalyticsKey =
76-
Config.environmentVarOrDefault
77-
Keys.GOOGLE_ANALYTICS_KEY
78-
""
79-
80-
let googleAnalyticsViewId =
81-
Config.environmentVarOrDefault
82-
Keys.GOOGLE_ANALYTICS_VIEWID
83-
""
84-
8580
let enableRequestLogging =
8681
Config.InvariantCulture.typedEnvironmentVarOrDefault<bool>
8782
Keys.ENABLE_REQUEST_LOGGING
@@ -126,14 +121,13 @@ module Env =
126121
"Log Level", logLevel
127122
"Sentry DSN", sentryDsn.ToSecret()
128123
]
124+
"Redirection", dict [
125+
"Force HTTPS", forceHttps.ToString()
126+
]
129127
"URLs", dict [
130128
"Domain", domainName
131129
"Base URL", baseUrl
132130
]
133-
"Analytics", dict [
134-
"Google Analytics key", googleAnalyticsKey.ToSecret()
135-
"Google Analytics viewID", googleAnalyticsViewId
136-
]
137131
"Proxies", dict [
138132
"Proxy count", proxyCount.ToString()
139133
"Known proxies", knownProxies.ToPrettyString()

src/Giraffe.Website/Giraffe.Website.fsproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<Compile Include="DotEnv.fs" />
2120
<Compile Include="Common.fs" />
2221
<Compile Include="Env.fs" />
2322
<Compile Include="Program.fs" />
2423
</ItemGroup>
2524

2625
<ItemGroup>
27-
<Content Include="Assets\**\*;Dockerfile">
26+
<Content Include="Assets\**\*">
2827
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2928
</Content>
3029
</ItemGroup>

src/Giraffe.Website/Program.fs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace Giraffe.Website
2+
open Microsoft.AspNetCore.Http
23

34
[<RequireQualifiedAccess>]
45
module Css =
@@ -325,50 +326,51 @@ module Main =
325326
open Logfella.Adapters
326327
open Logfella.AspNetCore
327328

328-
let private googleCloudLogWriter =
329-
GoogleCloudLogWriter
330-
.Create(Env.logSeverity)
331-
.AddServiceContext(
332-
Env.appName,
333-
Env.appVersion)
334-
.UseGoogleCloudTimestamp()
335-
.AddLabels(
336-
dict [
337-
"appName", Env.appName
338-
"appVersion", Env.appVersion
339-
])
340-
341329
let private muteFilter =
342330
Func<Severity, string, IDictionary<string, obj>, exn, bool>(
343331
fun severity msg data ex ->
344332
msg.StartsWith "The response could not be cached for this request")
345333

346-
let private defaultLogWriter =
347-
Mute.When(muteFilter)
348-
.Otherwise(
349-
match Env.isProduction with
350-
| false -> ConsoleLogWriter(Env.logSeverity).AsLogWriter()
351-
| true -> googleCloudLogWriter.AsLogWriter())
334+
let private createLogWriter (ctx : HttpContext option) =
335+
match Env.isProduction with
336+
| false -> ConsoleLogWriter(Env.logSeverity).AsLogWriter()
337+
| true ->
338+
let basic =
339+
GoogleCloudLogWriter
340+
.Create(Env.logSeverity)
341+
.AddServiceContext(
342+
Env.appName,
343+
Env.appVersion)
344+
.UseGoogleCloudTimestamp()
345+
.AddLabels(
346+
dict [
347+
"appName", Env.appName
348+
"appVersion", Env.appVersion
349+
])
350+
let final =
351+
match ctx with
352+
| None -> basic
353+
| Some ctx ->
354+
basic
355+
.AddHttpContext(ctx)
356+
.AddCorrelationId(Guid.NewGuid().ToString("N"))
357+
Mute.When(muteFilter)
358+
.Otherwise(final)
359+
360+
let private createReqLogWriter =
361+
Func<HttpContext, ILogWriter>(Some >> createLogWriter)
362+
363+
let private toggleRequestLogging =
364+
Action<RequestLoggingOptions>(
365+
fun x -> x.IsEnabled <- Env.enableRequestLogging)
352366

353367
let configureApp (appBuilder : IApplicationBuilder) =
354368
appBuilder
355369
.UseGiraffeErrorHandler(WebApp.errorHandler)
356-
.UseWhen(
357-
(fun _ -> Env.isProduction),
358-
fun x ->
359-
x.UseRequestScopedLogWriter(
360-
fun ctx ->
361-
Mute.When(muteFilter)
362-
.Otherwise(
363-
googleCloudLogWriter
364-
.AddHttpContext(ctx)
365-
.AddCorrelationId(Guid.NewGuid().ToString("N"))
366-
.AsLogWriter()))
367-
|> ignore)
368-
.UseRequestLogging(
369-
fun o -> o.IsEnabled <- Env.enableRequestLogging)
370+
.UseRequestScopedLogWriter(createReqLogWriter)
371+
.UseRequestLogging(toggleRequestLogging)
370372
.UseForwardedHeaders()
371-
.UseHttpsRedirection(Env.domainName)
373+
.UseHttpsRedirection(Env.forceHttps, Env.domainName)
372374
.UseTrailingSlashRedirection()
373375
.UseStaticFiles()
374376
.UseResponseCaching()
@@ -394,7 +396,7 @@ module Main =
394396
[<EntryPoint>]
395397
let main args =
396398
try
397-
Log.SetDefaultLogWriter(defaultLogWriter)
399+
Log.SetDefaultLogWriter(createLogWriter None)
398400
Logging.outputEnvironmentSummary Env.summary
399401

400402
Host.CreateDefaultBuilder(args)

0 commit comments

Comments
 (0)