From 56f7aa3b6a45d4309cc2c77fcac7e6fda5f45e55 Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 8 Nov 2017 04:09:28 -0800 Subject: App initialization convenience methods --- src/main/scala/xyz/driver/core/app/init.scala | 118 +++++++++++++++++++++ src/main/scala/xyz/driver/core/app/module.scala | 18 ++++ .../driver/core/logging/MdcExecutionContext.scala | 19 ++-- src/main/scala/xyz/driver/core/swagger.scala | 7 ++ 4 files changed, 151 insertions(+), 11 deletions(-) create mode 100644 src/main/scala/xyz/driver/core/app/init.scala diff --git a/src/main/scala/xyz/driver/core/app/init.scala b/src/main/scala/xyz/driver/core/app/init.scala new file mode 100644 index 0000000..36eaeda --- /dev/null +++ b/src/main/scala/xyz/driver/core/app/init.scala @@ -0,0 +1,118 @@ +package xyz.driver.core.app + +import java.nio.file.{Files, Paths} +import java.util.concurrent.{Executor, Executors} + +import akka.actor.ActorSystem +import akka.stream.ActorMaterializer +import com.typesafe.config.{Config, ConfigFactory} +import com.typesafe.scalalogging.Logger +import org.slf4j.LoggerFactory +import xyz.driver.core.logging.MdcExecutionContext +import xyz.driver.core.time.provider.{SystemTimeProvider, TimeProvider} +import xyz.driver.tracing.{GoogleTracer, NoTracer, Tracer} + +import scala.concurrent.ExecutionContext +import scala.util.Try + +object init { + + type RequiredBuildInfo = { + val name: String + val version: String + val gitHeadCommit: scala.Option[String] + } + + case class ApplicationContext(config: Config, time: TimeProvider, log: Logger) + + /** NOTE: This needs to be the first that is run when application starts. + * Otherwise if another command causes the logger to be instantiated, + * it will default to logback.xml, and not honor this configuration + */ + def configureLogging() = { + scala.sys.env.get("JSON_LOGGING") match { + case Some("true") => + System.setProperty("logback.configurationFile", "deployed-logback.xml") + case _ => + System.setProperty("logback.configurationFile", "logback.xml") + } + } + + def getEnvironmentSpecificConfig(): Config = { + scala.sys.env.get("APPLICATION_CONFIG_TYPE") match { + case Some("deployed") => + ConfigFactory.load(this.getClass.getClassLoader, "deployed-application.conf") + case _ => + xyz.driver.core.config.loadDefaultConfig + } + } + + def configureTracer(actorSystem: ActorSystem, applicationContext: ApplicationContext): Tracer = { + + val serviceAccountKeyFile = + Paths.get(applicationContext.config.getString("tracing.google.serviceAccountKeyfile")) + + if (Files.exists(serviceAccountKeyFile)) { + val materializer = ActorMaterializer()(actorSystem) + new GoogleTracer( + projectId = applicationContext.config.getString("tracing.google.projectId"), + serviceAccountFile = serviceAccountKeyFile + )(actorSystem, materializer) + } else { + applicationContext.log.warn(s"Tracing file $serviceAccountKeyFile was not found, using NoTracer!") + NoTracer + } + } + + def serviceActorSystem(serviceName: String, executionContext: ExecutionContext, config: Config) = { + val actorSystem = + ActorSystem(s"$serviceName-actors", Option(config), Option.empty[ClassLoader], Option(executionContext)) + + Runtime.getRuntime.addShutdownHook(new Thread() { + override def run(): Unit = Try(actorSystem.terminate()) + }) + + actorSystem + } + + def toMdcExecutionContext(executor: Executor) = + new MdcExecutionContext(ExecutionContext.fromExecutor(executor)) + + def newFixedMdcExecutionContext(capacity: Int): MdcExecutionContext = + toMdcExecutionContext(Executors.newFixedThreadPool(capacity)) + + def defaultApplicationContext() = { + val config = getEnvironmentSpecificConfig() + + val time = new SystemTimeProvider() + val log = Logger(LoggerFactory.getLogger(classOf[DriverApp])) + + ApplicationContext(config, time, log) + } + + def createDefaultApplication(modules: Seq[Module], + buildInfo: RequiredBuildInfo, + actorSystem: ActorSystem, + tracer: Tracer, + context: ApplicationContext) = { + val scheme = context.config.getString("application.scheme") + val baseUrl = context.config.getString("application.baseUrl") + val port = context.config.getInt("application.port") + + new DriverApp( + buildInfo.name, + buildInfo.version, + buildInfo.gitHeadCommit.getOrElse("None"), + modules = modules, + context.time, + context.log, + context.config, + interface = "::0", + baseUrl, + scheme, + port, + tracer + )(actorSystem, actorSystem.dispatcher) + } + +} diff --git a/src/main/scala/xyz/driver/core/app/module.scala b/src/main/scala/xyz/driver/core/app/module.scala index bbb29f4..a70e97a 100644 --- a/src/main/scala/xyz/driver/core/app/module.scala +++ b/src/main/scala/xyz/driver/core/app/module.scala @@ -3,7 +3,9 @@ package xyz.driver.core.app import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives.complete import akka.http.scaladsl.server.{Route, RouteConcatenation} +import com.typesafe.config.Config import com.typesafe.scalalogging.Logger +import xyz.driver.core.database.Database import xyz.driver.core.rest.{DriverRoute, NoServiceDiscovery, SavingUsedServiceDiscovery, ServiceDiscovery} import scala.reflect.runtime.universe._ @@ -36,6 +38,22 @@ class SimpleModule(override val name: String, theRoute: Route, routeType: Type) override def routeTypes: Seq[Type] = Seq(routeType) } +trait SingleDatabaseModule { self: Module => + + val databaseName: String + val config: Config + + val database = Database.fromConfig(config, databaseName) + + override def deactivate(): Unit = { + try { + database.database.close() + } finally { + self.deactivate() + } + } +} + /** * Module implementation which may be used to compose multiple modules * diff --git a/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala index 9f8db3e..f39f31d 100644 --- a/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala +++ b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala @@ -13,17 +13,14 @@ import scala.concurrent.ExecutionContext class MdcExecutionContext(executionContext: ExecutionContext) extends ExecutionContext { override def execute(runnable: Runnable): Unit = { val callerMdc = MDC.getCopyOfContextMap - executionContext.execute(new Runnable { - def run(): Unit = { - // copy caller thread diagnostic context to execution thread - // scalastyle:off - if (callerMdc != null) MDC.setContextMap(callerMdc) - try { - runnable.run() - } finally { - // the thread might be reused, so we clean up for the next use - MDC.clear() - } + executionContext.execute(() => { + // copy caller thread diagnostic context to execution thread + Option(callerMdc).foreach(MDC.setContextMap) + try { + runnable.run() + } finally { + // the thread might be reused, so we clean up for the next use + MDC.clear() } }) } diff --git a/src/main/scala/xyz/driver/core/swagger.scala b/src/main/scala/xyz/driver/core/swagger.scala index a97e0ac..44ca6e1 100644 --- a/src/main/scala/xyz/driver/core/swagger.scala +++ b/src/main/scala/xyz/driver/core/swagger.scala @@ -15,6 +15,13 @@ import spray.json._ object swagger { + def configureCustomSwaggerModels(customPropertiesExamples: Map[Class[_], Property], + customObjectsExamples: Map[Class[_], JsValue]) = { + ModelConverters + .getInstance() + .addConverter(new CustomSwaggerJsonConverter(Json.mapper(), customPropertiesExamples, customObjectsExamples)) + } + object CustomSwaggerJsonConverter { def stringProperty(pattern: Option[String] = None, example: Option[String] = None): Property = { -- cgit v1.2.3 From 92eb152fd61f381ae39e2887fcec4a564b06b058 Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 8 Nov 2017 04:18:44 -0800 Subject: App initialization convenience methods --- .../xyz/driver/core/logging/MdcExecutionContext.scala | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala index f39f31d..df21b48 100644 --- a/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala +++ b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala @@ -13,14 +13,16 @@ import scala.concurrent.ExecutionContext class MdcExecutionContext(executionContext: ExecutionContext) extends ExecutionContext { override def execute(runnable: Runnable): Unit = { val callerMdc = MDC.getCopyOfContextMap - executionContext.execute(() => { - // copy caller thread diagnostic context to execution thread - Option(callerMdc).foreach(MDC.setContextMap) - try { - runnable.run() - } finally { - // the thread might be reused, so we clean up for the next use - MDC.clear() + executionContext.execute(new Runnable { + def run(): Unit = { + // copy caller thread diagnostic context to execution thread + Option(callerMdc).foreach(MDC.setContextMap) + try { + runnable.run() + } finally { + // the thread might be reused, so we clean up for the next use + MDC.clear() + } } }) } -- cgit v1.2.3 From 9748dcd59d13d729595c1fab4fec548834705ebe Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 8 Nov 2017 11:42:23 -0800 Subject: App initialization convenience methods --- src/main/scala/xyz/driver/core/app/module.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/xyz/driver/core/app/module.scala b/src/main/scala/xyz/driver/core/app/module.scala index a70e97a..7be38eb 100644 --- a/src/main/scala/xyz/driver/core/app/module.scala +++ b/src/main/scala/xyz/driver/core/app/module.scala @@ -43,7 +43,7 @@ trait SingleDatabaseModule { self: Module => val databaseName: String val config: Config - val database = Database.fromConfig(config, databaseName) + def database = Database.fromConfig(config, databaseName) override def deactivate(): Unit = { try { -- cgit v1.2.3