Skip to content

Instantly share code, notes, and snippets.

@windymelt
Created October 12, 2023 15:43
Show Gist options
  • Save windymelt/af9fed0de84359521bed5cde1804057e to your computer and use it in GitHub Desktop.
Save windymelt/af9fed0de84359521bed5cde1804057e to your computer and use it in GitHub Desktop.
//> using scala 3.3.0
//> using dep org.http4s::http4s-ember-server:0.23.23
//> using dep org.http4s::http4s-dsl:0.23.23
//> using dep org.http4s::http4s-circe:0.23.23
//> using dep "io.circe::circe-generic:0.14.5"
//> using dep "io.circe::circe-literal:0.14.5"
import cats.effect._
import com.comcast.ip4s._
import org.http4s.HttpRoutes
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.ember.server._
// .asJsonを使えるようにする
import io.circe.syntax._
// JSONオブジェクトをレスポンスとして返せるようにする
import org.http4s.circe._
// 自動的にJSONへの変換を導出する
import io.circe.generic.auto._
case class Song(title: String, releasedYear: Int, artist: String)
object Main extends IOApp.Simple {
val routes = HttpRoutes.of[IO] { case GET -> Root =>
Ok(Song("ずんだシェイキング", 2023, "なみぐる").asJson)
}
def run: IO[Unit] =
EmberServerBuilder
.default[IO]
.withHost(ipv4"0.0.0.0")
.withPort(port"8080")
.withHttpApp(routes.orNotFound)
.build
.useForever
.as(ExitCode.Success)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment