2015-11-06 83 views
1

我有以下的多模塊項目結構使用SBT內置:噴霧REST API應用程序結構 - 建議所需

myProject-api 
myProject-core 

myProject的核心的組織結構如下:

它包含作爲行爲的某些行爲給我的服務一個門面。例如,我有一個位於UserService前面的UserActor。位於NotificationService之前的NotificationActor等。

我還有一個特質暴露有演員誰是感興趣:

trait MyProjectCoreActors { 

    def myAppCfg = MyProjConfig.appCfg  

    def userActor = myAppCfg.userActor 
    def notifyActor = myAppCfg.notifyActor 
} 
object MyProjectCoreActors { 

    ... some initialization routing that initializes the MyProjConfig 
} 

因此,我國UserActor被定義爲:

class UserActor(service: UserService) extends Actor { 

    ... 
    ... 
} 

我UserService如下:

class UserService(dbConfig: DbConfig) { 

    ... 
    ... 
} 

我有另一個名爲MyProjectConfig的類,我使用application.co初始化nf文件。在這個文件中,我有連接細節到數據庫等等。定義見下文

trait MyProjectConfig { 

    def actorSystem: ActorSystem 

    // the actors 
    def userActor: ActorRef 
} 
object MyProjectConfig { 

    def apply(appConfig: Config, system: ActorSystem): MyProjectConfig = { 

    new MyProjectConfig { 
     private val dbConfig = loadDBConfig(appConfig) 

     override val actorSystem = system 

     // each actor gets its own DBConfigInstance instance 
     override val userActor = 
     actorSystem.actorOf(
      Props(new UserActor(UserService(dbConfig))) 
     ) 
    } 
    } 
} 

我現在有噴霧路由:

trait MyProjectService extends HttpService with MyProjectCoreActors { 

    def pingRoute = path("ping") { 
    get { 
     userActor ! "newUser" 
     complete("pong!") 
    } 
    } 

    def pongRoute = path("pong") { 
    get { complete("pong!?") } 
    } 

    def route = pingRoute ~ pongRoute 
} 

什麼是現在缺少的是一個方法調用MyProjectConfig.apply(....)的MyProjectConfig被如下初始化方法並傳入Actor系統和底層application.conf!

這最初是一個基於Play的應用程序,我有一個Lifecycle插件,可以從中獲取配置和演員系統的底層應用程序。我現在怎麼能和Spray一樣呢?

我有一個啓動類,看起來像這樣:

object MyBootHttpService extends App { 

    implicit val actorSystem = ActorSystem("myproj-actor-system") 

} 

我怎麼能忽略這ActorSytem到MyProjectConfig.apply(....)?並從哪裏可以得到application.conf?

回答

0

我認爲你可以在你的MyBootHttpService類中做這樣的事情(DI)。

例如

object MyBootHttpService extends App { 

    implicit val actorSystem = ActorSystem("myproj-actor-system") 
    private val config = ConfigFactory.load 
    private val myAppConfig = MyProjectConfig(config, actorSystem) 

    // Initialise classes that depend on config and actorsystem.... 
    private val service = new MyProjectService with HttpServiceActor { 
    override implicit val actorRefFactory = actorSystem 
    } 
    // Bind our service 
    IO(Http) ? Bind(listener = service, interface = "0.0.0.0", port = config.getInt("port")) 

} 

類型安全配置庫對象ConfigFactory通常用於加載配置文件。沒有參數的ConfigFactory.load將嘗試從類路徑加載application.conf