Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 15, 2024 07:58
Show Gist options
  • 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/30d18165ab5c89aa2c3453f05888f655a527baab
// 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.4.2"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio:1.10.7"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.10.7"
//> using dep "com.softwaremill.sttp.tapir::tapir-swagger-ui-bundle:1.10.7"
// ---------------------
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
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, age: Option[Int]) =
for {
ref <- ZIO.service[Ref[AppState]]
state <- ref.updateAndGet(state => state.manage(name))
count = state.counters.getOrElse(name, 0)
} yield s"Hello $age $name $count"
val helloEndPoint =
endpoint
.tag("Greetings")
.name("hello")
.description("Returns stateful greeting")
.get
.in("hello")
.in(path("name").example("joe"))
.in(query[Option[Int]]("age"))
.out(stringBody)
val helloRoute = helloEndPoint.zServerLogic(helloLogic)
// --------------------------------------------------
val apiDocRoute =
SwaggerInterpreter()
.fromServerEndpoints(
List(helloRoute),
Info(
title = "Greeting API",
version = "1.0",
description = Some("Everything required to be polite")
)
)
// --------------------------------------------------
val routes = ZioHttpInterpreter().toHttp(List(helloRoute) ++ apiDocRoute)
// --------------------------------------------------
val appStateLayer = ZLayer(Ref.make(AppState()))
val server = for {
_ <- ZIO.log("API documentation : http://127.0.0.1:8080/docs")
_ <- Server.serve(routes)
} yield ()
override def run = server.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