Skip to content

Commit

Permalink
Cleanup namespacing and dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
dam5s committed Mar 4, 2024
1 parent 419a26c commit 052d5c1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 46 deletions.
1 change: 1 addition & 0 deletions Damo.Io.Server/Damo.Io.Server.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Include="src\ArticleListTemplate.fs" />
<Compile Include="src\ArticlesHandler.fs" />
<Compile Include="src\FeedsProcessor.fs" />
<Compile Include="src\BackgroundProcessor.fs" />
<Compile Include="src\App.fs" />
<Compile Include="src\Program.fs" />
</ItemGroup>
Expand Down
40 changes: 11 additions & 29 deletions Damo.Io.Server/src/App.fs
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
module DamoIoServer.App

open System
open DamoIoServer
open DamoIoServer.SourcesRepository
open FSharp.Control
open Giraffe
open Microsoft.Extensions.Logging

open DamoIoServer.ArticlesRepository

let private updatesSequence logger =
asyncSeq {
let tenMinutes = 10 * 1000 * 60
[<RequireQualifiedAccess>]
module App =
let handler: HttpHandler =
choose
[ GET >=> route "/" >=> redirectTo false "/About,Social,Blog"
GET >=> routef "/%s" (ArticlesHandler.list ArticlesRepository.findAllBySources)
setStatusCode 404 >=> text "Not Found" ]

while true do
let! newArticles =
SourcesRepository.findAll ()
|> FeedsProcessor.processFeeds logger
|> AsyncSeq.toListAsync

yield newArticles

do! Async.Sleep tenMinutes
}

let backgroundProcessing logger =
AsyncSeq.iter ArticlesRepository.updateAll (updatesSequence logger)

let handler: HttpHandler =
choose
[ GET >=> route "/" >=> redirectTo false "/About,Social,Blog"
GET >=> routef "/%s" (ArticlesHandler.list ArticlesRepository.findAllBySources)
setStatusCode 404 >=> text "Not Found" ]

let errorHandler (ex: Exception) (logger: ILogger) : HttpHandler =
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> setStatusCode 500 >=> text ex.Message
let errorHandler (ex: Exception) (logger: ILogger) : HttpHandler =
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> setStatusCode 500 >=> text ex.Message
15 changes: 8 additions & 7 deletions Damo.Io.Server/src/AppConfig.fs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[<RequireQualifiedAccess>]
module DamoIoServer.AppConfig

open System.IO

let serverPort = Env.varDefault "PORT" (always "9000")
[<RequireQualifiedAccess>]
module AppConfig =
let serverPort = Env.varDefault "PORT" (always "9000")

let contentRoot = Env.varDefault "CONTENT_ROOT" Directory.GetCurrentDirectory
let contentRoot = Env.varDefault "CONTENT_ROOT" Directory.GetCurrentDirectory

let assetMinificationDisabled =
Env.var "DISABLE_MINIFIED_ASSETS" |> Option.hasValue "true"
let assetMinificationDisabled =
Env.var "DISABLE_MINIFIED_ASSETS" |> Option.hasValue "true"

let assetMinificationEnabled = not assetMinificationDisabled
let assetMinificationEnabled = not assetMinificationDisabled

let enableExceptionPage = Env.var "ENABLE_EXCEPTION_PAGE" |> Option.hasValue "true"
let enableExceptionPage = Env.var "ENABLE_EXCEPTION_PAGE" |> Option.hasValue "true"
15 changes: 9 additions & 6 deletions Damo.Io.Server/src/ArticlesRepository.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[<RequireQualifiedAccess>]
module DamoIoServer.ArticlesRepository

open DamoIoServer.Source
Expand Down Expand Up @@ -51,11 +50,15 @@ let private about: ArticleRecord =

let mutable private allRecords: ArticleRecord list = []

let findAll () = about :: allRecords
[<RequireQualifiedAccess>]
module ArticlesRepository =
let findAll () = about :: allRecords

type FindAllBySources = Source list -> ArticleRecord list

type FindAllBySources = Source list -> ArticleRecord list
let findAllBySources: FindAllBySources =
fun sources -> findAll () |> List.filter (fun r -> List.contains r.SourceType sources)

let findAllBySources: FindAllBySources =
fun sources -> findAll () |> List.filter (fun r -> List.contains r.SourceType sources)
type UpdateArticles = ArticleRecord list -> unit

let updateAll newRecords = allRecords <- newRecords
let updateAll: UpdateArticles = fun newRecords -> allRecords <- newRecords
28 changes: 28 additions & 0 deletions Damo.Io.Server/src/BackgroundProcessor.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module DamoIoServer.BackgroundProcessor

open FSharp.Control
open Microsoft.Extensions.Logging

open DamoIoServer.SourcesRepository
open DamoIoServer.ArticlesRepository

[<RequireQualifiedAccess>]
type BackgroundProcessor(logger: ILogger, updateArticles: ArticlesRepository.UpdateArticles) =

let updatesSequence () =
asyncSeq {
let tenMinutes = 10 * 1000 * 60

while true do
let! newArticles =
SourcesRepository.findAll ()
|> FeedsProcessor.processFeeds logger
|> AsyncSeq.toListAsync

yield newArticles

do! Async.Sleep tenMinutes
}

member this.StartProcessing() =
Async.Start(updatesSequence () |> AsyncSeq.iter updateArticles)
14 changes: 10 additions & 4 deletions Damo.Io.Server/src/Program.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Program

open DamoIoServer
open DamoIoServer.AssetHashBuilder
open Giraffe
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
Expand All @@ -11,6 +9,12 @@ open System
open System.IO
open WebOptimizer

open DamoIoServer.App
open DamoIoServer.AppConfig
open DamoIoServer.ArticlesRepository
open DamoIoServer.BackgroundProcessor
open DamoIoServer.AssetHashBuilder

let private configureErrorHandling (app: IApplicationBuilder) =
if AppConfig.enableExceptionPage then
app.UseDeveloperExceptionPage()
Expand Down Expand Up @@ -56,8 +60,10 @@ let main _ =
let webHost = webHostBuilder().Build()

let loggerProvider = webHost.Services.GetRequiredService<ILoggerProvider>()
let processorLogger = loggerProvider.CreateLogger("Damo.Io.Server.FeedsProcessor")
Async.Start(App.backgroundProcessing processorLogger)
let logger = loggerProvider.CreateLogger("Damo.Io.Server.FeedsProcessor")
let processor = BackgroundProcessor(logger, ArticlesRepository.updateAll)

processor.StartProcessing()

webHost.Run()
0

0 comments on commit 052d5c1

Please sign in to comment.