HttpServerSettings.kt

  1. package com.hexagontk.http.server

  2. import com.hexagontk.core.Platform
  3. import com.hexagontk.http.SslSettings
  4. import com.hexagontk.http.model.HttpProtocol
  5. import com.hexagontk.http.model.HttpProtocol.HTTP
  6. import java.net.InetAddress
  7. import java.net.URI

  8. /**
  9.  * Holds server settings info.
  10.  *
  11.  * @property bindAddress Address for the server.
  12.  * @property bindPort Port for the server.
  13.  * @property protocol Server's protocol.
  14.  * @property sslSettings SSL settings info for configuring the server.
  15.  * @property zip Option to compress server responses.
  16.  * @property bindUrl Base URI to connect to the server. It lacks the port (as it can be dynamic).
  17.  */
  18. class HttpServerSettings(
  19.     val bindAddress: InetAddress = InetAddress.getLoopbackAddress(),
  20.     val bindPort: Int = 2010,
  21.     val protocol: HttpProtocol = HTTP,
  22.     val sslSettings: SslSettings? = null,
  23.     val zip: Boolean = false,
  24. ) {
  25.     val bindUrl: URI by lazy {
  26.         val scheme = if (protocol == HTTP) "http" else "https"
  27.         val hostName =
  28.             if (bindAddress.isAnyLocalAddress) Platform.ip else bindAddress.canonicalHostName

  29.         URI("$scheme://$hostName")
  30.     }

  31.     fun with(
  32.         bindAddress: InetAddress = this.bindAddress,
  33.         bindPort: Int = this.bindPort,
  34.         protocol: HttpProtocol = this.protocol,
  35.         sslSettings: SslSettings? = this.sslSettings,
  36.         zip: Boolean = this.zip,
  37.     ): HttpServerSettings =
  38.         HttpServerSettings(bindAddress, bindPort, protocol, sslSettings, zip)
  39. }