Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNico committed Jan 17, 2024
1 parent 8b61f9c commit b23fb41
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 71 deletions.
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ lazy val smithy4play = project
),
Compile / smithy4sOutputDir := (ThisBuild / baseDirectory).value / "smithy4play" / "target" / "scala-3.3.1" / "src_managed" / "main",
name := "smithy4play",
scalacOptions += "-Ymacro-annotations",
Compile / compile / wartremoverWarnings ++= Warts.unsafe,
libraryDependencies ++= Dependencies.list
)
Expand All @@ -55,7 +54,6 @@ lazy val smithy4playTest = project
sharedSettings,
scalaVersion := Dependencies.scalaVersion,
name := "smithy4playTest",
scalacOptions += "-Ymacro-annotations",
Compile / compile / wartremoverWarnings ++= Warts.unsafe,
cleanKeepFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app",
cleanFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app" / "testDefinitions" / "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import scala.concurrent.ExecutionContext

trait AutoRoutableController {

implicit def transformToRouter[Alg[_[_, _, _, _, _]], F[
_
] <: ContextRoute[_]](
implicit def transformToRouter[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_]](
impl: FunctorAlgebra[Alg, F]
)(implicit
service: smithy4s.Service[Alg],
Expand All @@ -21,5 +19,5 @@ trait AutoRoutableController {
new SmithyPlayRouter[Alg, F](impl, service).routes(middlewares)

val router: Seq[MiddlewareBase] => Routes

}

Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package de.innfactory.smithy4play

import com.typesafe.config.Config
import de.innfactory.smithy4play.middleware.{ MiddlewareBase, MiddlewareRegistryBase, ValidateAuthMiddleware }
import io.github.classgraph.{ ClassGraph, ScanResult }
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 java.util.Optional
import javax.inject.{ Inject, Provider, Singleton }
import javax.inject.{Inject, Singleton}
import scala.annotation.{Annotation, StaticAnnotation}
import scala.concurrent.ExecutionContext
import scala.jdk.CollectionConverters.CollectionHasAsScala
import scala.util.Try



@Singleton
class AutoRouter @Inject(
) (validateAuthMiddleware: ValidateAuthMiddleware)(implicit
Expand Down Expand Up @@ -42,4 +44,4 @@ class AutoRouter @Inject(
case c: AutoRoutableController => c.router(middlewares)
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.innfactory.smithy4play.middleware

import cats.data.Kleisli
import de.innfactory.smithy4play.{RouteResult, RoutingContext}
import de.innfactory.smithy4play.{EndpointResult, RouteResult, RoutingContext}
import play.api.Logger

trait MiddlewareBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package de.innfactory.smithy4play.middleware

import cats.data.EitherT
import de.innfactory.smithy4play
import de.innfactory.smithy4play.{ EndpointResult, RouteResult, RoutingContext, Smithy4PlayError }
import smithy.api.{ Auth, HttpBearerAuth }
import de.innfactory.smithy4play.{EndpointResult, RouteResult, RoutingContext, Smithy4PlayError}
import smithy.api.{Auth, HttpBearerAuth}

import javax.inject.{ Inject, Singleton }
import scala.concurrent.{ ExecutionContext, Future }
import smithy.api.AuthTraitReference.asBijection
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class ValidateAuthMiddleware @Inject() (implicit
executionContext: ExecutionContext
) extends MiddlewareBase {

override protected def skipMiddleware(r: RoutingContext): Boolean = {
val serviceAuthHints =
r.serviceHints.get(HttpBearerAuth.tagInstance).flatMap(x => HttpBearerAuth.hints.get(Auth.tag))
r.serviceHints.get(HttpBearerAuth.tagInstance).map(_ => Auth(Set(smithy.api.AuthTraitReference("smithy.api#httpBearerAuth"))))
for {
authSet <- r.endpointHints.get(Auth.tag) orElse serviceAuthHints
_ <- authSet.value.find(_.value == HttpBearerAuth.id.show)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package de.innfactory.smithy4play

import cats.data.{EitherT, Kleisli}
import de.innfactory.smithy4play.client.SmithyPlayClientEndpointErrorResponse
import de.innfactory.smithy4play.client.{SmithyPlayClientEndpointErrorResponse, SmithyPlayClientEndpointResponse}
import org.slf4j
import play.api.Logger
import play.api.libs.json.{JsValue, Json, OFormat}
import play.api.mvc.{Headers, RequestHeader}
import smithy4s.http.{CaseInsensitive, HttpEndpoint}

import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.concurrent.Future
import scala.language.experimental.macros

Expand Down Expand Up @@ -72,13 +71,6 @@ private[smithy4play] def matchRequestPath(
): Option[Map[String, String]] =
ep.matches(x.path.replaceFirst("/", "").split("/").filter(_.nonEmpty))

@compileTimeOnly(
"Macro failed to expand. \"Add: scalacOptions += \"-Ymacro-annotations\"\" to project settings"
)
class AutoRouting extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro AutoRoutingMacro.impl
}

private[smithy4play] trait Showable {
this: Product =>
override def toString: String = this.show
Expand Down
16 changes: 8 additions & 8 deletions smithy4playTest/app/controller/TestController.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package controller

import cats.data.{ EitherT, Kleisli }
import cats.data.{EitherT, Kleisli}
import controller.models.TestError
import de.innfactory.smithy4play.{ AutoRouting, ContextRoute, ContextRouteError }
import de.innfactory.smithy4play.{AutoRoutableController, ContextRoute, ContextRouteError}
import play.api.mvc.ControllerComponents
import smithy4s.ByteArray
import testDefinitions.test._
import testDefinitions.test.*

import javax.inject.{ Inject, Singleton }
import scala.concurrent.{ ExecutionContext, Future }
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

@Singleton
@AutoRouting
class TestController @Inject() (implicit
cc: ControllerComponents,
executionContext: ExecutionContext
) extends TestControllerService[ContextRoute] {
executionContext: ExecutionContext,
) extends TestControllerService[ContextRoute] with AutoRoutableController {
override val router = this

override def test(): ContextRoute[SimpleTestResponse] = Kleisli { rc =>
rc.attributes.get("Not") match {
Expand Down
8 changes: 4 additions & 4 deletions smithy4playTest/app/controller/models/TestError.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package controller.models

import de.innfactory.smithy4play.{ ContextRouteError, Status }
import play.api.libs.json.{ JsValue, Json }
import de.innfactory.smithy4play.{ContextRouteError, Status}
import play.api.libs.json.{Format, JsValue, Json}

case class TestError(
message: String,
status: Status = Status(Map.empty, 500)
) extends ContextRouteError {
override def toJson: JsValue = Json.toJson(this)(TestError.format)
override def toJson: JsValue = Json.toJson(this)
override def addHeaders(headers: Map[String, String]): TestError = this.copy(
status = status.copy(
headers = status.headers ++ headers
Expand All @@ -16,5 +16,5 @@ case class TestError(
}

object TestError {
implicit val format = Json.format[TestError]
given Format[TestError] = Json.format[TestError]
}
6 changes: 3 additions & 3 deletions smithy4playTest/test/TestControllerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.scalatestplus.play.{ BaseOneAppPerSuite, FakeApplicationFactory, Play
import play.api.Application
import play.api.Play.materializer
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{ Json, OWrites }
import play.api.libs.json.{ Json, Writes }
import play.api.mvc.{ AnyContentAsEmpty, Result }
import play.api.test.FakeRequest
import play.api.test.Helpers._
Expand Down Expand Up @@ -99,7 +99,7 @@ class TestControllerTest extends PlaySpec with BaseOneAppPerSuite with FakeAppli
val testQuery = "thisIsATestQuery"
val testHeader = "thisIsATestHeader"
val body = TestRequestBody("thisIsARequestBody")
implicit val format: OWrites[TestRequestBody] = Json.writes[TestRequestBody]
given Writes[TestRequestBody] = Json.writes[TestRequestBody]
val future: Future[Result] =
route(
app,
Expand All @@ -113,7 +113,7 @@ class TestControllerTest extends PlaySpec with BaseOneAppPerSuite with FakeAppli

"route to Query Endpoint but should return error because query is not set" in {
val testQuery = "thisIsATestQuery"
implicit val format: OWrites[TestRequestBody] = Json.writes[TestRequestBody]
given Writes[TestRequestBody] = Json.writes[TestRequestBody]
val future: Future[Result] =
route(
app,
Expand Down
5 changes: 3 additions & 2 deletions smithy4playTest/test/models/TestJson.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package models

import play.api.libs.json.Json
import play.api.libs.json.{Json, Reads, Writes}

case class TestJson(message: Option[String])

object TestJson {
implicit val format = Json.format[TestJson]
given Writes[TestJson] = Json.format[TestJson]
given Reads[TestJson] = Json.reads[TestJson]
}

0 comments on commit b23fb41

Please sign in to comment.