def routes: Route = prepareCombinedRoutes()

in management/src/main/scala/org/apache/pekko/management/scaladsl/PekkoManagement.scala [101:171]


  def routes: Route = prepareCombinedRoutes(providerSettings)

  /**
   * Amend the [[ManagementRouteProviderSettings]] and get the routes for the HTTP management endpoint.
   *
   * Use this when adding authentication and HTTPS.
   *
   * This method can be used to embed the Pekko management routes in an existing Pekko HTTP server.
   *
   * @throws java.lang.IllegalArgumentException if routes not configured for pekko management
   */
  def routes(transformSettings: ManagementRouteProviderSettings => ManagementRouteProviderSettings): Route =
    prepareCombinedRoutes(transformSettings(providerSettings))

  /**
   * Start a Pekko HTTP server to serve the HTTP management endpoint.
   */
  def start(): Future[Uri] =
    start(identity)

  /**
   * Amend the [[ManagementRouteProviderSettings]] and start a Pekko HTTP server to serve
   * the HTTP management endpoint.
   *
   * Use this when adding authentication and HTTPS.
   */
  def start(transformSettings: ManagementRouteProviderSettings => ManagementRouteProviderSettings): Future[Uri] = {
    val serverBindingPromise = Promise[Http.ServerBinding]()
    if (bindingFuture.compareAndSet(null, serverBindingPromise.future)) {
      try {
        val effectiveBindHostname = settings.Http.EffectiveBindHostname
        val effectiveBindPort = settings.Http.EffectiveBindPort
        val effectiveProviderSettings = transformSettings(providerSettings)

        // TODO instead of binding to hardcoded things here, discovery could also be used for this binding!
        // Basically: "give me the SRV host/port for the port called `pekko-bootstrap`"
        // discovery.lookup("_pekko-bootstrap" + ".effective-name.default").find(myaddress)
        // ----
        // FIXME -- think about the style of how we want to make these available

        log.info("Binding Pekko Management (HTTP) endpoint to: {}:{}", effectiveBindHostname, effectiveBindPort)

        val combinedRoutes = prepareCombinedRoutes(effectiveProviderSettings)

        val baseBuilder = Http()
          .newServerAt(effectiveBindHostname, effectiveBindPort)
          .withSettings(ServerSettings(system).withRemoteAddressHeader(true))

        val securedBuilder = effectiveProviderSettings.httpsConnectionContext match {
          case Some(httpsContext) => baseBuilder.enableHttps(httpsContext)
          case None               => baseBuilder
        }
        val serverFutureBinding = securedBuilder.bind(combinedRoutes)

        serverBindingPromise.completeWith(serverFutureBinding).future.flatMap { binding =>
          val boundPort = binding.localAddress.getPort
          log.info(
            ManagementLogMarker.boundHttp(s"$effectiveBindHostname:$boundPort"),
            "Bound Pekko Management (HTTP) endpoint to: {}:{}",
            effectiveBindHostname,
            boundPort)
          selfUriPromise.success(effectiveProviderSettings.selfBaseUri.withPort(boundPort)).future
        }

      } catch {
        case NonFatal(ex) =>
          log.warning(ex.getMessage)
          Future.failed(new IllegalArgumentException("Failed to start Pekko Management HTTP endpoint.", ex))
      }
    } else selfUriPromise.future
  }