Plug + Bandit Elixir
一个用于 Plug + Bandit 的 Elixir 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。
Plug + Bandit Elixir
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