Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Jan 30, 2021
1 parent 8332004 commit f4d2d84
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 107 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ jobs:
uses: actions/checkout@v2

# Build the Docker image
- name: Build
- name: Build Docker image
run: |
PATTERN="refs/tags/v"
SUB=""
TAG="${GITHUB_REF/$PATTERN/$SUB}"
docker build -t "$PROJECT"/"$IMAGE":"$TAG" -f src/Giraffe.Website/Dockerfile .
docker build --build-arg version=$TAG -t "$PROJECT"/"$IMAGE":"$TAG" -f src/Giraffe.Website/Dockerfile .
# Auth with Docker Hub
- name: Login to Docker Hub
Expand All @@ -52,7 +52,7 @@ jobs:
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

# Push the Docker image to Docker Hub
- name: Publish
- name: Publish Docker image
run: |
PATTERN="refs/tags/v"
SUB=""
Expand Down
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Release Notes
=============

## 1.4.0

- Fixed versioning
- Smaller Docker image
- Toggle for HTTPS redirection

## 1.3.0

Updated project to .NET 5 and latest NuGet dependencies.
Expand Down
34 changes: 19 additions & 15 deletions src/Giraffe.Website/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,24 @@ module NetworkExtensions =
Threading.Tasks.Task.CompletedTask
| false -> next.Invoke())

member this.UseHttpsRedirection (domainName : string) =
this.Use(
fun ctx next ->
let host = ctx.Request.Host.Host
// Only HTTPS redirect for the chosen domain:
let mustUseHttps =
host = domainName
|| host.EndsWith ("." + domainName)
// Otherwise prevent the HTTP redirection middleware
// to redirect by force setting the scheme to https:
if not mustUseHttps then
ctx.Request.Scheme <- "https"
next.Invoke())
.UseHttpsRedirection()
member this.UseHttpsRedirection (isEnabled : bool, domainName : string) =
match isEnabled with
| true ->
this.Use(
fun ctx next ->
let host = ctx.Request.Host.Host
// Only HTTPS redirect for the chosen domain:
let mustUseHttps =
host = domainName
|| host.EndsWith ("." + domainName)
// Otherwise prevent the HTTP redirection middleware
// to redirect by force setting the scheme to https:
if not mustUseHttps then
ctx.Request.Scheme <- "https"
ctx.Request.IsHttps <- true
next.Invoke())
.UseHttpsRedirection()
| false -> this

// ---------------------------------
// Logging
Expand All @@ -265,7 +269,7 @@ module Logging =
summary.[category].Keys
|> Seq.toList
|> List.map(fun k -> k.Length)
|> List.sortByDescending (fun l -> l)
|> List.sortByDescending (id)
|> List.head
|> max len
) 0
Expand Down
12 changes: 7 additions & 5 deletions src/Giraffe.Website/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build

ARG version=0.0.0-undefined

WORKDIR /app

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

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine AS runtime

WORKDIR /app
COPY --from=build /app/published .
ENV ASPNETCORE_URLS http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "Giraffe.Website.dll"]
31 changes: 0 additions & 31 deletions src/Giraffe.Website/DotEnv.fs

This file was deleted.

26 changes: 10 additions & 16 deletions src/Giraffe.Website/Env.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module Env =
let LOG_LEVEL = "LOG_LEVEL"
let SENTRY_DSN = "SENTRY_DSN"
let DOMAIN_NAME = "DOMAIN_NAME"
let GOOGLE_ANALYTICS_KEY = "GOOGLE_ANALYTICS_KEY"
let GOOGLE_ANALYTICS_VIEWID = "GOOGLE_ANALYTICS_VIEWID"
let FORCE_HTTPS = "FORCE_HTTPS"
let ENABLE_REQUEST_LOGGING = "ENABLE_REQUEST_LOGGING"
let ENABLE_ERROR_ENDPOINT = "ENABLE_ERROR_ENDPOINT"
let PROXY_COUNT = "PROXY_COUNT"
Expand Down Expand Up @@ -67,21 +66,17 @@ module Env =
Keys.DOMAIN_NAME
"giraffe.wiki"

let forceHttps =
Config.typedEnvironmentVarOrDefault
None
Keys.FORCE_HTTPS
false

let baseUrl =
match isProduction with
| true -> sprintf "https://%s" domainName
| false -> "http://localhost:5000"

let googleAnalyticsKey =
Config.environmentVarOrDefault
Keys.GOOGLE_ANALYTICS_KEY
""

let googleAnalyticsViewId =
Config.environmentVarOrDefault
Keys.GOOGLE_ANALYTICS_VIEWID
""

