emptygif
언어: 한국어

모든 언어 및 프레임워크

Nim standard library (asynchttpserver) Nim

/pixel.gif 요청에 빈 GIF로 응답하는 Nim standard library (asynchttpserver)용 Nim 스니펫입니다. GIF는 시작할 때 한 번만 디코딩합니다.

Nim standard library (asynchttpserver) Nim

파일
server.nim
실행
nim c -r -d:release server.nim
import std/[asyncdispatch, asynchttpserver, base64]

# Decoded at compile time; a const is also safe to use from the gcsafe callback.
const gif = decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

proc handler(req: Request) {.async.} =
  if req.reqMethod == HttpGet and req.url.path == "/pixel.gif":
    let headers = newHttpHeaders({"Content-Type": "image/gif", "Cache-Control": "no-store"})
    await req.respond(Http200, gif, headers)
  else:
    await req.respond(Http404, "Not Found")

let server = newAsyncHttpServer()
waitFor server.serve(Port(8080), handler)

프레임워크의 최신 문서와 대조해 확인했습니다.

확인

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

공식 사이트: Nim standard library (asynchttpserver)