-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.scala
50 lines (41 loc) · 1.48 KB
/
demo.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sttp.tapir.server.jdkhttp.*
import smithy4s.deriving.{*, given}
import smithy4s.deriving.aliases.*
import scala.annotation.experimental
import sttp.monad.MonadError
import java.net.InetSocketAddress
import com.sun.net.httpserver._
@experimental
@simpleRestJson
class HelloWorld derives API {
@httpGet("/hello/{name}")
def greet(@httpLabel name: String): String = s"Hello $name!"
}
@main
@experimental
def main(): Unit = {
// copied from Tapir
// TODO put the equivalent `MonadThrowLike` somewhere
implicit val idMonad: MonadError[Id] = new MonadError[Id] {
override def unit[T](t: T): Id[T] = t
override def map[T, T2](fa: Id[T])(f: T => T2): Id[T2] = f(fa)
override def flatMap[T, T2](fa: Id[T])(f: T => Id[T2]): Id[T2] = f(fa)
override def error[T](t: Throwable): Id[T] = throw t
override protected def handleWrappedError[T](rt: Id[T])(
h: PartialFunction[Throwable, Id[T]]
): Id[T] = rt
override def eval[T](t: => T): Id[T] = t
override def ensure[T](f: Id[T], e: => Id[Unit]): Id[T] =
try f
finally e
}
val handler =
JdkHttpServerInterpreter(JdkHttpServerOptions.Default).toHandler(
tapirBridge(new HelloWorld().liftService[Id], simpleRestJsonCodecs[Id])
)
val socketAddress = new InetSocketAddress("localhost", 8080)
val server = HttpServer.create(socketAddress, 64)
server.createContext(JdkHttpServerOptions.Default.basePath, handler)
server.start()
println(s"Running on : ${server.getAddress()}")
}