emptygif
भाषा: मराठी

सर्व भाषा आणि फ्रेमवर्क

http4s Scala

http4s साठी Scala स्निपेट, जो /pixel.gif वरच्या रिक्वेस्टना empty GIF ने उत्तर देतो. GIF स्टार्टअपला एकदाच डिकोड होतो.

http4s Scala

इन्स्टॉल
libraryDependencies ++= Seq("org.http4s" %% "http4s-ember-server" % "0.23.37", "org.http4s" %% "http4s-dsl" % "0.23.37")
फाइल
Main.scala
import cats.effect.{IO, IOApp}
import com.comcast.ip4s.*
import org.http4s.*
import org.http4s.dsl.io.*
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.headers.{`Cache-Control`, `Content-Type`}

object Main extends IOApp.Simple:
  private val gif = java.util.Base64.getDecoder.decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
  private val PixelPath = Uri.Path.unsafeFromString("/pixel.gif")

  private val routes = HttpRoutes.of[IO] { case GET -> PixelPath =>
    Ok(gif, `Content-Type`(MediaType.image.gif), `Cache-Control`(CacheDirective.`no-store`))
  }

  val run = EmberServerBuilder
    .default[IO]
    .withHost(ipv4"0.0.0.0")
    .withPort(port"8080")
    .withHttpApp(routes.orNotFound)
    .build
    .useForever

आम्ही हा स्निपेट चालवला आणि त्याने परत केलेले बाइट्स तुलना करून पाहिले.

तपासा

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

अधिकृत साइट: http4s

Scala मध्ये आणखी