emptygif
Language: English

All languages and frameworks

ZIO HTTP Scala

A Scala snippet for ZIO HTTP that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

ZIO HTTP Scala

Install
libraryDependencies += "dev.zio" %% "zio-http" % "3.11.6"
File
Main.scala
import zio.*
import zio.http.*

object Main extends ZIOAppDefault:
  private val gif = Chunk.fromArray(java.util.Base64.getDecoder.decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="))

  private val routes = Routes(
    Method.GET / "/pixel.gif" -> handler(
      Response(
        headers = Headers(Header.ContentType(MediaType.image.gif), Header.CacheControl.NoStore),
        body = Body.fromChunk(gif),
      )
    )
  )

  override val run = Server.serve(routes).provide(Server.defaultWithPort(8080))

We ran this snippet and compared the bytes it returned.

Check it

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

Official site: ZIO HTTP

More in Scala