|
| 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 |
0 commit comments