Skip to content
This repository was archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Feb 7, 2021
2 parents c7615bd + cc7f061 commit 177cde8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 66 deletions.
21 changes: 15 additions & 6 deletions src/DustedCodes/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ module Config =
{
ProxyCount : int
FwdIPHeaderName : string
TraceIdHeaderName : string
}
static member Load() =
{
ProxyCount = Env.InvariantCulture.typedVarOrDefault "PROXY_COUNT" 2
FwdIPHeaderName = Env.varOrDefault "FORWARDED_IP_HEADER_NAME" "X-Forwarded-For"
TraceIdHeaderName = Env.varOrDefault "TRACE_ID_HEADER_NAME" "X-Trace-Id"
}

[<RequireQualifiedAccess>]
Expand Down Expand Up @@ -118,10 +116,19 @@ module Config =
CaptchaSecretKey = Env.varOrDefault "CAPTCHA_SECRETKEY" ""
}

[<RequireQualifiedAccess>]
type GCP =
{
ProjectId : string
}
static member Load() =
{
ProjectId = Env.varOrDefault "GCP_PROJECT_ID" ""
}

[<RequireQualifiedAccess>]
type Mail =
{
GcpProjectId : string
GcpDatastoreKind : string
GcpPubSubTopic : string
Domain : string
Expand All @@ -130,7 +137,6 @@ module Config =
}
static member Load() =
{
GcpProjectId = Env.varOrDefault "GCP_PROJECT_ID" ""
GcpDatastoreKind = Env.varOrDefault "GCP_DS_CONTACT_MESSAGE_KIND" ""
GcpPubSubTopic = Env.varOrDefault "GCP_PS_EMAILS_TOPIC" ""
Domain = Env.varOrDefault "MAIL_DOMAIN" ""
Expand Down Expand Up @@ -165,6 +171,7 @@ module Config =
Proxy : Proxy
Blog : Blog
ThirdParties : ThirdParties
GCP : GCP
Mail : Mail
Redis : Redis
}
Expand Down Expand Up @@ -193,7 +200,6 @@ module Config =
"Proxy", dict [
"Proxy Count", this.Proxy.ProxyCount.ToString()
"Forwarded IP header name", this.Proxy.FwdIPHeaderName
"Trace ID header name", this.Proxy.TraceIdHeaderName
]
"Blog", dict [
"Blog Title", this.Blog.Title
Expand All @@ -210,8 +216,10 @@ module Config =
"Captcha site key", this.ThirdParties.CaptchaSiteKey
"Captcha secret key", this.ThirdParties.CaptchaSecretKey.ToSecret()
]
"GCP", dict [
"Project ID", this.GCP.ProjectId
]
"Mail", dict [
"GCP Project ID", this.Mail.GcpProjectId
"GCP Datastore Kind", this.Mail.GcpDatastoreKind
"GCP PubSub Topic", this.Mail.GcpPubSubTopic
"Mail Domain", this.Mail.Domain
Expand Down Expand Up @@ -268,6 +276,7 @@ module Config =
Proxy = Proxy.Load()
Blog = Blog.Load()
ThirdParties = ThirdParties.Load()
GCP = GCP.Load()
Mail = Mail.Load()
Redis = Redis.Load()
} : Settings
Expand Down
3 changes: 2 additions & 1 deletion src/DustedCodes/DustedCodes.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<Compile Include="RSS.fs" />
<Compile Include="Minification.fs" />
<Compile Include="Views.fs" />
<Compile Include="Web.fs" />
<Compile Include="HttpHandlers.fs" />
<Compile Include="Router.fs" />
<Compile Include="Middlewares.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
Expand Down
57 changes: 2 additions & 55 deletions src/DustedCodes/Web.fs → src/DustedCodes/HttpHandlers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ module HttpHandlers =

