<commit_before>package io.flow.play.clients

import io.flow.play.util.{Config, EnvironmentConfig, FlowEnvironment, PropertyConfig}
import io.flow.registry.v0.{Authorization, Client}
import io.flow.registry.v0.errors.UnitResponse
import io.flow.registry.v0.models.Application
import play.api.Logger
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.Duration

/**
  * This class implements service discovery for flow based on the
  * environment in which we are in. In production, hostnames are build
  * using convention (e.g. 'user' => 'user.api.flow.io'). In
  * development, hostnames are built by querying the registry for port
  * mappings.
  * 
  * Example:
  *
  *    lazy val client = new Client(new Registry(env).host("user"))
  */
trait Registry {

  /**
    * Returns the host of the application
    * (e.g. http://user.api.flow.io or http://vm:6011)
    */
  def host(applicationId: String): String

}

object RegistryConstants {

  val ProductionDomain = "api.flow.io"

  val WorkstationHostVariableName = "WORKSTATION_HOST"

  val DefaultWorkstationHost = "ws"

  /**
    * Defaults to the workstation host
    */
  private[this] lazy val devHost: String = workstationHost

  /**
    * The resolved name of the host used in workstation
    */
  private[this] lazy val workstationHost: String = {
    EnvironmentConfig.optionalString(WorkstationHostVariableName).getOrElse {
      PropertyConfig.optionalString(WorkstationHostVariableName).getOrElse {
        Logger.info(s"[${getClass.getName}] defaulting workstationHost to '$DefaultWorkstationHost' (can override via env var[$WorkstationHostVariableName])")
        DefaultWorkstationHost
      }
    }
  }

  def log(env: String, applicationId: String, message: String) {
    Logger.info(s"[${getClass.getName} $env] app[$applicationId] $message")
  }

  /**
    * Returns the hostname of the specified application in the
    * production environment.
    */
  def productionHost(applicationId: String): String = {
    s"https://${applicationId}.${ProductionDomain}"
  }

  def developmentHost(applicationId: String, port: Long): String = {
    s"http://$devHost:$port"
  }

  def workstationHost(applicationId: String, port: Long): String = {
    s"http://$workstationHost:$port"
  }

  def host(applicationId: String, port: Long) = {
    FlowEnvironment.Current match {
      case FlowEnvironment.Production => productionHost(applicationId)
      case FlowEnvironment.Development => developmentHost(applicationId, port)
      case FlowEnvironment.Workstation => workstationHost(applicationId, port)
    }
  }

}

/**
  * Production works by convention with no external dependencies.
  */
class ProductionRegistry() extends Registry {

  override def host(applicationId: String) = {
    val host = RegistryConstants.productionHost(applicationId)
    RegistryConstants.log("Production", applicationId, s"Host[$host]")
    host
  }

}

@javax.inject.Singleton
class DevelopmentRegistry @javax.inject.Inject() (
  app: play.api.Application,
  config: Config
) extends Registry {

  private[this] lazy val RegistryHost: String = {
    val applicationId = "registry"
    val varName = overriddeVariableName(applicationId)

    overridden(applicationId) match {
      case Some(host) => {<commit_msg>Remove Application argument from DevelopmentRegistry (#209)<commit_after>package io.flow.play.clients

import io.flow.play.util.{Config, EnvironmentConfig, FlowEnvironment, PropertyConfig}
import io.flow.registry.v0.Client
import io.flow.registry.v0.errors.UnitResponse
import io.flow.registry.v0.models.Application
import play.api.Logger
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.Duration

/**
  * This class implements service discovery for flow based on the
  * environment in which we are in. In production, hostnames are build
  * using convention (e.g. 'user' => 'user.api.flow.io'). In
  * development, hostnames are built by querying the registry for port
  * mappings.
  * 
  * Example:
  *
  *    lazy val client = new Client(new Registry(env).host("user"))
  */
trait Registry {

  /**
    * Returns the host of the application
    * (e.g. http://user.api.flow.io or http://vm:6011)
    */
  def host(applicationId: String): String

}

object RegistryConstants {

  val ProductionDomain = "api.flow.io"

  val WorkstationHostVariableName = "WORKSTATION_HOST"

  val DefaultWorkstationHost = "ws"

  /**
    * Defaults to the workstation host
    */
  private[this] lazy val devHost: String = workstationHost

  /**
    * The resolved name of the host used in workstation
    */
  private[this] lazy val workstationHost: String = {
    EnvironmentConfig.optionalString(WorkstationHostVariableName).getOrElse {
      PropertyConfig.optionalString(WorkstationHostVariableName).getOrElse {
        Logger.info(s"[${getClass.getName}] defaulting workstationHost to '$DefaultWorkstationHost' (can override via env var[$WorkstationHostVariableName])")
        DefaultWorkstationHost
      }
    }
  }

  def log(env: String, applicationId: String, message: String) {
    Logger.info(s"[${getClass.getName} $env] app[$applicationId] $message")
  }

  /**
    * Returns the hostname of the specified application in the
    * production environment.
    */
  def productionHost(applicationId: String): String = {
    s"https://${applicationId}.${ProductionDomain}"
  }

  def developmentHost(applicationId: String, port: Long): String = {
    s"http://$devHost:$port"
  }

  def workstationHost(applicationId: String, port: Long): String = {
    s"http://$workstationHost:$port"
  }

  def host(applicationId: String, port: Long) = {
    FlowEnvironment.Current match {
      case FlowEnvironment.Production => productionHost(applicationId)
      case FlowEnvironment.Development => developmentHost(applicationId, port)
      case FlowEnvironment.Workstation => workstationHost(applicationId, port)
    }
  }

}

/**
  * Production works by convention with no external dependencies.
  */
class ProductionRegistry() extends Registry {

  override def host(applicationId: String) = {
    val host = RegistryConstants.productionHost(applicationId)
    RegistryConstants.log("Production", applicationId, s"Host[$host]")
    host
  }

}

@javax.inject.Singleton
class DevelopmentRegistry @javax.inject.Inject() (
  config: Config
) extends Registry {

  private[this] lazy val RegistryHost: String = {
    val applicationId = "registry"
    val varName = overriddeVariableName(applicationId)

    overridden(applicationId) match {
      case Some(host) => {
{'fraction_non_alphanumeric': 0.10872198667474257, 'fraction_numerical': 0.00257419745608722, 'mean_word_length': 4.030464584920031, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1773202', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1852, 'n_words': 480}