diff --git a/lib/towerops_web/endpoint.ex b/lib/towerops_web/endpoint.ex index 30255675..eda53092 100644 --- a/lib/towerops_web/endpoint.ex +++ b/lib/towerops_web/endpoint.ex @@ -117,5 +117,6 @@ defmodule ToweropsWeb.Endpoint do plug Phoenix.Ecto.SQL.Sandbox end + plug ToweropsWeb.Plugs.MarkdownNegotiation plug ToweropsWeb.Router end diff --git a/lib/towerops_web/plugs/markdown_negotiation.ex b/lib/towerops_web/plugs/markdown_negotiation.ex new file mode 100644 index 00000000..4ae2b76e --- /dev/null +++ b/lib/towerops_web/plugs/markdown_negotiation.ex @@ -0,0 +1,61 @@ +defmodule ToweropsWeb.Plugs.MarkdownNegotiation do + @moduledoc false + + import Plug.Conn + + @markdown_content %{ + "/" => """ + # TowerOps + + Network monitoring and alerting platform built for WISP and ISP operators. + + See the business impact of every network event. + + ## Features + + - Real-time network monitoring + - Subscriber impact analysis + - Revenue impact tracking + - Equipment management + - Multi-site support + + ## Get Started + + - [Register](/users/register) + - [Log In](/users/log_in) + - [API Documentation](/docs/api) + - [Privacy Policy](/privacy) + - [Terms of Service](/terms) + """, + "/privacy" => + "# Privacy Policy\n\nVisit [towerops.net/privacy](https://towerops.net/privacy) for the full privacy policy.\n", + "/terms" => + "# Terms of Service\n\nVisit [towerops.net/terms](https://towerops.net/terms) for the full terms of service.\n" + } + + def init(opts), do: opts + + def call(conn, _opts) do + if accepts_markdown?(conn) do + case Map.get(@markdown_content, conn.request_path) do + nil -> conn + markdown -> send_markdown(conn, markdown) + end + else + conn + end + end + + defp accepts_markdown?(conn) do + conn + |> get_req_header("accept") + |> Enum.any?(&String.contains?(&1, "text/markdown")) + end + + defp send_markdown(conn, content) do + conn + |> put_resp_content_type("text/markdown") + |> send_resp(200, content) + |> halt() + end +end diff --git a/test/towerops_web/controllers/page_controller_test.exs b/test/towerops_web/controllers/page_controller_test.exs index 76fe6d96..f3bd75b0 100644 --- a/test/towerops_web/controllers/page_controller_test.exs +++ b/test/towerops_web/controllers/page_controller_test.exs @@ -65,4 +65,18 @@ defmodule ToweropsWeb.PageControllerTest do assert response =~ "Terms of Service" end + + describe "GET / with Accept: text/markdown" do + test "returns markdown content when markdown is requested", %{conn: conn} do + conn = + conn + |> put_req_header("accept", "text/markdown") + |> get(~p"/") + + assert response(conn, 200) + assert get_resp_header(conn, "content-type") == ["text/markdown; charset=utf-8"] + body = response(conn, 200) + assert body =~ "# TowerOps" + end + end end