-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple default bundle compatible with Scala toolkit #3914
Comments
for example, if I bundle the jdkhttp, and upickle json handlers in one object like this package scala.toolkit
import sttp.tapir.Tapir
import sttp.tapir.server.jdkhttp
import sttp.tapir.json.upickle.TapirJsonuPickle
import scala.util.Try
object httpserver extends Tapir
with TapirJsonuPickle:
private val ujsonSchema = Schema.schemaForString.map(
s => Try(ujson.read(s)).toOption
)(ujson.write(_))
given Schema[ujson.Value] = ujsonSchema
def ujsonBody = jsonBody[ujson.Value]
export sttp.tapir.Schema
export jdkhttp.{HttpServer, HttpsConfigurator}
export jdkhttp.{JdkHttpResponseBody, JdkHttpServer, JdkHttpServerInterpreter, JdkHttpServerOptions} then I can make a very simple main app like so package app
import scala.toolkit.httpserver.*
import java.time.LocalDate
val date = endpoint.get.in("date").out(ujsonBody)
.handleSuccess(_ => ujson.Obj("date" -> LocalDate.now().toString))
@main def launch() =
val server = JdkHttpServer()
.addEndpoint(date)
.port(8080)
.host("localhost")
.executor(scala.concurrent.ExecutionContext.global)
.start()
println("Server started at http://localhost:8080, ctrl-c to stop")
sys.addShutdownHook:
server.stop(0) An open question is then "what about JDK 21?" - then we could bundle the NettySyncServer instead maybe? we'd have to make a policy in scala/toolkit about JDK support though |
Definitely |
ok so would you accept a PR to add a new artefact that can bundle a few of these things in one import? (based on |
I see also that NettySyncServer requires Ox and is only Scala 3, so this complicates it possibly |
Sure why not - we already have a "swagger bundle", the same way we could have a "direct bundle". I don't understand the |
Oh I wasn't aware of this module - it seems good - the reason I defined ujsonBody in this example was so you can do very basic serialisation of |
So there are no default ReadWriters for |
Do you think it would be possible to bundle a bunch of defaults into an object for building simple apps in direct style
e.g. JdkHttpServer, Upickle json
Then there can be fewer imports needed
with this simple bundle it would be useful to recommend for bundling with scala toolkit
The text was updated successfully, but these errors were encountered: