-
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 #153 from innFactory/feat/middleware
Feat/middleware
- Loading branch information
Showing
20 changed files
with
294 additions
and
101 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
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
22 changes: 15 additions & 7 deletions
22
smithy4play/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 |
---|---|---|
@@ -1,37 +1,45 @@ | ||
package de.innfactory.smithy4play | ||
|
||
import com.typesafe.config.Config | ||
import de.innfactory.smithy4play.middleware.{ MiddlewareBase, MiddlewareRegistryBase, ValidateAuthMiddleware } | ||
import io.github.classgraph.{ ClassGraph, ScanResult } | ||
import play.api.Application | ||
import play.api.mvc.ControllerComponents | ||
import play.api.routing.Router.Routes | ||
|
||
import javax.inject.{ Inject, Singleton } | ||
import java.util.Optional | ||
import javax.inject.{ Inject, Provider, Singleton } | ||
import scala.concurrent.ExecutionContext | ||
import scala.jdk.CollectionConverters.CollectionHasAsScala | ||
import scala.util.Try | ||
|
||
@Singleton | ||
class AutoRouter @Inject( | ||
) (implicit | ||
) (validateAuthMiddleware: ValidateAuthMiddleware)(implicit | ||
cc: ControllerComponents, | ||
app: Application, | ||
ec: ExecutionContext, | ||
config: Config | ||
) extends BaseRouter { | ||
|
||
private val pkg = config.getString("smithy4play.autoRoutePackage") | ||
|
||
override val controllers: Seq[Routes] = { | ||
val pkg = config.getString("smithy4play.autoRoutePackage") | ||
val classGraphScanner: ScanResult = new ClassGraph().enableAllInfo().acceptPackages(pkg).scan() | ||
val controllers = classGraphScanner.getClassesImplementing(classOf[AutoRoutableController]) | ||
logger.debug(s"[AutoRouter] found ${controllers.size().toString} Controllers") | ||
val routes = controllers.asScala.map(_.loadClass(true)).map(clazz => createFromClass(clazz)).toSeq | ||
val middlewares = Try { | ||
app.injector.instanceOf[MiddlewareRegistryBase].middlewares | ||
}.toOption.getOrElse(Seq(validateAuthMiddleware)) | ||
logger.debug(s"[AutoRouter] found ${controllers.size().toString} controllers") | ||
logger.debug(s"[AutoRouter] found ${middlewares.size.toString} middlewares") | ||
val routes = controllers.asScala.map(_.loadClass(true)).map(clazz => createFromClass(clazz, middlewares)).toSeq | ||
classGraphScanner.close() | ||
routes | ||
} | ||
|
||
def createFromClass(clazz: Class[_]): Routes = | ||
private def createFromClass(clazz: Class[_], middlewares: Seq[MiddlewareBase]): Routes = | ||
app.injector.instanceOf(clazz) match { | ||
case c: AutoRoutableController => c.routes | ||
case c: AutoRoutableController => c.router(middlewares) | ||
} | ||
|
||
} |
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
24 changes: 20 additions & 4 deletions
24
smithy4play/src/main/scala/de/innfactory/smithy4play/RoutingContext.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 |
---|---|---|
@@ -1,10 +1,26 @@ | ||
package de.innfactory.smithy4play | ||
|
||
import play.api.mvc.{ RawBuffer, Request } | ||
import play.api.mvc.{ RawBuffer, Request, RequestHeader } | ||
import smithy4s.{ Hints, ShapeTag } | ||
|
||
case class RoutingContext(map: Map[String, Seq[String]]) | ||
case class RoutingContext( | ||
headers: Map[String, Seq[String]], | ||
serviceHints: Hints, | ||
endpointHints: Hints, | ||
attributes: Map[String, Any], | ||
requestHeader: RequestHeader | ||
) { | ||
def hasHints(s: ShapeTag.Companion[_]): Boolean = hasEndpointHints(s) || hasServiceHints(s) | ||
def hasServiceHints(s: ShapeTag.Companion[_]): Boolean = serviceHints.has(s.tagInstance) | ||
def hasEndpointHints(s: ShapeTag.Companion[_]): Boolean = endpointHints.has(s.tagInstance) | ||
} | ||
|
||
object RoutingContext { | ||
def fromRequest(request: Request[RawBuffer]): RoutingContext = | ||
RoutingContext(request.headers.toMap) | ||
def fromRequest( | ||
request: Request[RawBuffer], | ||
sHints: Hints, | ||
eHints: Hints, | ||
requestHeader: RequestHeader | ||
): RoutingContext = | ||
RoutingContext(request.headers.toMap, sHints, eHints, Map.empty, requestHeader) | ||
} |
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
26 changes: 26 additions & 0 deletions
26
smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/MiddlewareBase.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,26 @@ | ||
package de.innfactory.smithy4play.middleware | ||
|
||
import cats.data.Kleisli | ||
import de.innfactory.smithy4play.{ EndpointResult, RouteResult, RoutingContext } | ||
import play.api.Logger | ||
|
||
trait MiddlewareBase { | ||
|
||
val logger: Logger = Logger("smithy4play") | ||
|
||
protected def logic( | ||
r: RoutingContext, | ||
next: RoutingContext => RouteResult[EndpointResult] | ||
): RouteResult[EndpointResult] | ||
|
||
protected def skipMiddleware(r: RoutingContext): Boolean = false | ||
|
||
def middleware( | ||
f: RoutingContext => RouteResult[EndpointResult] | ||
): Kleisli[RouteResult, RoutingContext, EndpointResult] = | ||
Kleisli { r => | ||
if (skipMiddleware(r)) f(r) | ||
else logic(r, f) | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/MiddlewareRegistryBase.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,7 @@ | ||
package de.innfactory.smithy4play.middleware | ||
|
||
trait MiddlewareRegistryBase { | ||
|
||
val middlewares: Seq[MiddlewareBase] | ||
|
||
} |
Oops, something went wrong.