emptygif
言語: 日本語

すべての言語・フレームワーク

Plug + Bandit Elixir

/pixel.gif へのリクエストに空の GIF を返す、Plug + Bandit 用の Elixir スニペット。GIF は起動時に一度だけデコードします。

Plug + Bandit Elixir

ファイル
pixel.exs
実行
elixir pixel.exs
Mix.install([:plug, :bandit])

defmodule Pixel.Router do
  use Plug.Router

  @gif Base.decode64!("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

  plug :match
  plug :dispatch

  get "/pixel.gif" do
    conn
    |> put_resp_content_type("image/gif", nil)
    |> put_resp_header("cache-control", "no-store")
    |> send_resp(200, @gif)
  end

  match _ do
    send_resp(conn, 404, "Not Found")
  end
end

Bandit.start_link(plug: Pixel.Router, port: 8080)
Process.sleep(:infinity)

このスニペットは実行し、返されたバイト列を照合しました。

確認

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

公式サイト: Plug + Bandit

Elixir の他の例