Browse Source

use an algorithm to generate image paths in config

test/local-setup
Silberengel 3 weeks ago
parent
commit
f67abf2068
  1. 4
      config/config.exs
  2. 42
      lib/gc_index_relay_web/plugs/relay_info.ex

4
config/config.exs

@ -54,8 +54,8 @@ config :phoenix, :json_library, Jason @@ -54,8 +54,8 @@ config :phoenix, :json_library, Jason
# Edit these values to describe your relay instance.
config :gc_index_relay, :relay_info,
name: "Mercury Index-Relay",
icon: "http://localhost:4000/favicon.ico",
banner: "http://localhost:4000/images/mercury_icon.png",
icon: "favicon.ico",
banner: "images/mercury_icon.png",
description:
"A Nostr index relay for the http protocol, from GitCitadel. Featuring a RESTful API and Swagger, it specializes in swift retrieval or publications, repos, and similar graphs of related events",
software: "https://git.imwald.eu/silberengel/gc_index_relay.git",

42
lib/gc_index_relay_web/plugs/relay_info.ex

@ -18,7 +18,12 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do @@ -18,7 +18,12 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do
accept = conn |> get_req_header("accept") |> Enum.join(",")
if String.contains?(accept, "application/nostr+json") do
relay_info = Application.get_env(:gc_index_relay, :relay_info, []) |> Map.new()
base_url = build_base_url(conn)
relay_info =
Application.get_env(:gc_index_relay, :relay_info, [])
|> Map.new()
|> resolve_image_urls(base_url)
conn
|> put_resp_content_type("application/nostr+json")
@ -30,4 +35,39 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do @@ -30,4 +35,39 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do
end
def call(conn, _opts), do: conn
# Build "scheme://host[:port]" from the incoming request.
# Standard ports (80 for http, 443 for https) are omitted.
defp build_base_url(conn) do
port_suffix =
case {conn.scheme, conn.port} do
{:http, 80} -> ""
{:https, 443} -> ""
{_, port} -> ":#{port}"
end
"#{conn.scheme}://#{conn.host}#{port_suffix}"
end
# Prepend base_url to any relative (non-absolute) value for :icon and :banner.
defp resolve_image_urls(relay_info, base_url) do
relay_info
|> resolve_field(:icon, base_url)
|> resolve_field(:banner, base_url)
end
defp resolve_field(map, key, base_url) do
case Map.get(map, key) do
nil -> map
"" -> map
url when is_binary(url) ->
if String.starts_with?(url, ["http://", "https://"]) do
map
else
path = if String.starts_with?(url, "/"), do: url, else: "/#{url}"
Map.put(map, key, "#{base_url}#{path}")
end
_ -> map
end
end
end

Loading…
Cancel
Save