added rss feed for blog content

This commit is contained in:
Adam Piontek 2021-04-06 18:13:57 -04:00
parent a4249392f3
commit 7f783b916d
4 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,15 @@
defmodule Home73kWeb.FeedController do
use Home73kWeb, :controller
alias Home73k.Blog
def rss(conn, _params) do
posts = Blog.list_posts()
last_build_date = posts |> List.first() |> Map.get(:date)
conn
|> put_resp_content_type("application/rss+xml")
|> put_layout(:false)
|> render("rss.xml", posts: posts, last_build_date: last_build_date)
end
end

View file

@ -10,6 +10,10 @@ defmodule Home73kWeb.Router do
plug :put_secure_browser_headers
end
pipeline :xml_rss do
plug :accepts, ["xml", "rss", "atom"]
end
pipeline :api do
plug :accepts, ["json"]
end
@ -30,6 +34,13 @@ defmodule Home73kWeb.Router do
live "/blog/:id", BlogLive, :show
end
scope "/feed", Home73kWeb do
pipe_through :xml_rss
# Feeds
get "/", FeedController, :rss
end
# Other scopes may use custom stacks.
# scope "/api", Home73kWeb do
# pipe_through :api

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>73k Blog - Adam Piontek</title>
<link><%= Routes.blog_url(@conn, :index) %></link>
<atom:link href="<%= Routes.feed_url(@conn, :rss) %>" rel="self" type="application/rss+xml" />
<description>Blog by Adam Piontek. Desktop Systems Engineer. Human. Casual anthropologist, cautious DIYer, cuckoo leftist, cat papa.</description>
<language>en</language>
<copyright>Copyright <%= DateTime.utc_now.year %> Adam Piontek</copyright>
<lastBuildDate><%= @last_build_date |> to_rfc822 %></lastBuildDate>
<category>Personal/Political/Society/Technology/IT/Internet/Web development</category>
<ttl>60</ttl><%= for post <- @posts do %>
<item>
<title><%= post.title %></title>
<link><%= Routes.blog_url(@conn, :show, post) %></link>
<guid><%= Routes.blog_url(@conn, :show, post) %></guid>
<description><![CDATA[ <%= post.lede %> ]]></description>
<category><%= post.tags |> List.first() %></category>
<pubDate><%= post.date |> to_rfc822 %></pubDate>
<source url="<%= Routes.feed_url(@conn, :rss) %>">73k Blog - Adam Piontek</source>
</item><% end %>
</channel>
</rss>

View file

@ -0,0 +1,11 @@
defmodule Home73kWeb.FeedView do
use Home73kWeb, :view
import Home73k, only: [app_time_zone: 0]
def to_rfc822(naive_dt) do
naive_dt
|> DateTime.from_naive!(app_time_zone())
|> Calendar.strftime("%a, %d %b %Y %H:%M:%S %Z")
end
end