Lahat ng language at framework
Apache Pekko HTTP Scala
Isang Scala snippet para sa Apache Pekko HTTP na sumasagot sa mga request sa /pixel.gif gamit ang empty GIF, na isang beses lang dine-decode sa startup.
Apache Pekko HTTP Scala
import org.apache.pekko.actor.typed.ActorSystem
import org.apache.pekko.actor.typed.scaladsl.Behaviors
import org.apache.pekko.http.scaladsl.Http
import org.apache.pekko.http.scaladsl.model.{HttpEntity, MediaTypes}
import org.apache.pekko.http.scaladsl.model.headers.`Cache-Control`
import org.apache.pekko.http.scaladsl.model.headers.CacheDirectives.`no-store`
import org.apache.pekko.http.scaladsl.server.Directives.*
@main def pixelServer(): Unit =
given ActorSystem[Nothing] = ActorSystem(Behaviors.empty, "pixel")
val gif = HttpEntity(MediaTypes.`image/gif`, java.util.Base64.getDecoder.decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="))
val route =
path(separateOnSlashes("/pixel.gif".stripPrefix("/"))) {
get {
respondWithHeader(`Cache-Control`(`no-store`)) {
complete(gif)
}
}
}
Http().newServerAt("0.0.0.0", 8080).bind(route)
Pinatakbo namin ang snippet na ito at ikinumpara ang mga byte na ibinalik nito.
Suriin
curl -sI http://localhost:8080/pixel.gif
Opisyal na site: Apache Pekko HTTP