Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/plexus/apps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ defmodule Plexus.Apps do
|> Repo.paginate(page_opts)
end

@doc """
Fetches the most recently added app
"""
@spec fetch_most_recently_added_app! :: App.t()
def fetch_most_recently_added_app! do
App
|> order_by(:inserted_at)
|> limit(1)
|> Repo.one!()
end

@doc """
Fetches an App.

Expand Down
2 changes: 1 addition & 1 deletion lib/plexus_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule PlexusWeb do
def controller do
quote do
use Phoenix.Controller,
formats: [:html, :json],
formats: [:html, :json, :xml],
layouts: [html: PlexusWeb.Layouts]

import Plug.Conn
Expand Down
67 changes: 67 additions & 0 deletions lib/plexus_web/controllers/sitemap_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule PlexusWeb.SitemapController do
use PlexusWeb, :controller

def index(conn, _params) do
conn
|> put_resp_content_type("text/xml")
|> put_layout(false)
|> render("sitemap.xml", routes: routes())
end

defp routes do
base_url = PlexusWeb.Endpoint.url()

PlexusWeb.Router
|> Phoenix.Router.routes()
|> Enum.reduce([], fn
%{verb: :get, path: path}, acc ->
if String.starts_with?(path, "/admin") or
String.starts_with?(path, "/api") or
String.contains?(path, ":") or
String.ends_with?(path, ".xml") do
acc
else
[build_route(base_url, path) | acc]
end

_route, acc ->
acc
end)
end

defp build_route(base_url, path) do
%{
path: full_path(base_url, path),
priority: priority(path),
change_freq: change_freq(path),
last_mod: last_mod(path)
}
end

defp full_path(base_url, path) do
path =
case path do
"/" -> "/"
path -> path <> "/"
end

base_url
|> URI.new!()
|> URI.append_path(path)
|> URI.to_string()
end

defp priority("/"), do: 1.0
defp priority(_path), do: 0.5

defp change_freq("/swaggerui"), do: "yearly"
defp change_freq(_), do: "weekly"

defp last_mod("/") do
Plexus.Apps.fetch_most_recently_added_app!()
|> Map.fetch!(:inserted_at)
|> DateTime.to_date()
end

defp last_mod(_), do: false
end
4 changes: 4 additions & 0 deletions lib/plexus_web/controllers/sitemap_xml.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule PlexusWeb.SitemapXML do
use PlexusWeb, :html
embed_templates "sitemap_xml/*"
end
11 changes: 11 additions & 0 deletions lib/plexus_web/controllers/sitemap_xml/sitemap.xml.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<%= for route <- @routes do %>
<url>
<loc><%= route.path %></loc>
<%= if route.last_mod do %><lastmod><%= route.last_mod %></lastmod><% end %>
<changefreq><%= route.change_freq %></changefreq>
<priority><%= route.priority %></priority>
</url>
<% end %>
</urlset>
2 changes: 2 additions & 0 deletions lib/plexus_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ defmodule PlexusWeb.Router do
plug :auth
end

get "/sitemap.xml", PlexusWeb.SitemapController, :index

scope "/" do
pipe_through :browser

Expand Down