-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
767 additions
and
383 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
46 changes: 46 additions & 0 deletions
46
app/de/innfactory/bootstrapplay2/application/controller/BaseController.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,46 @@ | ||
package de.innfactory.bootstrapplay2.application.controller | ||
|
||
import cats.data.{EitherT, Kleisli} | ||
import de.innfactory.bootstrapplay2.commons.{RequestContext, RequestContextWithUser} | ||
import de.innfactory.bootstrapplay2.commons.application.actions.utils.UserUtils | ||
import de.innfactory.bootstrapplay2.users.domain.interfaces.UserService | ||
import de.innfactory.bootstrapplay2.users.domain.models.UserId | ||
import de.innfactory.play.controller.{ErrorResult, ResultStatus} | ||
import de.innfactory.smithy4play.{ContextRouteError, RoutingContext} | ||
import play.api.Application | ||
import de.innfactory.play.smithy4play.{AbstractBaseController, HttpHeaders, ImplicitLogContext} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class BaseController(implicit ec: ExecutionContext, app: Application) | ||
extends AbstractBaseController[ResultStatus, RequestContextWithUser, RequestContext] | ||
with ImplicitLogContext | ||
with BaseMapper { | ||
|
||
private val userUtils = app.injector.instanceOf[UserUtils] | ||
private val userService = app.injector.instanceOf[UserService] | ||
|
||
override def AuthAction: Kleisli[ApplicationRouteResult, RequestContext, RequestContextWithUser] = Kleisli { | ||
context => | ||
val result = for { | ||
_ <- EitherT(userUtils.validateJwtToken(context.httpHeaders.authAsJwt)) | ||
userId <- EitherT(userUtils.extractUserId(context.httpHeaders.authAsJwt)) | ||
user <- userService.getUserByIdWithoutRequestContext(UserId(userId)) | ||
} yield RequestContextWithUser(context.httpHeaders, context.span, user) | ||
result | ||
} | ||
|
||
override def errorHandler(e: ResultStatus): ContextRouteError = | ||
e match { | ||
case result: ErrorResult => | ||
new ContextRouteError { | ||
override def message: String = result.message | ||
override def additionalInfoToLog: Option[String] = result.additionalInfoToLog | ||
override def additionalInfoErrorCode: Option[String] = result.additionalInfoErrorCode | ||
override def statusCode: Int = result.statusCode | ||
} | ||
} | ||
|
||
override def createRequestContextFromRoutingContext(r: RoutingContext): RequestContext = | ||
new RequestContext(HttpHeaders(r.map)) | ||
} |
33 changes: 33 additions & 0 deletions
33
app/de/innfactory/bootstrapplay2/application/controller/BaseMapper.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,33 @@ | ||
package de.innfactory.bootstrapplay2.application.controller | ||
|
||
import de.innfactory.play.smithy4play.PlayJsonToDocumentMapper | ||
import io.scalaland.chimney.Transformer | ||
import org.joda.time.DateTime | ||
import play.api.libs.json.JsValue | ||
import smithy4s.Document | ||
import de.innfactory.bootstrapplay2.definition.DateWithTime | ||
|
||
import scala.language.implicitConversions | ||
|
||
trait BaseMapper { | ||
|
||
implicit val transformJsValueToDocument: Transformer[JsValue, Document] = PlayJsonToDocumentMapper.mapToDocument | ||
implicit val transformDocumentToJsValue: Transformer[Document, JsValue] = PlayJsonToDocumentMapper.documentToJsValue | ||
|
||
implicit def unitMapper[T](any: T): Unit = () | ||
|
||
implicit def dateWithTimeToDateTime(dateWithTime: DateWithTime): DateTime = | ||
DateTime.parse(dateWithTime.value) | ||
|
||
implicit def dateTimeToDateWithTime(dateTime: DateTime): DateWithTime = | ||
DateWithTime(dateTime.toString()) | ||
|
||
implicit def sequenceTransformer[T, R](seq: Seq[T])(implicit transform: T => R): List[R] = | ||
seq.map(transform).toList | ||
|
||
implicit val dateWithTimeToDateTimeTransformer: Transformer[DateWithTime, DateTime] = | ||
(dateWithTime: DateWithTime) => dateWithTimeToDateTime(dateWithTime) | ||
|
||
implicit val dateTimeToDateWithTimeTransformer: Transformer[DateTime, DateWithTime] = | ||
(dateTime: DateTime) => dateTimeToDateWithTime(dateTime) | ||
} |
65 changes: 26 additions & 39 deletions
65
app/de/innfactory/bootstrapplay2/commons/RequestContext.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,56 +1,43 @@ | ||
package de.innfactory.bootstrapplay2.commons | ||
|
||
import de.innfactory.bootstrapplay2.commons.application.actions.models.RequestWithUser | ||
import de.innfactory.bootstrapplay2.commons.logging.TraceLogger | ||
import de.innfactory.bootstrapplay2.users.domain.models.User | ||
import de.innfactory.play.tracing.TraceRequest | ||
import cats.implicits.toBifunctorOps | ||
import de.innfactory.bootstrapplay2.users.domain.models.{User, UserId} | ||
import de.innfactory.play.smithy4play.{HttpHeaders, TraceContext} | ||
import io.opencensus.trace.Span | ||
import org.joda.time.DateTime | ||
|
||
import scala.util.Try | ||
|
||
abstract class TraceContext { | ||
|
||
def span: Span | ||
|
||
private val traceLogger = new TraceLogger(span) | ||
|
||
final def log: TraceLogger = traceLogger | ||
trait ApplicationTraceContext extends TraceContext { | ||
|
||
def timeOverride: Option[DateTime] = Try( | ||
httpHeaders.getHeader("x-app-datetime").map(DateTime.parse) | ||
).toEither | ||
.leftMap(left => this.log.error("cannot parse x-app-time, because of error " + left.getMessage)) | ||
.toOption | ||
.flatten | ||
} | ||
|
||
trait RequestContextUser[USER] { | ||
def user: USER | ||
class RequestContext( | ||
rhttpHeaders: HttpHeaders, | ||
rSpan: Option[Span] = None | ||
) extends ApplicationTraceContext { | ||
override def httpHeaders: HttpHeaders = rhttpHeaders | ||
override def span: Option[Span] = rSpan | ||
} | ||
|
||
class RequestContext(rcSpan: Span, rcHeaders: Map[String, Seq[String]]) extends TraceContext { | ||
def headers: Map[String, Seq[String]] = rcHeaders | ||
override def span: Span = rcSpan | ||
object RequestContext { | ||
implicit def fromRequestContextWithUser(requestContextWithUser: RequestContextWithUser): RequestContext = | ||
new RequestContext(requestContextWithUser.httpHeaders, requestContextWithUser.span) | ||
|
||
def empty = new RequestContext(HttpHeaders(Map.empty)) | ||
} | ||
|
||
case class RequestContextWithUser( | ||
override val span: Span, | ||
override val headers: Map[String, Seq[String]], | ||
override val httpHeaders: HttpHeaders, | ||
override val span: Option[Span], | ||
user: User | ||
) extends RequestContext(span, headers) | ||
with RequestContextUser[User] {} | ||
) extends RequestContext(httpHeaders, span) | ||
|
||
object RequestContextWithUser { | ||
implicit def toRequestContext(requestContextWithUser: RequestContextWithUser): RequestContext = | ||
new RequestContext(requestContextWithUser.span, requestContextWithUser.headers) | ||
} | ||
|
||
object ReqConverterHelper { | ||
|
||
def requestContext(implicit req: TraceRequest[_]): RequestContext = { | ||
val headers = Try(req.request.headers.toMap).toOption.getOrElse(Map.empty) | ||
new RequestContext(req.traceSpan, headers) | ||
} | ||
|
||
def requestContextWithUser[Q](implicit | ||
req: RequestWithUser[Q] | ||
): RequestContextWithUser = { | ||
val headers = Try(req.request.headers.toMap).toOption.getOrElse(Map.empty) | ||
RequestContextWithUser(req.traceSpan, headers, req.user) | ||
} | ||
|
||
implicit def toUserId(requestContextWithUser: RequestContextWithUser): UserId = requestContextWithUser.user.userId | ||
} |
2 changes: 1 addition & 1 deletion
2
app/de/innfactory/bootstrapplay2/commons/application/actions/TracingAction.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
2 changes: 1 addition & 1 deletion
2
app/de/innfactory/bootstrapplay2/commons/application/actions/TracingUserAction.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
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
2 changes: 1 addition & 1 deletion
2
app/de/innfactory/bootstrapplay2/commons/application/actions/utils/UserUtils.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
2 changes: 1 addition & 1 deletion
2
app/de/innfactory/bootstrapplay2/commons/application/actions/utils/UserUtilsImpl.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
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
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
Oops, something went wrong.