All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.server.SunHttpServer.kt Maven / Gradle / Ivy

There is a newer version: 5.31.0.0
Show newest version
package org.http4k.server

import com.sun.net.httpserver.HttpServer
import org.http4k.core.HttpHandler
import java.net.InetSocketAddress
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors.newWorkStealingPool
import java.util.concurrent.TimeUnit.MILLISECONDS

internal fun SunHttpServer(
    http: HttpHandler,
    port: Int,
    stopMode: ServerConfig.StopMode,
    executor: ExecutorService = newWorkStealingPool()
) = object : Http4kServer {
    override fun port(): Int = if (port > 0) port else server.address.port

    private val server = HttpServer.create(InetSocketAddress(port), 1000)

    override fun start(): Http4kServer = apply {
        server.createContext("/", HttpExchangeHandler(http))
        server.executor = executor
        server.start()
    }

    override fun stop() = apply {
        if (stopMode is ServerConfig.StopMode.Graceful) {
            executor.shutdown()
            executor.awaitTermination(stopMode.timeout.toMillis(), MILLISECONDS)
        }
        server.stop(0)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy