HttpServers.kt

  1. package com.hexagontk.http.server

  2. import com.hexagontk.core.text.Ansi.RESET
  3. import com.hexagontk.core.text.AnsiColor.BLUE
  4. import com.hexagontk.core.text.AnsiColor.CYAN
  5. import com.hexagontk.core.text.AnsiColor.DEFAULT
  6. import com.hexagontk.core.text.AnsiEffect.BOLD
  7. import com.hexagontk.http.handlers.HandlerBuilder
  8. import com.hexagontk.http.handlers.HttpHandler

  9. /**
  10.  * Default server banner message.
  11.  */
  12. val serverBanner: String = """
  13.     $CYAN          _________
  14.     $CYAN         /         \
  15.     $CYAN        /   ____   /
  16.     $CYAN       /   /   /  /
  17.     $CYAN      /   /   /__/$BLUE   /\$BOLD    H E X A G O N$RESET
  18.     $CYAN     /   /$BLUE          /  \$DEFAULT        ___
  19.     $CYAN     \  /$BLUE   ___    /   /
  20.     $CYAN      \/$BLUE   /  /   /   /$CYAN    T O O L K I T$RESET
  21.     $BLUE          /  /___/   /
  22.     $BLUE         /          /
  23.     $BLUE         \_________/       https://hexagontk.com/http_server
  24.     $RESET
  25.     """.trimIndent()

  26. /**
  27.  * Create a server and start it.
  28.  *
  29.  * @param adapter Adapter instance which implements [HttpServerPort].
  30.  * @param handler Handler to be used by the server.
  31.  * @param settings Server settings info.
  32.  *
  33.  * @return The started [HttpServer] instance.
  34.  */
  35. fun serve(
  36.     adapter: HttpServerPort,
  37.     handler: HttpHandler,
  38.     settings: HttpServerSettings = HttpServerSettings()
  39. ): HttpServer =
  40.     HttpServer(adapter, handler, settings).apply { start() }

  41. /**
  42.  * Create a server and start it.
  43.  *
  44.  * @param adapter Adapter instance which implements [HttpServerPort].
  45.  * @param settings Server settings info.
  46.  * @param block Lambda to be used to create the server's handlers.
  47.  *
  48.  * @return The started [HttpServer] instance.
  49.  */
  50. fun serve(
  51.     adapter: HttpServerPort,
  52.     settings: HttpServerSettings = HttpServerSettings(),
  53.     block: HandlerBuilder.() -> Unit
  54. ): HttpServer =
  55.     HttpServer(adapter, settings, block).apply { start() }