Merge pull request 'Publish Atom feeds for posts.' (#1) from feeds into main

Reviewed-on: #1
This commit is contained in:
Jessica Canady 2024-08-20 21:21:15 +00:00
commit dce932a33c
5 changed files with 45 additions and 5 deletions

View file

@ -0,0 +1,12 @@
defmodule JOLWeb.FeedController do
use JOLWeb, :controller
def feed(conn, _params) do
feed = JOL.Blog.all_posts()
|> JOLWeb.Feeds.build_feed()
conn
|> put_resp_content_type("text/xml")
|> send_resp(200, feed)
end
end

20
lib/jol_web/feeds.ex Normal file
View file

@ -0,0 +1,20 @@
defmodule JOLWeb.Feeds do
use Phoenix.VerifiedRoutes, endpoint: JOLWeb.Endpoint, router: JOLWeb.Router
alias Atomex.{Feed, Entry}
def build_feed(posts) do
Feed.new(url(~p"/"), DateTime.utc_now, "Jessica Online")
|> Feed.author("Jessica Phoenix Canady", email: "jess@canady.tech")
|> Feed.link(url(~p"/feed.xml"), rel: "self")
|> Feed.entries(Enum.map(posts, &build_entry/1))
|> Feed.build()
|> Atomex.generate_document()
end
defp build_entry(post) do
Entry.new(url(~p"/blog/#{post.slug}"), post.date, post.title)
|> Entry.author(post.author, uri: url(~p"/"))
|> Entry.content(post.body, type: "html")
|> Entry.build()
end
end

View file

@ -14,6 +14,10 @@ defmodule JOLWeb.Router do
plug :accepts, ["json"]
end
pipeline :feeds do
plug :accepts, ["xml"]
end
scope "/", JOLWeb do
pipe_through :browser
@ -27,10 +31,11 @@ defmodule JOLWeb.Router do
get "/blog/:slug", BlogController, :show
end
# Other scopes may use custom stacks.
# scope "/api", JOLWeb do
# pipe_through :api
# end
scope "/", JOLWeb do
pipe_through :feeds
get "/feed.xml", FeedController, :feed
end
# Enable LiveDashboard and Swoosh mailbox preview in development
if Application.compile_env(:jol, :dev_routes) do

View file

@ -59,7 +59,8 @@ defmodule JOL.MixProject do
{:dns_cluster, "~> 0.1.1"},
{:bandit, "~> 1.2"},
{:nimble_publisher, "~> 1.1.0"},
{:toml, "~> 0.7"}
{:toml, "~> 0.7"},
{:atomex, "~> 0.3.0"}
]
end

View file

@ -1,4 +1,5 @@
%{
"atomex": {:hex, :atomex, "0.3.0", "19b5d1a2aef8706dbd307385f7d5d9f6f273869226d317492c396c7bacf26402", [:mix], [{:xml_builder, "~> 2.0.0", [hex: :xml_builder, repo: "hexpm", optional: false]}], "hexpm", "025dbc3a3e99380894791a093019f535d0ef6cf1916f6ec1b778ac107fcfc3e4"},
"bandit": {:hex, :bandit, "1.5.2", "ed0a41c43a9e529c670d0fd48371db4027e7b80d43b1942893e17deb8bed0540", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "35ddbdce7e8a2a3c6b5093f7299d70832a43ed2f4a1852885a61d334cab1b4ad"},
"castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"},
"db_connection": {:hex, :db_connection, "2.6.0", "77d835c472b5b67fc4f29556dee74bf511bbafecdcaf98c27d27fa5918152086", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2f992d15725e721ec7fbc1189d4ecdb8afef76648c746a8e1cad35e3b8a35f3"},
@ -43,4 +44,5 @@
"toml": {:hex, :toml, "0.7.0", "fbcd773caa937d0c7a02c301a1feea25612720ac3fa1ccb8bfd9d30d822911de", [:mix], [], "hexpm", "0690246a2478c1defd100b0c9b89b4ea280a22be9a7b313a8a058a2408a2fa70"},
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
"websock_adapter": {:hex, :websock_adapter, "0.5.6", "0437fe56e093fd4ac422de33bf8fc89f7bc1416a3f2d732d8b2c8fd54792fe60", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "e04378d26b0af627817ae84c92083b7e97aca3121196679b73c73b99d0d133ea"},
"xml_builder": {:hex, :xml_builder, "2.0.0", "371ed27bb63bf0598dbaf3f0c466e5dc7d16cb4ecb68f06a67f953654062e21b", [:mix], [], "hexpm", "baeb5c8d42204bac2b856ffd50e8cda42d63b622984538d18d92733e4e790fbd"},
}