ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Helidon Nima Server
    scala/tapir 2024. 4. 8. 13:15

    GitHub Code

    https://github.com/windbird123/scala3-tapir-helidon

     

    Gzip compression (Helidon Nima)

    아래 dependency 를 추가하면 자동으로 적용되는 듯 하다. (build.sbt)

     libraryDependencies += "io.helidon.http.encoding" % "helidon-http-encoding-gzip" % "4.0.7"
     libraryDependencies += "io.helidon.http.encoding" % "helidon-http-encoding-deflate" % "4.0.7"

     

    HOCON config (Helidon Nima)

    • dependency (build.sbt)
    • libraryDependencies += "io.helidon.config" % "helidon-config-hocon" % "4.0.7"

     

    • src/main/resources/application.conf
      config 상세 옵션: https://helidon.io/docs/v4/config/io_helidon_webserver_WebServer
      server {
        port = 8080
        host = 0.0.0.0
      }​
    • 예제
      import io.helidon.config.Config
      
      val config: Config = Config.create
      val port: Int = config.get("server.port").asInt.get()
      val port: Int = config.get("server.port").asInt.orElse(8080)
      
      import scala.jdk.CollectionConverters.*
      val tables: List[String] = config.get("database.tables").asList(classOf[String]).get().asScala.toList



    CORS (Tapir)

    val customServerOptions: NimaServerOptions = NimaServerOptions.customiseInterceptors
        .corsInterceptor(CORSInterceptor.default[Id])
        .options
    
    val handler = NimaServerInterpreter(customServerOptions).toHandler(List(helloEndpoint))​

     

    Serving StaticFiles (Tapir)

    https://tapir.softwaremill.com/en/latest/endpoint/static.html 참고

    • dependency (build.sbt)
      helidon 4.0.0 compression 버그로, 상위 버전의 helidon-webserver 를 사용하도록 강제한다.
      libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-files" % "1.10.4"
      libraryDependencies += "io.helidon.webserver" % "helidon-webserver" % "4.0.7"​
    • code (tapir)
        // http://localhost:8080/docs/data.txt --> c:/now/data.txt
        val staticEndpoint = staticFilesGetServerEndpoint[Id]("docs")("c:/now")​

     

    Full Code

    • dependency (build.sbt)
      libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-nima-server" % "1.10.3"​

     

    • full code
        import io.helidon.config.Config
        import io.helidon.webserver.WebServer
        import sttp.tapir.*
        import sttp.tapir.files.*
        import sttp.tapir.server.interceptor.cors.CORSInterceptor
        import sttp.tapir.server.nima.{Id, NimaServerInterpreter, NimaServerOptions}
      
        val config: Config = Config.create
        
        // val port: Int = config.get("server.port").asInt.orElse(8080)
        // println(port)
      
        val helloEndpoint = endpoint.get
          .in("hello")
          .out(stringBody)
          .serverLogicSuccess[Id] { _ =>
            Thread.sleep(1000)
            "hello, world!"
          }
      
        // http://localhost:8080/docs/data.txt --> c:/now/data.txt
        val staticEndpoint = staticFilesGetServerEndpoint[Id]("docs")("c:/now")
      
        val customServerOptions: NimaServerOptions = NimaServerOptions.customiseInterceptors
          .corsInterceptor(CORSInterceptor.default[Id])
          .options
      
        val handler = NimaServerInterpreter(customServerOptions).toHandler(List(helloEndpoint, staticEndpoint))
      
        WebServer
          .builder()
          .config(config.get("server"))
          .routing { builder =>
            builder.any(handler)
            ()
          }
          .build()
          .start()

     

     

    댓글

Designed by Tistory.