let contact (next : HttpFunc) =
let internalErr =
"Unfortunately the message failed to send due to an unexpected error. "
+ "The website owner has been notified about the issue. "
+ "Please try a little bit later again."
|> Error
Error "An unexpected error occurred and the message could not be sent. Please try a little bit later again."
fun (ctx : HttpContext) ->
task {
let settings = ctx.GetService<Config.Settings>()
Expand Down Expand Up @@ -262,54 +259,4 @@ module HttpHandlers =
fun (next : HttpFunc) (ctx : HttpContext) ->
let log = ctx.GetLogFunc()
log Level.Warning "Someone tried to subscribe to the Atom feed."
ServerErrors.notImplemented (text "Atom feed is not supported at the moment. If you were using Atom to subscribe to this blog before, please file an issue on https://github.com/dustinmoris/DustedCodes to create awareness.") next ctx

[<RequireQualifiedAccess>]
module WebApp =
open System
open Microsoft.Extensions.Logging
open Giraffe
open Giraffe.EndpointRouting

let endpoints (settings : Config.Settings) : Endpoint list =
[
GET_HEAD [
// Static assets
route UrlPaths.``/logo.svg`` (HttpHandlers.svg Icons.logo)
routef "/images/link-%s.svg" (Icons.link >> HttpHandlers.svg)
routef "/bundle.%s.css" (fun _ -> HttpHandlers.css)

// Health check
route UrlPaths.``/ping`` HttpHandlers.pingPong
route UrlPaths.``/version`` HttpHandlers.version

// Debug
if settings.Web.ErrorEndpoint then
route UrlPaths.Debug.``/error`` (warbler (fun _ -> json(1/0)))

// Content paths
route UrlPaths.``/`` HttpHandlers.index
route UrlPaths.``/about`` (HttpHandlers.markdown "About.md" (Views.about settings))
route UrlPaths.``/hire`` HttpHandlers.hire
route UrlPaths.``/trending`` HttpHandlers.trending

route UrlPaths.``/feed/rss`` HttpHandlers.rssFeed
route UrlPaths.``/feed/atom`` HttpHandlers.atomFeed

// Deprecated URLs kept alive in order to not break
// existing links in the world wide web
route UrlPaths.Deprecated.``/archive`` HttpHandlers.index

// Keeping old links still working
// (From observing 404 errors in GCP)
route
"/demystifying-aspnet-mvc-5-error-pages"
(redirectTo true "/demystifying-aspnet-mvc-5-error-pages-and-error-logging")

routef UrlPaths.``/tagged/%s`` HttpHandlers.tagged
routef UrlPaths.``/%s`` HttpHandlers.blogPost
]
POST [
route UrlPaths.``/hire`` HttpHandlers.contact
]
]
ServerErrors.notImplemented (text "Atom feed is not supported at the moment. If you were using Atom to subscribe to this blog before, please file an issue on https://github.com/dustinmoris/DustedCodes to create awareness.") next ctx
8 changes: 4 additions & 4 deletions src/DustedCodes/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ module Program =

let topicName =
TopicName(
settings.Mail.GcpProjectId,
settings.GCP.ProjectId,
settings.Mail.GcpPubSubTopic)
pubSubClient <- PublisherClient.CreateAsync(topicName).Result
let dsClient = DatastoreDb.Create settings.Mail.GcpProjectId
let dsClient = DatastoreDb.Create settings.GCP.ProjectId

let saveEntityFunc =
Datastore.saveEntity
Expand All @@ -46,7 +46,7 @@ module Program =
services
.AddHttpClient(Http.clientName)
.AddOutgoingGoogleTraceHandler().Services
.AddGoogleTrace(fun x -> x.ProjectId <- settings.Mail.GcpProjectId)
.AddGoogleTrace(fun x -> x.ProjectId <- settings.GCP.ProjectId)
.AddSingleton(settings)
.AddSingleton<GoogleAnalytics.GetReportFunc>(getReportFunc)
.AddSingleton<Messages.SaveFunc>(Messages.save saveEntityFunc publishMsgFunc)
Expand Down Expand Up @@ -77,7 +77,7 @@ module Program =
.UseStaticFiles()
.UseResponseCompression()
.UseRouting()
.UseGiraffe(WebApp.endpoints settings)
.UseGiraffe(Router.endpoints settings)
.UseGiraffe(HttpHandlers.notFound)

[<EntryPoint>]
Expand Down
49 changes: 49 additions & 0 deletions src/DustedCodes/Router.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace DustedCodes

[<RequireQualifiedAccess>]
module Router =
open Giraffe
open Giraffe.EndpointRouting

let endpoints (settings : Config.Settings) : Endpoint list =
[
GET_HEAD [
// Static assets
route UrlPaths.``/logo.svg`` (HttpHandlers.svg Icons.logo)
routef "/images/link-%s.svg" (Icons.link >> HttpHandlers.svg)
routef "/bundle.%s.css" (fun _ -> HttpHandlers.css)

// Health check
route UrlPaths.``/ping`` HttpHandlers.pingPong
route UrlPaths.``/version`` HttpHandlers.version

// Debug
if settings.Web.ErrorEndpoint then
route UrlPaths.Debug.``/error`` (warbler (fun _ -> json(1/0)))

// Content paths
route UrlPaths.``/`` HttpHandlers.index
route UrlPaths.``/about`` (HttpHandlers.markdown "About.md" (Views.about settings))
route UrlPaths.``/hire`` HttpHandlers.hire
route UrlPaths.``/trending`` HttpHandlers.trending

route UrlPaths.``/feed/rss`` HttpHandlers.rssFeed
route UrlPaths.``/feed/atom`` HttpHandlers.atomFeed

// Deprecated URLs kept alive in order to not break
// existing links in the world wide web
route UrlPaths.Deprecated.``/archive`` HttpHandlers.index

// Keeping old links still working
// (From observing 404 errors in GCP)
route
"/demystifying-aspnet-mvc-5-error-pages"
(redirectTo true "/demystifying-aspnet-mvc-5-error-pages-and-error-logging")

routef UrlPaths.``/tagged/%s`` HttpHandlers.tagged
routef UrlPaths.``/%s`` HttpHandlers.blogPost
]
POST [
route UrlPaths.``/hire`` HttpHandlers.contact
]
]

0 comments on commit 177cde8

Please sign in to comment.