Skip to content

Instantly share code, notes, and snippets.

@ghostdogpr
Last active November 3, 2024 07:16
Show Gist options
  • Save ghostdogpr/ff2e394c4948fd15aa9d5f29795c2528 to your computer and use it in GitHub Desktop.
Save ghostdogpr/ff2e394c4948fd15aa9d5f29795c2528 to your computer and use it in GitHub Desktop.
//> using dep dev.zio::zio:2.1.11
//> using dep dev.zio::zio-config-magnolia:4.0.2
//> using dep com.softwaremill.sttp.client3::zio:3.10.1
import sttp.client3.httpclient.zio.HttpClientZioBackend
import zio.*
import zio.config.derivation.name
import zio.config.magnolia.deriveConfig
type Language = String
type SttpClient = sttp.client3.SttpBackend[Task, Any]
case class WordFilterConfig(
@name("WORDFILTER_URL") url: String
)
object WordFilterConfig {
given Config[WordFilterConfig] = deriveConfig[WordFilterConfig]
}
trait WordFilter {
def maskChat(msg: String, language: Option[Language]): Task[String]
}
object WordFilter {
val live: ZLayer[SttpClient, Throwable, WordFilter] =
ZLayer.derive[WordFilterLive]
val test: ZLayer[Any, Nothing, WordFilter] =
ZLayer.derive[WordFilterTest]
}
class WordFilterLive(config: WordFilterConfig, sttp: SttpClient)
extends WordFilter {
def maskChat(msg: String, language: Option[Language]): Task[String] =
???
}
class WordFilterTest extends WordFilter {
def maskChat(msg: String, language: Option[Language]): Task[String] =
ZIO.succeed(msg)
}
class ChatService(wordFilter: WordFilter) {
def chat(message: String): Task[Unit] = ???
}
object ChatService {
val live: ZLayer[WordFilter, Nothing, ChatService] =
ZLayer.derive[ChatService]
}
class GrpcServer(chatService: ChatService) {
def start: Task[Nothing] = ZIO.never
}
object GrpcServer {
val live: ZLayer[ChatService, Nothing, GrpcServer] =
ZLayer.derive[GrpcServer]
}
object App extends ZIOAppDefault {
override val bootstrap =
// this is to tell ZIO to load config keys from env variables
Runtime.setConfigProvider(ConfigProvider.envProvider)
val run =
ZIO
.serviceWithZIO[GrpcServer](_.start)
.provide(
GrpcServer.live,
ChatService.live,
WordFilter.live, // or WordFilter.test
HttpClientZioBackend.layer()
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment