Skip to content

Instantly share code, notes, and snippets.

@majk-p
Created July 3, 2023 08:38
Show Gist options
  • Save majk-p/90908f751a6a94be6a7c9817d5a958c7 to your computer and use it in GitHub Desktop.
Save majk-p/90908f751a6a94be6a7c9817d5a958c7 to your computer and use it in GitHub Desktop.
MatchError: null with endpoint output initialized after endpoint definition - http4s server interpreter
//> using scala "3.3.0"
//> using dep "org.typelevel::cats-effect:3.5.1"
//> using dep "com.softwaremill.sttp.tapir::tapir-core:1.6.0"
//> using dep "com.softwaremill.sttp.tapir::tapir-cats:1.6.0"
//> using dep "com.softwaremill.sttp.tapir::tapir-http4s-server:1.6.0"
//> using dep "com.softwaremill.sttp.tapir::tapir-swagger-ui-bundle:1.6.0"
//> using dep "org.http4s::http4s-ember-server:0.23.22"
//> using dep "org.slf4j:slf4j-simple:1.7.36"
import cats.effect.ExitCode
import cats.effect.IO
import cats.effect.IOApp
import com.comcast.ip4s.Port
import sttp.model.Uri.*
import sttp.model.*
import sttp.model.headers.CookieValueWithMeta
import sttp.tapir.*
import sttp.tapir.swagger.bundle.SwaggerInterpreter
import sttp.tapir.server.http4s.Http4sServerInterpreter
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.Router
import sttp.tapir.server.http4s.Http4sServerOptions
val authorize: Endpoint[Unit, Unit, Unit, CookieValueWithMeta, Any] =
endpoint
.in("auth" / "authorize")
.summary("Create user session")
.get
.out(setSessionCookie)
val setSessionCookie: sttp.tapir.EndpointIO.Header[CookieValueWithMeta] =
setCookie("SESSION")
val authorizeServerEndpoint =
authorize.serverLogicOption(_ =>
IO.pure(
CookieValueWithMeta.safeApply("cookieValue").toOption
)
)
object Main extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
val endpoints = List(authorizeServerEndpoint)
val options = Http4sServerOptions.defaultServerLog[IO]
val routes = Http4sServerInterpreter[IO]().toRoutes(endpoints)
EmberServerBuilder
.default[IO]
.withPort(Port.fromInt(9999).get)
.withHttpApp(Router("/" -> routes).orNotFound)
.build
.use { _ => IO.never }
.as(ExitCode.Success)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment