Skip to content

Commit 4268d3b

Browse files
committed
Add support for sitemap.xml
1 parent 105432c commit 4268d3b

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

lib/plexus/apps.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ defmodule Plexus.Apps do
3232
|> Repo.paginate(page_opts)
3333
end
3434

35+
@doc """
36+
Fetches the most recently added app
37+
"""
38+
@spec fetch_most_recently_added_app! :: App.t()
39+
def fetch_most_recently_added_app! do
40+
App
41+
|> order_by(:inserted_at)
42+
|> limit(1)
43+
|> Repo.one!()
44+
end
45+
3546
@doc """
3647
Fetches an App.
3748

lib/plexus_web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule PlexusWeb do
4141
def controller do
4242
quote do
4343
use Phoenix.Controller,
44-
formats: [:html, :json],
44+
formats: [:html, :json, :xml],
4545
layouts: [html: PlexusWeb.Layouts]
4646

4747
import Plug.Conn
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
defmodule PlexusWeb.SitemapController do
2+
use PlexusWeb, :controller
3+
4+
def index(conn, _params) do
5+
conn
6+
|> put_resp_content_type("text/xml")
7+
|> put_layout(false)
8+
|> render("sitemap.xml", routes: routes())
9+
end
10+
11+
defp routes do
12+
base_url = PlexusWeb.Endpoint.url()
13+
14+
PlexusWeb.Router
15+
|> Phoenix.Router.routes()
16+
|> Enum.reduce([], fn
17+
%{verb: :get, path: path}, acc ->
18+
if String.starts_with?(path, "/admin") or
19+
String.starts_with?(path, "/api") or
20+
String.contains?(path, ":") or
21+
String.ends_with?(path, ".xml") do
22+
acc
23+
else
24+
[build_route(base_url, path) | acc]
25+
end
26+
27+
_route, acc ->
28+
acc
29+
end)
30+
end
31+
32+
defp build_route(base_url, path) do
33+
%{
34+
path: full_path(base_url, path),
35+
priority: priority(path),
36+
change_freq: change_freq(path),
37+
last_mod: last_mod(path)
38+
}
39+
end
40+
41+
defp full_path(base_url, path) do
42+
path =
43+
case path do
44+
"/" -> "/"
45+
path -> path <> "/"
46+
end
47+
48+
base_url
49+
|> URI.new!()
50+
|> URI.append_path(path)
51+
|> URI.to_string()
52+
end
53+
54+
defp priority("/"), do: 1.0
55+
defp priority(_path), do: 0.5
56+
57+
defp change_freq("/swaggerui"), do: "yearly"
58+
defp change_freq(_), do: "weekly"
59+
60+
defp last_mod("/") do
61+
Plexus.Apps.fetch_most_recently_added_app!()
62+
|> Map.fetch!(:inserted_at)
63+
|> DateTime.to_date()
64+
end
65+
66+
defp last_mod(_), do: false
67+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule PlexusWeb.SitemapXML do
2+
use PlexusWeb, :html
3+
embed_templates "sitemap_xml/*"
4+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3+
<%= for route <- @routes do %>
4+
<url>
5+
<loc><%= route.path %></loc>
6+
<%= if route.last_mod do %><lastmod><%= route.last_mod %></lastmod><% end %>
7+
<changefreq><%= route.change_freq %></changefreq>
8+
<priority><%= route.priority %></priority>
9+
</url>
10+
<% end %>
11+
</urlset>

lib/plexus_web/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ defmodule PlexusWeb.Router do
2323
plug :auth
2424
end
2525

26+
get "/sitemap.xml", PlexusWeb.SitemapController, :index
27+
2628
scope "/" do
2729
pipe_through :browser
2830

0 commit comments

Comments
 (0)