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)