let enableRequestLogging =
Config.InvariantCulture.typedEnvironmentVarOrDefault<bool>
Keys.ENABLE_REQUEST_LOGGING
Expand Down Expand Up @@ -126,14 +121,13 @@ module Env =
"Log Level", logLevel
"Sentry DSN", sentryDsn.ToSecret()
]
"Redirection", dict [
"Force HTTPS", forceHttps.ToString()
]
"URLs", dict [
"Domain", domainName
"Base URL", baseUrl
]
"Analytics", dict [
"Google Analytics key", googleAnalyticsKey.ToSecret()
"Google Analytics viewID", googleAnalyticsViewId
]
"Proxies", dict [
"Proxy count", proxyCount.ToString()
"Known proxies", knownProxies.ToPrettyString()
Expand Down
3 changes: 1 addition & 2 deletions src/Giraffe.Website/Giraffe.Website.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
</ItemGroup>

<ItemGroup>
<Compile Include="DotEnv.fs" />
<Compile Include="Common.fs" />
<Compile Include="Env.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<Content Include="Assets\**\*;Dockerfile">
<Content Include="Assets\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Expand Down
72 changes: 37 additions & 35 deletions src/Giraffe.Website/Program.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Giraffe.Website
open Microsoft.AspNetCore.Http

[<RequireQualifiedAccess>]
module Css =
Expand Down Expand Up @@ -325,50 +326,51 @@ module Main =
open Logfella.Adapters
open Logfella.AspNetCore

let private googleCloudLogWriter =
GoogleCloudLogWriter
.Create(Env.logSeverity)
.AddServiceContext(
Env.appName,
Env.appVersion)
.UseGoogleCloudTimestamp()
.AddLabels(
dict [
"appName", Env.appName
"appVersion", Env.appVersion
])

let private muteFilter =
Func<Severity, string, IDictionary<string, obj>, exn, bool>(
fun severity msg data ex ->
msg.StartsWith "The response could not be cached for this request")

let private defaultLogWriter =
Mute.When(muteFilter)
.Otherwise(
match Env.isProduction with
| false -> ConsoleLogWriter(Env.logSeverity).AsLogWriter()
| true -> googleCloudLogWriter.AsLogWriter())
let private createLogWriter (ctx : HttpContext option) =
match Env.isProduction with
| false -> ConsoleLogWriter(Env.logSeverity).AsLogWriter()
| true ->
let basic =
GoogleCloudLogWriter
.Create(Env.logSeverity)
.AddServiceContext(
Env.appName,
Env.appVersion)
.UseGoogleCloudTimestamp()
.AddLabels(
dict [
"appName", Env.appName
"appVersion", Env.appVersion
])
let final =
match ctx with
| None -> basic
| Some ctx ->
basic
.AddHttpContext(ctx)
.AddCorrelationId(Guid.NewGuid().ToString("N"))
Mute.When(muteFilter)
.Otherwise(final)

let private createReqLogWriter =
Func<HttpContext, ILogWriter>(Some >> createLogWriter)

let private toggleRequestLogging =
Action<RequestLoggingOptions>(
fun x -> x.IsEnabled <- Env.enableRequestLogging)

let configureApp (appBuilder : IApplicationBuilder) =
appBuilder
.UseGiraffeErrorHandler(WebApp.errorHandler)
.UseWhen(
(fun _ -> Env.isProduction),
fun x ->
x.UseRequestScopedLogWriter(
fun ctx ->
Mute.When(muteFilter)
.Otherwise(
googleCloudLogWriter
.AddHttpContext(ctx)
.AddCorrelationId(Guid.NewGuid().ToString("N"))
.AsLogWriter()))
|> ignore)
.UseRequestLogging(
fun o -> o.IsEnabled <- Env.enableRequestLogging)
.UseRequestScopedLogWriter(createReqLogWriter)
.UseRequestLogging(toggleRequestLogging)
.UseForwardedHeaders()
.UseHttpsRedirection(Env.domainName)
.UseHttpsRedirection(Env.forceHttps, Env.domainName)
.UseTrailingSlashRedirection()
.UseStaticFiles()
.UseResponseCaching()
Expand All @@ -394,7 +396,7 @@ module Main =
[<EntryPoint>]
let main args =
try
Log.SetDefaultLogWriter(defaultLogWriter)
Log.SetDefaultLogWriter(createLogWriter None)
Logging.outputEnvironmentSummary Env.summary

Host.CreateDefaultBuilder(args)
Expand Down

0 comments on commit f4d2d84

Please sign in to comment.