diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/scala/xyz/driver/core/app.scala | 31 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/rest.scala | 38 |
2 files changed, 57 insertions, 12 deletions
diff --git a/src/main/scala/xyz/driver/core/app.scala b/src/main/scala/xyz/driver/core/app.scala index e66fe73..c4f72ad 100644 --- a/src/main/scala/xyz/driver/core/app.scala +++ b/src/main/scala/xyz/driver/core/app.scala @@ -18,9 +18,8 @@ import com.typesafe.scalalogging.Logger import io.swagger.models.Scheme import io.swagger.util.Json import org.slf4j.{LoggerFactory, MDC} -import spray.json.DefaultJsonProtocol import xyz.driver.core -import xyz.driver.core.rest.{ContextHeaders, Swagger} +import xyz.driver.core.rest._ import xyz.driver.core.stats.SystemStats import xyz.driver.core.time.Time import xyz.driver.core.time.provider.{SystemTimeProvider, TimeProvider} @@ -30,6 +29,7 @@ import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, Future} import scala.reflect.runtime.universe._ import scala.util.control.NonFatal +import scala.util.Try import scalaz.Scalaz.stringInstance import scalaz.syntax.equal._ @@ -180,6 +180,7 @@ object app { } protected def versionRoute(version: String, gitHash: String, startupTime: Time): Route = { + import spray.json._ import DefaultJsonProtocol._ import SprayJsonSupport._ @@ -187,17 +188,27 @@ object app { val currentTime = time.currentTime().millis complete( Map( - "version" -> version, - "gitHash" -> gitHash, - "modules" -> modules.map(_.name).mkString(", "), - "startupTime" -> startupTime.millis.toString, - "serverTime" -> currentTime.toString, - "uptime" -> (currentTime - startupTime.millis).toString - )) + "version" -> version.toJson, + "gitHash" -> gitHash.toJson, + "modules" -> modules.map(_.name).toJson, + "dependencies" -> collectAppDependencies().toJson, + "startupTime" -> startupTime.millis.toString.toJson, + "serverTime" -> currentTime.toString.toJson, + "uptime" -> (currentTime - startupTime.millis).toString.toJson + ).toJson) } } + protected def collectAppDependencies(): Map[String, String] = { + + def serviceWithLocation(serviceName: String): (String, String) = + serviceName -> Try(config.getString(s"services.$serviceName.baseUrl")).getOrElse("not-detected") + + modules.flatMap(module => module.serviceDiscovery.getUsedServices.map(serviceWithLocation).toSeq).toMap + } + protected def healthRoute: Route = { + import spray.json._ import DefaultJsonProtocol._ import SprayJsonSupport._ import spray.json._ @@ -273,6 +284,8 @@ object app { def route: Route def routeTypes: Seq[Type] + val serviceDiscovery: ServiceDiscovery with SavingUsedServiceDiscovery = new NoServiceDiscovery() + def activate(): Unit = {} def deactivate(): Unit = {} } diff --git a/src/main/scala/xyz/driver/core/rest.scala b/src/main/scala/xyz/driver/core/rest.scala index 6944cf7..be4fcd8 100644 --- a/src/main/scala/xyz/driver/core/rest.scala +++ b/src/main/scala/xyz/driver/core/rest.scala @@ -9,14 +9,18 @@ import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.{HttpChallenges, RawHeader} import akka.http.scaladsl.server.AuthenticationFailedRejection.CredentialsRejected +import akka.http.scaladsl.server.Directive0 +import com.typesafe.scalalogging.Logger import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller} +import akka.http.scaladsl.settings.ClientConnectionSettings +import akka.http.scaladsl.settings.ConnectionPoolSettings +import akka.http.scaladsl.model.headers.`User-Agent` import akka.stream.ActorMaterializer import akka.stream.scaladsl.Flow import akka.util.ByteString import com.github.swagger.akka.model._ import com.github.swagger.akka.{HasActorSystem, SwaggerHttpService} import com.typesafe.config.Config -import com.typesafe.scalalogging.Logger import io.swagger.models.Scheme import pdi.jwt.{Jwt, JwtAlgorithm} import xyz.driver.core.auth._ @@ -344,7 +348,26 @@ package rest { def discover[T <: Service](serviceName: Name[Service]): T } - class HttpRestServiceTransport(actorSystem: ActorSystem, + class NoServiceDiscovery extends ServiceDiscovery with SavingUsedServiceDiscovery { + + def discover[T <: Service](serviceName: Name[Service]): T = + throw new IllegalArgumentException(s"Service with name $serviceName is unknown") + } + + trait SavingUsedServiceDiscovery { + + private val usedServices = new scala.collection.mutable.HashSet[String]() + + def saveServiceUsage(serviceName: Name[Service]): Unit = usedServices.synchronized { + usedServices += serviceName.value + } + + def getUsedServices: Set[String] = usedServices.synchronized { usedServices.toSet } + } + + class HttpRestServiceTransport(applicationName: Name[App], + applicationVersion: String, + actorSystem: ActorSystem, executionContext: ExecutionContext, log: Logger, time: TimeProvider) @@ -353,6 +376,15 @@ package rest { protected implicit val materializer = ActorMaterializer()(actorSystem) protected implicit val execution = executionContext + private val client = Http()(actorSystem) + + private val clientConnectionSettings: ClientConnectionSettings = + ClientConnectionSettings(actorSystem).withUserAgentHeader( + Option(`User-Agent`(applicationName.value + "/" + applicationVersion))) + + private val connectionPoolSettings: ConnectionPoolSettings = ConnectionPoolSettings(actorSystem) + .withConnectionSettings(clientConnectionSettings) + def sendRequestGetResponse(context: ServiceRequestContext)(requestStub: HttpRequest): Future[HttpResponse] = { val requestTime = time.currentTime() @@ -365,7 +397,7 @@ package rest { log.info(s"Sending request to ${request.method} ${request.uri}") - val response = Http()(actorSystem).singleRequest(request)(materializer) + val response = client.singleRequest(request, settings = connectionPoolSettings)(materializer) response.onComplete { case Success(r) => |