emptygif
Ngôn ngữ: Tiếng Việt

Tất cả ngôn ngữ và framework

Crystal standard library (HTTP::Server) Crystal

Đoạn mã Crystal cho Crystal standard library (HTTP::Server), trả lời các yêu cầu tới /pixel.gif bằng GIF rỗng, được giải mã một lần khi khởi động.

Crystal standard library (HTTP::Server) Crystal

Tệp
server.cr
Chạy
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

Đã đối chiếu với tài liệu hiện hành của framework.

Kiểm tra

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

Trang chính thức: Crystal standard library (HTTP::Server)

Thêm với Crystal