From 5fb1b8bd07ab50703a59b0e87cb3db668d9c6e1e Mon Sep 17 00:00:00 2001 From: Moritz Lintterer Date: Tue, 2 Aug 2022 15:01:02 +0200 Subject: [PATCH 1/3] update build.sbt --- build.sbt | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/build.sbt b/build.sbt index 5b70e429..c9e38a8c 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.91" +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", @@ -25,16 +33,14 @@ val defaultProjectSettings = Seq( val sharedSettings = defaultProjectSettings lazy val play4s = project + .enablePlugins(Smithy4sCodegenPlugin) .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) - From bab177711c154136b16765207b55067d61a723b6 Mon Sep 17 00:00:00 2001 From: Moritz Lintterer Date: Tue, 2 Aug 2022 15:25:18 +0200 Subject: [PATCH 2/3] adding AutoRoutableController trait and AutoRouter --- .../src/main/scala/de/innfactory/smithy4play/AutoRouter.scala | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala 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..e5b524fc --- /dev/null +++ b/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala @@ -0,0 +1,3 @@ +package de.innfactory.smithy4play class AutoRouter { + +} From 932cfecec23c87dd9e98df25c105521497c02f04 Mon Sep 17 00:00:00 2001 From: Moritz Lintterer Date: Tue, 2 Aug 2022 15:27:14 +0200 Subject: [PATCH 3/3] feat: adding AutoRouter with AutoRoutableController --- build.sbt | 3 +- .../innfactory/smithy4play/AutoRouter.scala | 33 +++++++++++++++++-- .../de/innfactory/smithy4play/package.scala | 21 ++++++++++-- project/Dependencies.scala | 4 +++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/build.sbt b/build.sbt index c9e38a8c..c17bac3f 100644 --- a/build.sbt +++ b/build.sbt @@ -2,7 +2,7 @@ import sbt.Compile import java.io.File -val releaseVersion = "0.1.91" +val releaseVersion = "0.1.88" name := "smithy4play" val token = sys.env.getOrElse("GITHUB_TOKEN", "") @@ -33,7 +33,6 @@ val defaultProjectSettings = Seq( val sharedSettings = defaultProjectSettings lazy val play4s = project - .enablePlugins(Smithy4sCodegenPlugin) .in(file("play4s")) .settings( sharedSettings, diff --git a/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala b/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala index e5b524fc..4f65fe76 100644 --- a/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala +++ b/play4s/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala @@ -1,3 +1,32 @@ -package de.innfactory.smithy4play class AutoRouter { +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