Created
November 23, 2022 18:25
-
-
Save wadouk/dbe455395463ebd821c6535ee4843639 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package server | |
import sttp.capabilities.zio.ZioStreams | |
import sttp.tapir.generic.auto.* | |
import sttp.tapir.json.zio.TapirJsonZio | |
import sttp.tapir.server.ziohttp.ZioHttpInterpreter | |
import sttp.tapir.ztapir.* | |
import sttp.tapir.{json, Endpoint, PublicEndpoint} | |
import zhttp.http.* | |
import zhttp.http.middleware.Cors.CorsConfig | |
import zhttp.service.Server | |
import zio.{ZIO, ZLayer} | |
object MyServer: | |
case class ServerConfig(port: Int) | |
case class Hello(int: Int) | |
import zio.json.* | |
implicit val helloDecoder: JsonDecoder[Hello] = DeriveJsonDecoder.gen[Hello] | |
implicit val helloEncoder: JsonEncoder[Hello] = DeriveJsonEncoder.gen[Hello] | |
val serverSetup: ZIO[Any, Nothing, Server[Any, Throwable]] = | |
def countCharacters(s: String): ZIO[Any, Nothing, Hello] = ZIO.succeed(Hello(s.length)) | |
val apiEndpoint = endpoint.securityIn(cookie[String]("hey")).zServerSecurityLogic(_ => ZIO.succeed(1)) | |
val countCharactersEndpoint = apiEndpoint.in(stringBody).out(json.zio.jsonBody[Hello]) | |
val value = countCharactersEndpoint.serverLogic(_ => countCharacters) | |
val countCharactersHttp: Http[Any, Throwable, Request, Response] = ZioHttpInterpreter().toHttp(value) | |
val serverConfig = ServerConfig(2000) | |
for _ <- ZIO.logInfo(s"Starting iesm-back on port ${serverConfig.port}") | |
yield Server.port(serverConfig.port) ++ Server.app(countCharactersHttp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get error