Skip to content

Commit

Permalink
More consistent namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
dam5s committed Mar 4, 2024
1 parent 9ca48e6 commit a0950b2
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 102 deletions.
1 change: 1 addition & 0 deletions Damo.Io.Blog/src/BlogGenerator/Html.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module BlogGenerator.Html

open System

open BlogGenerator.Posts
open BlogGenerator.Config

Expand Down
7 changes: 4 additions & 3 deletions Damo.Io.Blog/src/BlogGenerator/Posts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

open System
open System.IO
open Metadata
open Markdown
open Config

open BlogGenerator.Metadata
open BlogGenerator.Markdown
open BlogGenerator.Config

type Post =
{ Title: string
Expand Down
1 change: 1 addition & 0 deletions Damo.Io.Blog/src/BlogGenerator/Rss.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

open System
open System.Xml.Linq

open BlogGenerator.Posts
open BlogGenerator.Config

Expand Down
2 changes: 1 addition & 1 deletion Damo.Io.Blog/src/Program.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open BlogGenerator.Build

[<EntryPoint>]
let main argv =
let main _ =
Build.run ()
0
3 changes: 2 additions & 1 deletion Damo.Io.Server/src/Article.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module DamoIoServer.Article

open FeedsProcessing.Article
open Time

open FeedsProcessing.Article
open DamoIoServer.Source

type MediaRecord = { Url: string; Description: string }
Expand Down
18 changes: 10 additions & 8 deletions Damo.Io.Server/src/ArticleListTemplate.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[<RequireQualifiedAccess>]
module DamoIoServer.ArticleListTemplate

open DamoIoServer.Source
open DamoIoServer.Article
open DamoIoServer.ArticleTemplate

let private sourceToggleHref selectedSources source =
Source.all
Expand Down Expand Up @@ -41,10 +41,12 @@ let private sourceLink selectedSources source =

li [] [ a attrs [ str (Source.toString source) ] ]

let render (articles: ArticleRecord list) (sources: Source list) : XmlNode =
let sourceLinks = Source.all |> List.map (sourceLink sources)
let articleList = articles |> List.map ArticleTemplate.render
let logo = h1 [] [ str "damo.io" ]
let menu = ul [ _class "main-menu" ] sourceLinks

div [ _id "template" ] [ aside [] [ logo; menu ]; main [] articleList ]
[<RequireQualifiedAccess>]
module ArticleListTemplate =
let render (articles: ArticleRecord list) (sources: Source list) : XmlNode =
let sourceLinks = Source.all |> List.map (sourceLink sources)
let articleList = articles |> List.map ArticleTemplate.render
let logo = h1 [] [ str "damo.io" ]
let menu = ul [ _class "main-menu" ] sourceLinks

div [ _id "template" ] [ aside [] [ logo; menu ]; main [] articleList ]
38 changes: 20 additions & 18 deletions Damo.Io.Server/src/ArticleTemplate.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[<RequireQualifiedAccess>]
module DamoIoServer.ArticleTemplate

open Time

open DamoIoServer.Article

let private monthDisplayName month =
Expand Down Expand Up @@ -58,24 +58,26 @@ let private renderMedia (media: MediaRecord) : XmlNode =
[ img [ _src media.Url; _alt media.Description ]
figcaption [] [ str media.Description ] ]

let render (article: ArticleRecord) : XmlNode =
let articleHeader = articleHeader article
let articleContent = section [] [ rawText article.Content ]
let maybeSourceLink = trySourceLink article
[<RequireQualifiedAccess>]
module ArticleTemplate =
let render (article: ArticleRecord) : XmlNode =
let articleHeader = articleHeader article
let articleContent = section [] [ rawText article.Content ]
let maybeSourceLink = trySourceLink article

let sourceNameClass = article.SourceName.Replace(" ", "")
let sourceTypeClass = article.SourceType.ToString()
let cssClasses = $"{sourceNameClass} {sourceTypeClass}"
let sourceNameClass = article.SourceName.Replace(" ", "")
let sourceTypeClass = article.SourceType.ToString()
let cssClasses = $"{sourceNameClass} {sourceTypeClass}"

HtmlElements.article
[ _class cssClasses ]
[ yield articleHeader
yield articleContent
HtmlElements.article
[ _class cssClasses ]
[ yield articleHeader
yield articleContent

match article.Media with
| Some media -> yield renderMedia media
| None -> ()
match article.Media with
| Some media -> yield renderMedia media
| None -> ()

match maybeSourceLink with
| Some sourceLink -> yield sourceLink
| None -> () ]
match maybeSourceLink with
| Some sourceLink -> yield sourceLink
| None -> () ]
5 changes: 4 additions & 1 deletion Damo.Io.Server/src/ArticlesHandler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ module DamoIoServer.ArticlesHandler

open System
open Time
open DamoIoServer.Article

open DamoIoServer.Source
open DamoIoServer.LayoutTemplate
open DamoIoServer.Article
open DamoIoServer.ArticlesRepository
open DamoIoServer.ArticleListTemplate

let private sourcesFromPath (path: string) =
path.Split(",") |> Array.toList |> List.choose Source.tryFromString
Expand Down
1 change: 0 additions & 1 deletion Damo.Io.Server/src/AssetHashBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module DamoIoServer.AssetHashBuilder

open System
open System.Security.Cryptography

open System.Collections.Concurrent
open Microsoft.AspNetCore.Http
open WebOptimizer
Expand Down
1 change: 1 addition & 0 deletions Damo.Io.Server/src/BackgroundProcessor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open Microsoft.Extensions.Logging

open DamoIoServer.SourcesRepository
open DamoIoServer.ArticlesRepository
open DamoIoServer.FeedsProcessor

[<RequireQualifiedAccess>]
type BackgroundProcessor(logger: ILogger, updateArticles: ArticlesRepository.UpdateArticles) =
Expand Down
29 changes: 16 additions & 13 deletions Damo.Io.Server/src/FeedsProcessor.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module DamoIoServer.FeedsProcessor

open DamoIoServer.Article
open DamoIoServer.SourcesRepository
open FSharp.Control
open Microsoft.Extensions.Logging

open FeedsProcessing.Article
open FeedsProcessing.DataGateway
open FeedsProcessing.Feeds
open FeedsProcessing.ProcessingResult
open FeedsProcessing.Xml
open Microsoft.Extensions.Logging
open DamoIoServer.Article
open DamoIoServer.SourcesRepository

let private articleToRecord (sourceFeed: SourceFeed) (article: Article) : ArticleRecord =
{ Title = Article.title article
Expand All @@ -26,11 +27,11 @@ let private downloadAndProcessFeed (logger: ILogger) (sourceFeed: SourceFeed) :
match sourceFeed.Feed with
| Xml(url) ->
async {
let! download = downloadContent url
let! download = DataGateway.download url

return
download
|> Result.bind processFeed
|> Result.bind Xml.processFeed
|> Result.onOk (fun articles ->
let count = List.length articles
logger.LogInformation($"Parsed feed %A{url}, found %d{count} article(s)")
Expand All @@ -43,12 +44,14 @@ let private downloadAndProcessFeed (logger: ILogger) (sourceFeed: SourceFeed) :
)
}

let processFeeds (logger: ILogger) (sources: SourcesRepository.SourceFeed list) : AsyncSeq<ArticleRecord> =
asyncSeq {
for sourceFeed in sources do
let! processingResult = downloadAndProcessFeed logger sourceFeed
let articles = processingResult |> resultToList sourceFeed
[<RequireQualifiedAccess>]
module FeedsProcessor =
let processFeeds (logger: ILogger) (sources: SourcesRepository.SourceFeed list) : AsyncSeq<ArticleRecord> =
asyncSeq {
for sourceFeed in sources do
let! processingResult = downloadAndProcessFeed logger sourceFeed
let articles = processingResult |> resultToList sourceFeed

for a in articles do
yield a
}
for a in articles do
yield a
}
32 changes: 17 additions & 15 deletions Damo.Io.Server/src/LayoutTemplate.fs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
[<RequireQualifiedAccess>]
module DamoIoServer.LayoutTemplate

open DamoIoServer.AssetHashBuilder
open Giraffe
open Giraffe.ViewEngine
open Microsoft.AspNetCore.Http

let render (ctx: HttpContext) innerTemplate =
let hashBuilder = ctx.GetService<AssetHashBuilder>()
let _assetHref = hashBuilder.Path ctx >> _href
open DamoIoServer.AssetHashBuilder

[<RequireQualifiedAccess>]
module LayoutTemplate =
let render (ctx: HttpContext) innerTemplate =
let hashBuilder = ctx.GetService<AssetHashBuilder>()
let _assetHref = hashBuilder.Path ctx >> _href

html
[ _lang "en" ]
[ head
[]
[ meta [ _charset "utf-8" ]
meta [ _name "viewport"; _content "width=device-width" ]
title [] [ str "damo.io - Damien Le Berrigaud's feeds" ]
link [ _rel "stylesheet"; _type "text/css"; _assetHref "/styles/app.min.css" ]
link [ _rel "icon"; _type "image/svg+xml"; _sizes "any"; _href "/favicon.svg" ] ]
body [] [ innerTemplate; script [ _src "/javascript/htmx-1.8.5.min.js" ] [] ] ]
html
[ _lang "en" ]
[ head
[]
[ meta [ _charset "utf-8" ]
meta [ _name "viewport"; _content "width=device-width" ]
title [] [ str "damo.io - Damien Le Berrigaud's feeds" ]
link [ _rel "stylesheet"; _type "text/css"; _assetHref "/styles/app.min.css" ]
link [ _rel "icon"; _type "image/svg+xml"; _sizes "any"; _href "/favicon.svg" ] ]
body [] [ innerTemplate; script [ _src "/javascript/htmx-1.8.5.min.js" ] [] ] ]
2 changes: 1 addition & 1 deletion Damo.Io.Server/src/SourcesRepository.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module DamoIoServer.SourcesRepository

open DamoIoServer.Source
open FeedsProcessing.Download
open FeedsProcessing.Feeds
open DamoIoServer.Source

type SourceFeed =
{ Type: Source
Expand Down
19 changes: 10 additions & 9 deletions FeedsProcessing.Tests/src/XmlTests.fs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module ``Xml Processor Tests``

open FeedsProcessing.Article
open FeedsProcessing.Xml
open FeedsProcessingTests.DownloadSupport
open FsUnit
open FsUnitTyped
open NUnit.Framework
open System
open Time

open FeedsProcessing.Article
open FeedsProcessing.Xml
open FeedsProcessingTests.DownloadSupport

[<Test>]
let ``with unsupported XML`` () =
let download = Download.fromContent "<foo>Not quite expected xml content</foo>"

let result = processFeed download
let result = Xml.processFeed download

match result with
| Ok _ -> Assert.Fail "Expected failure"
Expand All @@ -28,7 +29,7 @@ let ``with github Atom XML`` () =
"../../../../FeedsProcessing.Tests/resources/test-samples/github.xml"
|> Download.fromFilePath

let result = processFeed download
let result = Xml.processFeed download

match result with
| Error _ -> Assert.Fail "Expected success"
Expand Down Expand Up @@ -58,7 +59,7 @@ let ``with Dualshock Atom XML`` () =
"../../../../FeedsProcessing.Tests/resources/test-samples/dualshock.xml"
|> Download.fromFilePath

let result = processFeed downloaded
let result = Xml.processFeed downloaded

match result with
| Error _ -> Assert.Fail "Expected success"
Expand All @@ -71,7 +72,7 @@ let ``with RSS XML`` () =
"../../../../FeedsProcessing/resources/samples/rss.sample.xml"
|> Download.fromFilePath

let result = processFeed downloadedFeed
let result = Xml.processFeed downloadedFeed

match result with
| Error _ -> Assert.Fail "Expected success"
Expand Down Expand Up @@ -112,7 +113,7 @@ let ``with mastodon RSS`` () =
"../../../../FeedsProcessing.Tests/resources/test-samples/mastodon.xml"
|> Download.fromFilePath

let result = processFeed downloadedFeed
let result = Xml.processFeed downloadedFeed

match result with
| Error _ -> Assert.Fail "Expected success"
Expand Down Expand Up @@ -141,7 +142,7 @@ let ``processFeed with slashdot RDF XML`` () =
"../../../../FeedsProcessing.Tests/resources/test-samples/slashdot.xml"
|> Download.fromFilePath

let result = processFeed downloadedFeed
let result = Xml.processFeed downloadedFeed

match result with
| Error _ -> Assert.Fail "Expected success"
Expand Down
3 changes: 2 additions & 1 deletion FeedsProcessing/src/Article.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module FeedsProcessing.Article

open FeedsProcessing
open System
open Time

open FeedsProcessing.Html

let private stringToOption text =
if String.IsNullOrWhiteSpace text then None else Some text

Expand Down
35 changes: 19 additions & 16 deletions FeedsProcessing/src/DataGateway.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module FeedsProcessing.DataGateway

open FSharp.Data
open FSharp.Data.HttpRequestHeaders
open FeedsProcessing.Download
open System
open System.Web

open FeedsProcessing.Download

type private BasicAuthHeader = BasicAuthHeader of string
type private BearerToken = BearerToken of string

Expand Down Expand Up @@ -56,19 +57,21 @@ let private requestToken (BasicAuthHeader authHeader) =
parseToken responseString
)

let downloadContent (Url url) : DownloadResult =
async {
return
Try.value
"Download content"
(fun _ ->
let content =
Http.RequestString(
url,
headers = [ "User-Agent", "somanyfeeds.com" ],
responseEncodingOverride = "utf-8"
)
[<RequireQualifiedAccess>]
module DataGateway =
let download (Url url) : DownloadResult =
async {
return
Try.value
"Download content"
(fun _ ->
let content =
Http.RequestString(
url,
headers = [ "User-Agent", "somanyfeeds.com" ],
responseEncodingOverride = "utf-8"
)

{ Url = (Url url); Content = content }
)
}
{ Url = (Url url); Content = content }
)
}
Loading

0 comments on commit a0950b2

Please sign in to comment.