emptygif
語言: 繁體中文

所有語言和框架

Nim standard library (asynchttpserver) Nim

適用於 Nim standard library (asynchttpserver) 的 Nim 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,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)