From 7309e8e37424f48f74ad7ea003ceea160069fedf Mon Sep 17 00:00:00 2001 From: Jessica Phoenix Canady Date: Tue, 20 Aug 2024 15:36:57 -0400 Subject: [PATCH 1/3] Add `atomex` dependency. --- mix.exs | 3 ++- mix.lock | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 49be7fb..0ddfce8 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/mix.lock b/mix.lock index dcb6fe4..2a82717 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, } From f3bcea48e647e9e6f72bcac781ab3231f3bdb8a0 Mon Sep 17 00:00:00 2001 From: Jessica Phoenix Canady Date: Tue, 20 Aug 2024 16:50:07 -0400 Subject: [PATCH 2/3] Add route for feed.xml. --- lib/jol_web/router.ex | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/jol_web/router.ex b/lib/jol_web/router.ex index d656163..2e644a3 100644 --- a/lib/jol_web/router.ex +++ b/lib/jol_web/router.ex @@ -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 From f9906fa4f7cd2c309882c6fd0ed1008f9fc95696 Mon Sep 17 00:00:00 2001 From: Jessica Phoenix Canady Date: Tue, 20 Aug 2024 16:50:26 -0400 Subject: [PATCH 3/3] Generate feed for posts. --- lib/jol_web/controllers/feed_controller.ex | 12 ++++++++++++ lib/jol_web/feeds.ex | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 lib/jol_web/controllers/feed_controller.ex create mode 100644 lib/jol_web/feeds.ex diff --git a/lib/jol_web/controllers/feed_controller.ex b/lib/jol_web/controllers/feed_controller.ex new file mode 100644 index 0000000..b8db889 --- /dev/null +++ b/lib/jol_web/controllers/feed_controller.ex @@ -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 diff --git a/lib/jol_web/feeds.ex b/lib/jol_web/feeds.ex new file mode 100644 index 0000000..ff4cb95 --- /dev/null +++ b/lib/jol_web/feeds.ex @@ -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