emptygif
언어: 한국어

모든 언어 및 프레임워크

Crystal standard library (HTTP::Server) Crystal

/pixel.gif 요청에 빈 GIF로 응답하는 Crystal standard library (HTTP::Server)용 Crystal 스니펫입니다. 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의 다른 예제