emptygif
语言: 简体中文

所有语言和框架

Ktor Kotlin

一个用于 Ktor 的 Kotlin 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Ktor Kotlin

安装
implementation("io.ktor:ktor-server-netty:3.6.0")
文件
Application.kt
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.response.header
import io.ktor.server.response.respondBytes
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import kotlin.io.encoding.Base64

private val GIF = Base64.decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

fun main() {
    embeddedServer(Netty, port = 8080) {
        routing {
            get("/pixel.gif") {
                call.response.header(HttpHeaders.CacheControl, "no-store")
                call.respondBytes(GIF, ContentType.Image.GIF)
            }
        }
    }.start(wait = true)
}

我们运行了此代码片段,并比对了它返回的字节。

验证

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

官方网站: Ktor

更多 Kotlin 示例