emptygif
語言: 繁體中文

所有語言和框架

Vapor Swift

適用於 Vapor 的 Swift 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。

Vapor Swift

安裝
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
檔案
Server.swift
執行
swift run
import Foundation
import Vapor

let gif = Data(base64Encoded: "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")!

@main
struct Server {
    static func main() async throws {
        let app = try await Application.make(.detect())
        app.http.server.configuration.port = 8080

        app.get("/pixel.gif".pathComponents) { _ in
            Response(
                status: .ok,
                headers: ["Content-Type": "image/gif", "Cache-Control": "no-store"],
                body: .init(data: gif)
            )
        }

        try await app.execute()
        try await app.asyncShutdown()
    }
}

我們執行了這段程式碼,並比對了它回傳的位元組。

驗證

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

官方網站: Vapor

更多 Swift 範例