Compare commits

..

2 commits

Author SHA1 Message Date
Jessica Canady f9906fa4f7
Generate feed for posts. 2024-08-20 16:50:26 -04:00
Jessica Canady f3bcea48e6
Add route for feed.xml. 2024-08-20 16:50:07 -04:00
3 changed files with 41 additions and 4 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"] plug :accepts, ["json"]
end end
pipeline :feeds do
plug :accepts, ["xml"]
end
scope "/", JOLWeb do scope "/", JOLWeb do
pipe_through :browser pipe_through :browser
@ -27,10 +31,11 @@ defmodule JOLWeb.Router do
get "/blog/:slug", BlogController, :show get "/blog/:slug", BlogController, :show
end end
# Other scopes may use custom stacks. scope "/", JOLWeb do
# scope "/api", JOLWeb do pipe_through :feeds
# pipe_through :api
# end get "/feed.xml", FeedController, :feed
end
# Enable LiveDashboard and Swoosh mailbox preview in development # Enable LiveDashboard and Swoosh mailbox preview in development
if Application.compile_env(:jol, :dev_routes) do if Application.compile_env(:jol, :dev_routes) do