Hello everyone,
I’m trying to create a simple server that has some dependencies and I’m following the instructions here, replacing *
with _
as I’m using Scala 3 (I believe that’s correct, but not really sure). This is the code, with comments regarding the compiler errors.
object Http4sMain extends ZIOAppDefault {
object endpoints {
object root {
val root: ZServerEndpoint[Any, Any] =
endpoint.get.out(stringBody).serverLogicSuccess(_ => ZIO.succeed("empty"))
val info: ZServerEndpoint[Indexing & Monitoring, Any] =
endpoint.get.in("info").out(stringBody).serverLogicSuccess(_ => ZIO.succeed("info"))
val health: ZServerEndpoint[Any, Any] =
endpoint.get.in("health").serverLogicSuccess(_ => ZIO.unit)
val all = List(info.widen[Env], health.widen[Env], root.widen[Env])
}
}
type Env = MyConfig & Indexing & Monitoring
// fails to compile with error "Type argument ZIO[Env, Throwable, ?]
// does not have the same kind as its bound"
val routes: HttpRoutes[RIO[Env, _]] = ZHttp4sServerInterpreter().from(endpoints.root.all).toRoutes
def run = ZIO.executor.flatMap { executor =>
BlazeServerBuilder[Task] // Neither Task nor RIO[Env, _] compile, probably because the error above
.withExecutionContext(executor.asExecutionContext)
.bindHttp(8000, "0.0.0.0")
.withHttpApp(Router("/" -> routes).orNotFound)
.resource
.use { server =>
for {
_ <- Console.printLine(
s"Server started at http://0.0.0.0:${server.address.getPort}. Press ENTER key to exit."
)
_ <- Console.readLine
} yield ()
}
}
}
EDIT: Doing this:
type Effect[+A] = RIO[Env, A]
val routes: HttpRoutes[Effect] = ZHttp4sServerInterpreter().from(endpoints.root.all).toRoutes
and then using Effect
as F
for BlazeServerBuilder
does compile, so this brings up two questions:
- What did I do wrong when trying to mimic the kind projector in Scala 3?
- How and where do I
.provide
the dependencies to my effects?
Any help is greatly appreciated!