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 示例