feat: add sitemap.xml, api-catalog, and agent-discovery Link headers

This commit is contained in:
Graham McIntire 2026-04-17 13:31:20 -05:00
parent b1afc36ec4
commit 4854157ee9
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
4 changed files with 77 additions and 0 deletions

View file

@ -60,6 +60,57 @@ defmodule AprsmeWeb.PageController do
end
end
@sitemap_paths ~w(/ /about /api /status /packets)
def sitemap(conn, _params) do
base_url = AprsmeWeb.Endpoint.url()
urls =
Enum.map(@sitemap_paths, fn path ->
" <url><loc>#{base_url}#{path}</loc></url>"
end)
xml = """
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
#{Enum.join(urls, "\n")}
</urlset>
"""
conn
|> put_resp_content_type("application/xml")
|> send_resp(200, xml)
end
def api_catalog(conn, _params) do
base_url = AprsmeWeb.Endpoint.url()
ws_url = String.replace(base_url, ~r/^http/, "ws")
body = %{
linkset: [
%{
anchor: "#{base_url}/api/v1",
"service-doc": [%{href: "#{base_url}/api", type: "text/html"}],
status: [%{href: "#{base_url}/status.json", type: "application/json"}]
},
%{
anchor: "#{ws_url}/mobile/websocket",
"service-doc": [
%{
href: "https://github.com/gmcintire/aprs.me/blob/main/docs/mobile-api.md",
type: "text/markdown"
}
],
status: [%{href: "#{base_url}/status.json", type: "application/json"}]
}
]
}
conn
|> put_resp_content_type("application/linkset+json")
|> send_resp(200, Jason.encode!(body))
end
def ready(conn, _params) do
# Simple readiness check without database dependency
# This is faster for startup health checks

View file

@ -0,0 +1,17 @@
defmodule AprsmeWeb.Plugs.AgentDiscoveryLinks do
@moduledoc false
@behaviour Plug
import Plug.Conn
@link_header ~s(</.well-known/api-catalog>; rel="api-catalog", </api>; rel="service-doc")
@impl true
def init(opts), do: opts
@impl true
def call(conn, _opts) do
put_resp_header(conn, "link", @link_header)
end
end

View file

@ -29,6 +29,7 @@ defmodule AprsmeWeb.Router do
plug AprsmeWeb.Plugs.SetLocale
plug IPGeolocation
plug RateLimiter, scale: 60_000, limit: 200
plug AprsmeWeb.Plugs.AgentDiscoveryLinks
end
pipeline :public_api do
@ -71,6 +72,12 @@ defmodule AprsmeWeb.Router do
get "/status.json", PageController, :status_json
end
# Agent/crawler discovery endpoints
scope "/", AprsmeWeb do
get "/sitemap.xml", PageController, :sitemap
get "/.well-known/api-catalog", PageController, :api_catalog
end
## Authentication routes
scope "/", AprsmeWeb do

View file

@ -3,3 +3,5 @@
# To ban all spiders from the entire site uncomment the next two lines:
# User-agent: *
# Disallow: /
Sitemap: https://aprs.me/sitemap.xml