Skip to content

Commit

Permalink
Properly override trace and route for assets
Browse files Browse the repository at this point in the history
  • Loading branch information
vkostyukov committed Nov 13, 2018
1 parent e7e736f commit 91610b5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 44 deletions.
39 changes: 13 additions & 26 deletions core/src/main/scala/io/finch/Endpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -584,23 +584,7 @@ object Endpoint {
*/
def fromInputStream[F[_]](stream: Resource[F, InputStream])(
implicit F: Effect[F], S: ContextShift[F]
): Endpoint[F, Buf] =
new Endpoint[F, Buf] {
private def readLoop(left: Buf, stream: InputStream): F[Buf] = F.suspend {
val buffer = new Array[Byte](1024)
val n = stream.read(buffer)
if (n == -1) F.pure(left)
else readLoop(left.concat(Buf.ByteArray.Owned(buffer, 0, n)), stream)
}

final def apply(input: Input): Result[F, Buf] = {
val output = stream.use(s =>
S.shift.flatMap(_ => readLoop(Buf.Empty, s)).map(buf => Output.payload(buf))
)

EndpointResult.Matched(input, Trace.empty, output)
}
}
): Endpoint[F, Buf] = new FromInputStream[F](stream)

/**
* Creates an [[Endpoint]] from a given [[File]]. Uses [[Resource]] for safer resource
Expand Down Expand Up @@ -649,11 +633,13 @@ object Endpoint {
def classpathAsset[F[_]](path: String)(implicit
F: Effect[F],
S: ContextShift[F]
): Endpoint[F, Buf] =
new Asset[F](
path,
): Endpoint[F, Buf] = {
val asset = new Asset[F](path)
val stream =
fromInputStream[F](Resource.fromAutoCloseable(F.delay(getClass.getResourceAsStream(path))))
)

asset :: stream
}

/**
* Creates an [[Endpoint]] that serves an asset (static content) from a filesystem, located at
Expand All @@ -673,11 +659,12 @@ object Endpoint {
def filesystemAsset[F[_]](path: String)(implicit
F: Effect[F],
S: ContextShift[F]
): Endpoint[F, Buf] =
new Asset[F](
path,
fromFile[F](new File(path))
)
): Endpoint[F, Buf] = {
val asset = new Asset[F](path)
val file = fromFile[F](new File(path))

asset :: file
}

/**
* A root [[Endpoint]] that always matches and extracts the current request.
Expand Down
18 changes: 0 additions & 18 deletions core/src/main/scala/io/finch/endpoint/asset.scala

This file was deleted.

47 changes: 47 additions & 0 deletions core/src/main/scala/io/finch/endpoint/endpoint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.finch

import cats.effect.{ContextShift, Effect, Resource}
import cats.syntax.all._
import com.twitter.finagle.http.{Method => FinagleMethod}
import com.twitter.io.Buf
import java.io.InputStream
import shapeless.HNil

package object endpoint {

private[finch] class FromInputStream[F[_]](stream: Resource[F, InputStream])(
implicit F: Effect[F], S: ContextShift[F]
) extends Endpoint[F, Buf] {

private def readLoop(left: Buf, stream: InputStream): F[Buf] = F.suspend {
val buffer = new Array[Byte](1024)
val n = stream.read(buffer)
if (n == -1) F.pure(left)
else readLoop(left.concat(Buf.ByteArray.Owned(buffer, 0, n)), stream)
}

final def apply(input: Input): Endpoint.Result[F, Buf] =
EndpointResult.Matched(
input,
Trace.empty,
stream.use(s =>
S.shift.flatMap(_ => readLoop(Buf.Empty, s)).map(buf => Output.payload(buf))
)
)
}

private[finch] class Asset[F[_]](path: String)(implicit F: Effect[F]) extends Endpoint[F, HNil] {
final def apply(input: Input): Endpoint.Result[F, HNil] = {
val req = input.request
if (req.method != FinagleMethod.Get || req.path != path) EndpointResult.NotMatched[F]
else
EndpointResult.Matched(
input.withRoute(Nil),
Trace.fromRoute(input.route),
F.pure(Output.HNil)
)
}

final override def toString: String = path
}
}

0 comments on commit 91610b5

Please sign in to comment.