-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly override trace and route for assets
- Loading branch information
1 parent
e7e736f
commit 91610b5
Showing
3 changed files
with
60 additions
and
44 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 was deleted.
Oops, something went wrong.
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,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 | ||
} | ||
} |