播放框架:动态添加子项目的路由 [英] Play framework: Add routes from sub-projects dynamically
问题描述
我正在开发一个在编译时发现子项目的系统.这行得通.请参见此处.现在唯一的问题是子项目的路由文件被忽略了.
I'm developing a system to discover subprojects at compile time. This works. See here. Now the only issue is that the subproject's route file is being ignored.
我知道在主路由文件中包含路由文件的通常方法是将后者硬编码为前者.但这违背了我动态子项目的目标.
I know that the normal way to include a route file in the main route file is by hardcoding the latter into the former. But that would defy my goal of dynamic subprojects.
我敢打赌,在Build.scala中,有一种方法可以发现路由文件并将其附加到主路由文件中.但是我是一个初学者,我不知道该怎么做.你能帮我吗?
I bet that there's a way to, in Build.scala, discover a route file and append it to the main route file. But I'm a beginner and I have no idea how to do it. Could you please help me out?
或者,如果没有办法在编译时执行它,也许有一种在运行时加载它的方法?我知道有一个 api可以拦截请求.因此,如果我们可以读取路由,则可以通过这种方式实现动态路由.那是个好主意吗?
Alternatively, if there's no way to do it at compile time, maybe there's a way to load it at runtime? I know there's an api to intercept requests. So if we can read the routes we could implement dynamic routing that way. Is that a good idea?
推荐答案
最后,我不得不编写路由文件的片段(每个子项目一个,使用诸如subproject.routes之类的不同扩展名),然后将它们全部串联到一个路由文件中.您还必须对application.conf文件执行此操作.
In the end i had to write fragments of the routing file (one per each sub-project, use a different extension like subproject.routes for example) and then concatenate them all together into a single routes file. You have to do this also for the application.conf file.
我是通过Build.sbt脚本完成的:
I did this via the Build.sbt script:
import sbt._
import Keys._
import play._
import java.io._
object Build extends Build {
val commonSettings: Seq[Setting[_]] = Seq(
scalaVersion := "2.11.1"
)
IO.copyFile(file("conf/base.routes"), file("conf/routes"))
IO.copyFile(file("conf/base.application.conf"), file("conf/application.conf"))
lazy val libFolder = file("base-lib");
lazy val baseLib = processModule(libFolder)
lazy val modules = (file("modules") * DirectoryFilter).get.map { dir =>
processModule(dir).dependsOn(baseLib)
}
lazy val root = (project in file("."))
.enablePlugins(PlayJava)
.settings(
name := "mainProject",
version := "1.0"
)
.dependsOn(modules map (m => m: ClasspathDependency): _*)
.aggregate(modules map (m => m: ProjectReference): _*)
override lazy val projects = root +: modules +: dspcloudLib
def processModule(dir: File):Project = {
val p = Project(dir.getName, dir).enablePlugins(PlayJava).settings(commonSettings: _*)
val mf = new File(dir, "conf/" + dir.getName + ".r")
val r = IO.read(mf)
IO.append(file("conf/routes"), r.toString)
val cf = new File(dir, "conf/" + dir.getName + ".application.conf")
val c = IO.read(cf)
IO.append(file("conf/application.conf"), c.toString)
p
}
}
这篇关于播放框架:动态添加子项目的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!