aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/com/drivergrp/core/DriverApp.scala
blob: 01fee031bb88c9b64cd58edec323caf8bb3c61a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.drivergrp.core

import akka.actor.ActorSystem
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


trait DriverApp {
  this: ConfigModule with LoggerModule =>

  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)

  val serviceTypes = servicesInstances.flatMap(_.serviceTypes)
  val swaggerService = new Swagger(actorSystem, serviceTypes, config)
  val swaggerRoutes = swaggerService.routes ~ swaggerService.swaggerUI

  Http()(actorSystem).bindAndHandle(
    route2HandlerFlow(logRequestResult("log")(servicesInstances.map(_.route).foldLeft(swaggerRoutes) { _ ~ _ })),
    interface, port)(materializer)


  /**
    * Initializes services
    */
  protected def activateServices(servicesInstances: Seq[Service]) = {
    servicesInstances.foreach { service =>
      Console.print(s"Service ${service.name} starts ...")
      try {
        service.activate()
      } catch {
        case t: Throwable =>
          log.fatal(s"Service ${service.name} failed to activate", t)
          Console.print(" Failed! (check log)")
      }
      Console.println(" Done")
    }
  }

  /**
    * Schedules services to be deactivated on the app shutdown
    */
  protected def scheduleServicesDeactivation(servicesInstances: Seq[Service]) = {
    Runtime.getRuntime.addShutdownHook(new Thread() {
      override def run(): Unit = {
        servicesInstances.foreach { service =>
          Console.print(s"Service ${service.name} shutting down ...")
          try {
            service.deactivate()
          } catch {
            case t: Throwable =>
              log.fatal(s"Service ${service.name} failed to deactivate", t)
              Console.print(" Failed! (check log)")
          }
          Console.println(" Done")
        }
      }
    })
  }
}