Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/auto router #12

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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