emptygif
言語: 日本語

すべての言語・フレームワーク

Vapor Swift

/pixel.gif へのリクエストに空の GIF を返す、Vapor 用の Swift スニペット。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 の他の例