Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active December 9, 2023 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/c79e1cd2990e6b71c68405b5a317a63e to your computer and use it in GitHub Desktop.
Save dacr/c79e1cd2990e6b71c68405b5a317a63e to your computer and use it in GitHub Desktop.
http server keeping a state at server side / published by https://github.com/dacr/code-examples-manager #50add5a2-2132-4255-a56d-375d1ed6656b/498fd887cc292705fcfc66eea32078cd7f74cb0b
// summary : http server keeping a state at server side
// keywords : scala, zio, tapir, http, zhttp, stateful, state, @testable, @exclusive
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 50add5a2-2132-4255-a56d-375d1ed6656b
// created-on : 2022-03-20T07:26:31+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// test-with : curl -L http://127.0.0.1:8080/hello/david
// ---------------------
//> using scala "3.3.1"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio:1.9.4"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.9.4"
//> using lib "com.softwaremill.sttp.tapir::tapir-swagger-ui-bundle:1.9.4"
// ---------------------
/*
other examples :
- https://github.com/softwaremill/tapir/blob/master/examples/src/main/scala/sttp/tapir/examples/ZioExampleZioHttpServer.scala
*/
import sttp.tapir.ztapir.*
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import sttp.apispec.openapi.Info
import sttp.tapir.swagger.bundle.SwaggerInterpreter
import zio.*, zio.http.Server
/*
curl -L http://127.0.0.1:8080/docs
curl http://127.0.0.1:8080/hello/david
curl http://127.0.0.1:8080/hello/joe
*/
object WebApp extends ZIOAppDefault {
case class AppState(counters: Map[String, Int] = Map.empty) {
def manage(id: String): AppState =
copy(counters = counters + (id -> (1 + counters.getOrElse(id, 0))))
}
// --------------------------------------------------
def helloLogic(name: String) =
for {
ref <- ZIO.service[Ref[AppState]]
state <- ref.updateAndGet(state => state.manage(name))
count = state.counters.getOrElse(name, 0)
} yield s"Hello $name $count"
val helloEndPoint =
endpoint
.description("Returns stateful greeting")
.get
.in("hello" )
.in(path("name").example("joe"))
.out(stringBody)
val helloRoute = helloEndPoint.zServerLogic(helloLogic)
// --------------------------------------------------
val docEndpoints =
SwaggerInterpreter()
.fromServerEndpoints(
List(helloRoute),
Info(title = "Greeting API", version = "1.0", description = Some("Everything required to be polite"))
)
// --------------------------------------------------
val routes = ZioHttpInterpreter().toHttp(List(helloRoute) ++ docEndpoints)
// --------------------------------------------------
val appStateLayer = ZLayer(Ref.make(AppState()))
override def run =
Server
.serve(routes)
.provide(appStateLayer, Server.default)
}
WebApp.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment