Browse Source
prevent cors errors in clients contacting the relay endpoing handles preflight OPTIONS requeststest/local-setup
2 changed files with 30 additions and 0 deletions
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
defmodule GcIndexRelayWeb.Plugs.CORS do |
||||
@moduledoc """ |
||||
CORS plug for the Nostr relay REST API. |
||||
|
||||
Nostr clients can be hosted on any origin, so this plug allows all origins |
||||
on all API routes. Preflight OPTIONS requests are halted here and returned |
||||
a 200 before they reach the router. |
||||
""" |
||||
|
||||
import Plug.Conn |
||||
|
||||
def init(opts), do: opts |
||||
|
||||
def call(conn, _opts) do |
||||
conn |
||||
|> put_resp_header("access-control-allow-origin", "*") |
||||
|> put_resp_header("access-control-allow-methods", "GET, POST, DELETE, OPTIONS") |
||||
|> put_resp_header("access-control-allow-headers", "content-type, authorization") |
||||
|> handle_preflight() |
||||
end |
||||
|
||||
defp handle_preflight(%Plug.Conn{method: "OPTIONS"} = conn) do |
||||
conn |
||||
|> send_resp(200, "") |
||||
|> halt() |
||||
end |
||||
|
||||
defp handle_preflight(conn), do: conn |
||||
end |
||||
Loading…
Reference in new issue