aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVlad Uspensky <v.uspenskiy@icloud.com>2017-11-08 22:00:49 -0800
committerGitHub <noreply@github.com>2017-11-08 22:00:49 -0800
commita996353504ffb50d352f8a0fc69681333ef695b4 (patch)
treee9b487a71cfcc17e22f3c1843a41347d0a638a2d
parentcd9ca017a1eb246cd0a5227fd984fa188ecd36d4 (diff)
parent9748dcd59d13d729595c1fab4fec548834705ebe (diff)
downloaddriver-core-a996353504ffb50d352f8a0fc69681333ef695b4.tar.gz
driver-core-a996353504ffb50d352f8a0fc69681333ef695b4.tar.bz2
driver-core-a996353504ffb50d352f8a0fc69681333ef695b4.zip
Merge pull request #89 from drivergroup/initv1.6.5v1.6.4v1.6.3
App initialization convenience methods
-rw-r--r--src/main/scala/xyz/driver/core/app/init.scala118
-rw-r--r--src/main/scala/xyz/driver/core/app/module.scala18
-rw-r--r--src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala3
-rw-r--r--src/main/scala/xyz/driver/core/swagger.scala7
4 files changed, 144 insertions, 2 deletions
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..7be38eb 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
+
+ def 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..df21b48 100644
--- a/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala
+++ b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala
@@ -16,8 +16,7 @@ class MdcExecutionContext(executionContext: ExecutionContext) extends ExecutionC
executionContext.execute(new Runnable {
def run(): Unit = {
// copy caller thread diagnostic context to execution thread
- // scalastyle:off
- if (callerMdc != null) MDC.setContextMap(callerMdc)
+ Option(callerMdc).foreach(MDC.setContextMap)
try {
runnable.run()
} finally {
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 = {