emptygif
Harshe: Hausa

Duk harsuna da frameworks

Apache Pekko HTTP Scala

Snippet na Scala don Apache Pekko HTTP da ke amsa requests zuwa /pixel.gif da empty GIF, wanda ake decoding ɗinsa sau ɗaya lokacin farawa.

Apache Pekko HTTP Scala

Girkawa
libraryDependencies ++= Seq("org.apache.pekko" %% "pekko-http" % "1.4.0", "org.apache.pekko" %% "pekko-stream" % "1.2.1")
Fayil
Main.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)

Mun gudanar da wannan snippet kuma mun kwatanta bytes ɗin da ya mayar.

Duba shi

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

Shafin hukuma: Apache Pekko HTTP

Ƙari a Scala