Skip to content

Commit

Permalink
Lower impact on routes
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski committed Oct 6, 2023
1 parent ca96550 commit 7f56d21
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,13 @@ final class ResourcesRoutes(
// Fetch a resource original source
(pathPrefix("source") & get & pathEndOrSingleSlash & idSegmentRef(id) & varyAcceptHeaders) { id =>
authorizeFor(ref, Read).apply {
(parameter("annotate".as[Boolean].withDefault(false))) { annotate =>
parameter("annotate".as[Boolean].withDefault(false)) { annotate =>
implicit val source: Printer = sourcePrinter
if (annotate) {
emit(
resources
.fetch(id, ref, schemaOpt)
.flatMap(asSourceWithMetadata)
.rejectWhen(wrongJsonOrNotFound)
)
} else {
val sourceIO = resources.fetch(id, ref, schemaOpt).map(_.value.source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,11 @@ final class FilesRoutes(
def fetch(id: IdSegmentRef, ref: ProjectRef)(implicit caller: Caller): Route =
(headerValueByType(Accept) & varyAcceptHeaders) {
case accept if accept.mediaRanges.exists(metadataMediaRanges.contains) =>
emit(fetchMetadata(id, ref).rejectWhen(unauthorizedOrNotFound))
emit(fetchMetadata(id, ref).rejectOn[FileNotFound])
case _ =>
emit(files.fetchContent(id, ref).rejectWhen(unauthorizedOrNotFound))
emit(files.fetchContent(id, ref).rejectOn[FileNotFound])
}

private val unauthorizedOrNotFound: PartialFunction[FileRejection, Boolean] = {
case _: AuthorizationFailed | _: FileNotFound => true
}

def fetchMetadata(id: IdSegmentRef, ref: ProjectRef)(implicit caller: Caller): IO[FileRejection, FileResource] =
aclCheck.authorizeForOr(ref, Read)(AuthorizationFailed(ref, Read)) >> files.fetch(id, ref)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ trait DeltaDirectives extends UriDirectives {
}
}

/** Injects a `Vary: Accept,Accept-Encoding` into the response */
def varyAcceptHeaders: Directive0 =
vary(Set(Accept.name, `Accept-Encoding`.name))

private def vary(headers: Set[String]): Directive0 =
respondWithHeader(RawHeader("Vary", headers.mkString(",")))

/**
* If the `Accept` header is set to `text/html`, redirect to the matching resource page in fusion if the feature is
* enabled
Expand Down Expand Up @@ -186,4 +179,19 @@ trait DeltaDirectives extends UriDirectives {
/** The URI of fusion's main login page */
def fusionLoginUri(implicit config: FusionConfig): UIO[Uri] =
UIO.pure { config.base / "login" }

/** Injects a `Vary: Accept,Accept-Encoding` into the response */
def varyAcceptHeaders: Directive0 =
vary(Set(Accept.name, `Accept-Encoding`.name))

private def vary(headers: Set[String]): Directive0 =
respondWithHeader(RawHeader("Vary", headers.mkString(",")))

private def respondWithHeader(responseHeader: HttpHeader): Directive0 =
mapSuccessResponse(r => r.withHeaders(r.headers :+ responseHeader))

private def mapSuccessResponse(f: HttpResponse => HttpResponse): Directive0 =
mapRouteResultPF {
case RouteResult.Complete(response) if response.status.isSuccess => RouteResult.Complete(f(response))
}
}

0 comments on commit 7f56d21

Please sign in to comment.