emptygif
Sprache: Deutsch

Alle Sprachen und Frameworks

Phoenix Elixir

Ein Elixir-Snippet für Phoenix, das Anfragen an /pixel.gif mit dem leeren GIF beantwortet, das einmal beim Start dekodiert wird.

Phoenix Elixir

Installieren
mix phx.new my_app
Datei
pixel_controller.ex
Ausführen
mix phx.server
# lib/my_app_web/controllers/pixel_controller.ex
defmodule MyAppWeb.PixelController do
  use MyAppWeb, :controller

  # Decoded once, at compile time.
  @gif Base.decode64!("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

  def show(conn, _params) do
    conn
    |> put_resp_content_type("image/gif", nil)
    |> put_resp_header("cache-control", "no-store")
    |> send_resp(200, @gif)
  end
end

# lib/my_app_web/router.ex - outside the :browser pipeline (no session cookie, no CSRF)
scope "/", MyAppWeb do
  get "/pixel.gif", PixelController, :show
end

Wir haben dieses Snippet ausgeführt und die zurückgegebenen Bytes verglichen.

Prüfen

curl -sI http://localhost:8080/pixel.gif

Offizielle Website: Phoenix

Mehr in Elixir