emptygif
语言: 简体中文

所有语言和框架

Phoenix Elixir

一个用于 Phoenix 的 Elixir 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Phoenix Elixir

安装
mix phx.new my_app
文件
pixel_controller.ex
运行
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

我们运行了此代码片段,并比对了它返回的字节。

验证

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

官方网站: Phoenix

更多 Elixir 示例