emptygif
语言: 简体中文

所有语言和框架

Crystal standard library (HTTP::Server) Crystal

一个用于 Crystal standard library (HTTP::Server) 的 Crystal 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Crystal standard library (HTTP::Server) Crystal

文件
server.cr
运行
crystal run server.cr
require "base64"
require "http/server"

GIF = Base64.decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

server = HTTP::Server.new do |context|
  request, response = context.request, context.response
  if request.method == "GET" && request.path == "/pixel.gif"
    response.content_type = "image/gif"
    response.headers["Cache-Control"] = "no-store"
    response.content_length = GIF.size
    response.write(GIF)
  else
    response.respond_with_status(:not_found)
  end
end

server.bind_tcp "0.0.0.0", 8080
server.listen

已对照该框架当前的文档核对。

验证

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

官方网站: Crystal standard library (HTTP::Server)

更多 Crystal 示例