Skip to content

Commit

Permalink
#43: add RSS generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Apr 17, 2017
1 parent 5a87c24 commit 874f296
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 72 deletions.
6 changes: 5 additions & 1 deletion ForneverMind/ConfigurationModule.fs
Expand Up @@ -3,11 +3,15 @@
open System.IO

open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Configuration

type ConfigurationModule (app : IHostingEnvironment) =
type ConfigurationModule(app : IHostingEnvironment, config : IConfigurationRoot) =
let root = app.ContentRootPath
let postsPath = Path.Combine(root, "posts")
let viewsPath = Path.Combine(root, "views")

let baseUrl = config.["baseUrl"]

member __.PostsPath = postsPath
member __.ViewsPath = viewsPath
member __.BaseUrl = baseUrl
7 changes: 5 additions & 2 deletions ForneverMind/ForneverMind.fsproj
Expand Up @@ -16,7 +16,7 @@
<Compile Include="TemplatingModule.fs" />
<Compile Include="PostsModule.fs" />
<Compile Include="PagesModule.fs" />
<Compile Include="Rss.fs" />
<Compile Include="RssModule.fs" />
<Compile Include="RoutesModule.fs" />
<Compile Include="KestrelInterop.fs" />
<Compile Include="Program.fs" />
Expand All @@ -29,7 +29,10 @@
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.*" />
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="1.*" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="RazorLight" Version="1.0.1" />
<PackageReference Include="WilderMinds.RssSyndication" Version="1.0.4" />
</ItemGroup>
</Project>
</Project>
17 changes: 13 additions & 4 deletions ForneverMind/Program.fs
@@ -1,21 +1,30 @@
module ForneverMind.Program

open System.IO

open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Logging

open ForneverMind.KestrelInterop

let private linkApplication env =
let configuration = ConfigurationModule(env)
let private fuseApplication cfg env =
let configuration = ConfigurationModule(env, cfg)
let posts = PostsModule(configuration)
let rss = RssModule(configuration, posts)
let templates = TemplatingModule(configuration)
let pages = PagesModule(posts, templates)
RoutesModule(pages)
RoutesModule(pages, rss)

let private createRouter (builder : IApplicationBuilder) =
let env = downcast builder.ApplicationServices.GetService typeof<IHostingEnvironment>
let routesModule = linkApplication env
let cfg =
ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build()
let routesModule = fuseApplication cfg env
routesModule.Router

let private useStaticFiles (app : IApplicationBuilder) =
Expand Down
4 changes: 2 additions & 2 deletions ForneverMind/RoutesModule.fs
Expand Up @@ -2,15 +2,15 @@

open Freya.Routers.Uri.Template

type RoutesModule (pages : PagesModule) =
type RoutesModule(pages : PagesModule, rss : RssModule) =
let router =
freyaRouter {
resource "/posts/{name}" pages.Post
resource "/" pages.Index
resource "/archive.html" pages.Archive
resource "/contact.html" pages.Contact
resource "/error.html" pages.Error
resource "/rss.xml" Rss.feed
resource "/rss.xml" rss.Feed
resource "/talks.html" pages.Talks
resource "/{q*}" pages.NotFound
}
Expand Down
63 changes: 0 additions & 63 deletions ForneverMind/Rss.fs

This file was deleted.

61 changes: 61 additions & 0 deletions ForneverMind/RssModule.fs
@@ -0,0 +1,61 @@
namespace ForneverMind

open System
open System.Text

open WilderMinds.RssSyndication
open Freya.Core
open Freya.Machines.Http
open Freya.Types.Http

open ForneverMind.Models

type RssModule(config : ConfigurationModule, posts : PostsModule) =

let sindicationItem (post : PostMetadata) : Item =
let url = config.BaseUrl + post.Url
Item(Title = post.Title,
Body = post.Description,
Link = Uri url,
Permalink = url, // TODO[F]: isPermaLink="true"
PublishDate = post.Date) // TODO[F]: Check date format used here

let feedContent =
let items = Seq.map sindicationItem posts.AllPosts
let feed =
Feed (
Title = "Engineer, programmer, gentleman",
Description = "Friedrich von Never: Engineer, Programmer, Gentleman.",
Link = Uri config.BaseUrl
)
Seq.iter feed.Items.Add items
let text = feed.Serialize()
Encoding.UTF8.GetBytes text

let lastModificationDate =
defaultArg (posts.AllPosts
|> Seq.tryHead
|> Option.map (fun p -> p.Date)) DateTime.UtcNow

let handleFeed _ =
freya {
return
{
Description =
{
Charset = Some Charset.Utf8
Encodings = None
MediaType = Some Common.rss
Languages = None
}
Data = feedContent
}
}

member __.Feed =
freyaMachine {
including Common.machine
methodsSupported Common.methods
lastModified (Common.initLastModified lastModificationDate)
handleOk handleFeed
}
3 changes: 3 additions & 0 deletions ForneverMind/appsettings.json
@@ -0,0 +1,3 @@
{
"baseUrl": "http://localhost:5000"
}

0 comments on commit 874f296

Please sign in to comment.