Skip to content

Commit

Permalink
chore: Support customize server and socket options of undertow
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Jan 18, 2025
1 parent 05ca192 commit 7f090b0
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import cask.model._
import cask.router.{Decorator, EndpointMetadata, EntryPoint, Result}
import cask.util.Logger
import io.undertow.Undertow
import io.undertow.server.handlers.BlockingHandler
import io.undertow.server.{HttpHandler, HttpServerExchange}
import io.undertow.server.handlers.BlockingHandler
import io.undertow.util.HttpString
import org.xnio.Options

import java.util.concurrent.ExecutorService

Expand Down Expand Up @@ -83,12 +84,28 @@ abstract class Main{
Main.defaultHandleError(routes, metadata, e, debugMode, req)
}

/**
* Set server options for the undertow server. By default, no options are set.
* */
protected def serverOptions : List[(org.xnio.Option[_], Any)] = Nil

/**
* Set server options for the undertow server. By default, no options are set.
* */
protected def socketOptions : List[(org.xnio.Option[_], Any)] = Nil

def main(args: Array[String]): Unit = {
if (!verbose) Main.silenceJboss()
val server = Undertow.builder
val builder = Undertow.builder
.addHttpListener(port, host)
.setHandler(defaultHandler)
.build
serverOptions.foreach {
case (option: org.xnio.Option[Any @unchecked], value) => builder.setServerOption(option, value)
}
socketOptions.foreach {
case (option: org.xnio.Option[Any @unchecked], value) => builder.setSocketOption(option, value)
}
val server = builder.build()
server.start()
//register an on exit hook to stop the server
Runtime.getRuntime.addShutdownHook(new Thread(() => {
Expand Down

0 comments on commit 7f090b0

Please sign in to comment.