-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from innFactory/feature/AutoRouter
Feature/auto router
- Loading branch information
Showing
4 changed files
with
75 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package de.innfactory.smithy4play | ||
|
||
import org.reflections.Reflections | ||
import play.api.Application | ||
import play.api.mvc.ControllerComponents | ||
import play.api.routing.Router.Routes | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import scala.concurrent.ExecutionContext | ||
import scala.jdk.CollectionConverters.CollectionHasAsScala | ||
|
||
@Singleton | ||
class AutoRouter @Inject( | ||
)(implicit | ||
cc: ControllerComponents, | ||
app: Application, | ||
ec: ExecutionContext | ||
) extends BaseRouter { | ||
val reflection = new Reflections(); | ||
|
||
override val controllers: Seq[Routes] = { | ||
reflection.getSubTypesOf(classOf[AutoRoutableController]).asScala.map( | ||
clazz => createFromClass(clazz)).toSeq | ||
} | ||
|
||
def createFromClass(clazz: Class[_]): Routes = { | ||
app.injector.instanceOf(clazz) match { | ||
case x: AutoRoutableController => x.routes | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters