Created
January 20, 2022 16:32
-
-
Save kubukoz/d824ecd6a17b43e16ffd146f75965151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using lib org.http4s::http4s-ember-server:0.23.7 | |
// using lib org.http4s::http4s-blaze-server:0.23.7 | |
// using lib org.http4s::http4s-circe:0.23.7 | |
// using lib org.http4s::http4s-dsl:0.23.7 | |
// using scala 2.13.7 | |
// using options -Xsource:3.0 -Wunused:imports -deprecation | |
import cats.effect.IO | |
import cats.effect.IOApp | |
import io.circe.Decoder | |
import org.http4s.HttpRoutes | |
import org.http4s.blaze.server.BlazeServerBuilder | |
import org.http4s.circe.CirceEntityCodec._ | |
import org.http4s.dsl.io._ | |
import org.http4s.server.middleware.CORS | |
import org.http4s.server.middleware.CORSConfig | |
object demo extends IOApp.Simple { | |
case class Foo(s: String) | |
implicit val decoder: Decoder[Foo] = Decoder.forProduct1("s")(Foo.apply) | |
def run: IO[Unit] = | |
BlazeServerBuilder[IO] | |
.withHttpApp { | |
CORS( | |
HttpRoutes | |
.of[IO] { case req @ POST -> Root / "foo" => req.as[Foo] *> Ok() } | |
.orNotFound, | |
CORSConfig | |
.default | |
.withAllowCredentials(false) | |
.withAnyOrigin(false) | |
.withAllowedOrigins(Set("http://localhost:8080")) | |
.withAnyMethod(true), | |
) | |
} | |
.resource | |
.useForever | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment