Crystal standard library (HTTP::Server) Crystal
/pixel.gif కు వచ్చే రిక్వెస్ట్లకు empty GIF తో జవాబిచ్చే, Crystal standard library (HTTP::Server) కోసం Crystal స్నిపెట్. GIF స్టార్టప్లో ఒక్కసారే డీకోడ్ అవుతుంది.
Crystal standard library (HTTP::Server) Crystal
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)