emptygif
Language: English

All languages and frameworks

Phoenix Elixir

A Elixir snippet for Phoenix that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

Phoenix Elixir

Install
mix phx.new my_app
File
pixel_controller.ex
Run
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

We ran this snippet and compared the bytes it returned.

Check it

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

Official site: Phoenix

More in Elixir