Skip to content

Commit

Permalink
Merge pull request #12 from innFactory/feature/AutoRouter
Browse files Browse the repository at this point in the history
Feature/auto router
  • Loading branch information
MoeQuadrat authored Aug 2, 2022
2 parents a480051 + 932cfec commit 0dc93dd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
35 changes: 20 additions & 15 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
val releaseVersion = "0.1.22"
name:= "smithy4play"
import sbt.Compile

import java.io.File

val releaseVersion = "0.1.88"
name := "smithy4play"

val token = sys.env.getOrElse("GITHUB_TOKEN", "")
val githubSettings = Seq(
githubOwner := "innFactory",
githubRepository := "de.innfactory.smithy4play",
githubRepository := "smithy4play",
githubTokenSource := TokenSource.GitConfig("github.token") || TokenSource.Environment("GITHUB_TOKEN"),
githubTokenSource := TokenSource.GitConfig("github.token") || TokenSource
.Environment("GITHUB_TOKEN"),
credentials :=
Seq(Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
"innFactory",
token
))
Seq(
Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
"innFactory",
token
)
)
)

val defaultProjectSettings = Seq(
scalaVersion := "2.13.8",
organization := "de.innfactory.smithy4play",
Expand All @@ -27,14 +35,11 @@ val sharedSettings = defaultProjectSettings
lazy val play4s = project
.in(file("play4s"))
.settings(
sharedSettings

sharedSettings,
Compile / resourceDirectory := (ThisBuild / baseDirectory).value / "play4s" / "src" / "main" / "scala" / "de" / "innfactory" / "smithy4play" / "resources"
)
.settings(
scalaVersion := Dependencies.scalaVersion,
name := "play4s",
name := "smithy4play",
libraryDependencies ++= Dependencies.list
)

lazy val root = project.in(file(".")).settings(sharedSettings).dependsOn(play4s).aggregate(play4s)

32 changes: 32 additions & 0 deletions play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala
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
}
}

}
21 changes: 19 additions & 2 deletions play4s/src/main/scala/de/innfactory/smithy4play/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package de.innfactory
import cats.data.{EitherT, Kleisli}
import org.slf4j
import play.api.Logger
import play.api.mvc.RequestHeader
import play.api.mvc.{ControllerComponents, RequestHeader}
import play.api.routing.Router.Routes
import smithy4s.Monadic
import smithy4s.http.{CaseInsensitive, HttpEndpoint, PathSegment, matchPath}

import scala.concurrent.Future
import scala.concurrent.{ExecutionContext, Future}

package object smithy4play {

Expand All @@ -29,4 +31,19 @@ package object smithy4play {
ep.matches(x.path.replaceFirst("/", "").split("/").filter(_.nonEmpty))
}

trait AutoRoutableController {
implicit def transformToRouter[Alg[_[_, _, _, _, _]], Op[_, _, _, _, _], F[
_
] <: ContextRoute[_]](
impl: Monadic[Alg, F]
)(implicit serviceProvider: smithy4s.Service.Provider[Alg, Op], ec: ExecutionContext, cc: ControllerComponents): Routes = {
new SmithyPlayRouter[Alg, Op, F](impl).routes()
}



val routes: Routes

}

}
4 changes: 4 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sbt.Keys.libraryDependencies
import sbt._

object Dependencies {
Expand All @@ -7,6 +8,8 @@ object Dependencies {

val scalaVersion = "2.13.8"


val reflections = "org.reflections" % "reflections" % "0.10.2"
val smithyCore = "com.disneystreaming.smithy4s" %% "smithy4s-core" % "0.14.2"
val smithyJson = "com.disneystreaming.smithy4s" %% "smithy4s-json" % "0.14.2"
val scalatestPlus =
Expand All @@ -16,6 +19,7 @@ object Dependencies {
lazy val list = Seq(
smithyCore,
smithyJson,
reflections,
scalatestPlus,
typesafePlay,
cats
Expand Down

0 comments on commit 0dc93dd

Please sign in to comment.