emptygif
語言: 繁體中文

所有語言和框架

Plug + Bandit Elixir

適用於 Plug + Bandit 的 Elixir 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,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 範例