diff --git a/build.sbt b/build.sbt index 5b70e429..c17bac3f 100644 --- a/build.sbt +++ b/build.sbt @@ -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", @@ -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) - diff --git a/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala b/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala new file mode 100644 index 00000000..4f65fe76 --- /dev/null +++ b/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala @@ -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 + } + } + +} \ No newline at end of file diff --git a/play4s/src/main/scala/de/innfactory/smithy4play/package.scala b/play4s/src/main/scala/de/innfactory/smithy4play/package.scala index 332ecf63..42f952c9 100644 --- a/play4s/src/main/scala/de/innfactory/smithy4play/package.scala +++ b/play4s/src/main/scala/de/innfactory/smithy4play/package.scala @@ -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 { @@ -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 + + } + } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 13c19411..7d498868 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,3 +1,4 @@ +import sbt.Keys.libraryDependencies import sbt._ object Dependencies { @@ -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 = @@ -16,6 +19,7 @@ object Dependencies { lazy val list = Seq( smithyCore, smithyJson, + reflections, scalatestPlus, typesafePlay, cats