aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/com/drivergrp/core/DriverApp.scala
diff options
context:
space:
mode:
authorvlad <vlad@drivergrp.com>2016-07-16 02:43:54 -0400
committervlad <vlad@drivergrp.com>2016-07-16 02:43:54 -0400
commit980deaf70e4e0ba906d0af534aebc839015f0581 (patch)
tree41730ad413bd7581d0ab54429930e83c48f97fe6 /src/main/scala/com/drivergrp/core/DriverApp.scala
parentc0d574dc6134e4f406875ea5a1301ba46602a6ec (diff)
downloaddriver-core-980deaf70e4e0ba906d0af534aebc839015f0581.tar.gz
driver-core-980deaf70e4e0ba906d0af534aebc839015f0581.tar.bz2
driver-core-980deaf70e4e0ba906d0af534aebc839015f0581.zip
Dependency injection through constructor parameters + bug fixes
Diffstat (limited to 'src/main/scala/com/drivergrp/core/DriverApp.scala')
-rw-r--r--src/main/scala/com/drivergrp/core/DriverApp.scala55
1 files changed, 28 insertions, 27 deletions
diff --git a/src/main/scala/com/drivergrp/core/DriverApp.scala b/src/main/scala/com/drivergrp/core/DriverApp.scala
index 01fee03..7b991d1 100644
--- a/src/main/scala/com/drivergrp/core/DriverApp.scala
+++ b/src/main/scala/com/drivergrp/core/DriverApp.scala
@@ -5,41 +5,42 @@ import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.RouteResult._
import akka.stream.ActorMaterializer
-import com.drivergrp.core.config.ConfigModule
-import com.drivergrp.core.logging.LoggerModule
+import com.drivergrp.core.logging.{Logger, TypesafeScalaLogger}
+import com.typesafe.config.Config
+import org.slf4j.LoggerFactory
-trait DriverApp {
- this: ConfigModule with LoggerModule =>
+class DriverApp(services: Seq[Service],
+ log: Logger = new TypesafeScalaLogger(
+ com.typesafe.scalalogging.Logger(LoggerFactory.getLogger(classOf[DriverApp]))),
+ config: Config = com.drivergrp.core.config.loadDefaultConfig,
+ interface: String = "localhost", port: Int = 8080) {
- def interface: String = "localhost"
- def port: Int = 8080
-
- def services: Seq[Service]
-
-
- val servicesInstances = services
- activateServices(servicesInstances)
- scheduleServicesDeactivation(servicesInstances)
-
- implicit val actorSystem = ActorSystem("spray-routing", config)
- implicit val executionContext = actorSystem.dispatcher
- implicit val materializer = ActorMaterializer()(actorSystem)
+ def run() = {
+ activateServices(services)
+ scheduleServicesDeactivation(services)
+ bindHttp(services)
+ }
- val serviceTypes = servicesInstances.flatMap(_.serviceTypes)
- val swaggerService = new Swagger(actorSystem, serviceTypes, config)
- val swaggerRoutes = swaggerService.routes ~ swaggerService.swaggerUI
+ protected def bindHttp(services: Seq[Service]) {
+ implicit val actorSystem = ActorSystem("spray-routing", config)
+ implicit val executionContext = actorSystem.dispatcher
+ implicit val materializer = ActorMaterializer()(actorSystem)
- Http()(actorSystem).bindAndHandle(
- route2HandlerFlow(logRequestResult("log")(servicesInstances.map(_.route).foldLeft(swaggerRoutes) { _ ~ _ })),
- interface, port)(materializer)
+ val serviceTypes = services.flatMap(_.serviceTypes)
+ val swaggerService = new Swagger(actorSystem, serviceTypes, config)
+ val swaggerRoutes = swaggerService.routes ~ swaggerService.swaggerUI
+ Http()(actorSystem).bindAndHandle(
+ route2HandlerFlow(logRequestResult("log")(services.map(_.route).foldLeft(swaggerRoutes) { _ ~ _ })),
+ interface, port)(materializer)
+ }
/**
* Initializes services
*/
- protected def activateServices(servicesInstances: Seq[Service]) = {
- servicesInstances.foreach { service =>
+ protected def activateServices(services: Seq[Service]) = {
+ services.foreach { service =>
Console.print(s"Service ${service.name} starts ...")
try {
service.activate()
@@ -55,10 +56,10 @@ trait DriverApp {
/**
* Schedules services to be deactivated on the app shutdown
*/
- protected def scheduleServicesDeactivation(servicesInstances: Seq[Service]) = {
+ protected def scheduleServicesDeactivation(services: Seq[Service]) = {
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run(): Unit = {
- servicesInstances.foreach { service =>
+ services.foreach { service =>
Console.print(s"Service ${service.name} shutting down ...")
try {
service.deactivate()