emptygif
Bahasa: Bahasa Indonesia

Semua bahasa dan framework

Phoenix Elixir

Snippet Elixir untuk Phoenix yang menjawab request ke /pixel.gif dengan empty GIF, yang di-decode sekali saat startup.

Phoenix Elixir

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

Kami telah menjalankan snippet ini dan membandingkan byte yang dikembalikannya.

Periksa

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

Situs resmi: Phoenix

Lainnya dalam Elixir