Ktor Kotlin
適用於 Ktor 的 Kotlin 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。
Ktor Kotlin
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