-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1017 from finagle/vk/resource
Introduce asset endpoints
- Loading branch information
Showing
9 changed files
with
229 additions
and
5 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 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
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 = s"GET /$path" | ||
} | ||
} |
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 @@ | ||
foo bar baz |
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