emptygif
語言: 繁體中文

所有語言和框架

Mist Gleam

適用於 Mist 的 Gleam 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。

Mist Gleam

安裝
gleam add mist gleam_http gleam_erlang
檔案
pixel.gleam
執行
gleam run
import gleam/bit_array
import gleam/bytes_tree
import gleam/erlang/process
import gleam/http
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import mist.{type Connection, type ResponseData}

pub fn main() {
  let assert Ok(gif) = bit_array.base64_decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

  let assert Ok(_) =
    fn(req: Request(Connection)) -> Response(ResponseData) {
      case req.method, req.path {
        http.Get, "/pixel.gif" ->
          response.new(200)
          |> response.set_header("content-type", "image/gif")
          |> response.set_header("cache-control", "no-store")
          |> response.set_body(mist.Bytes(bytes_tree.from_bit_array(gif)))
        _, _ ->
          response.new(404)
          |> response.set_body(mist.Bytes(bytes_tree.new()))
      }
    }
    |> mist.new
    |> mist.bind("0.0.0.0")
    |> mist.port(8080)
    |> mist.start

  process.sleep_forever()
}

我們執行了這段程式碼,並比對了它回傳的位元組。

驗證

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

官方網站: Mist

更多 Gleam 範例