Skip to content

Commit

Permalink
Merge pull request #50 from avast/DependenciesUpdate
Browse files Browse the repository at this point in the history
Dependencies update
  • Loading branch information
jendakol authored Mar 4, 2020
2 parents 40bfef3 + fb6c28d commit 900f9a0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
14 changes: 7 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import sbt.Keys.libraryDependencies

val logger: Logger = ConsoleLogger()

crossScalaVersions := Seq("2.12.10")
crossScalaVersions := Seq("2.12.10", "2.13.1")

lazy val Versions = new {
val gpb3Version = "3.11.1"
val grpcVersion = "1.26.0"
val circeVersion = "0.11.2"
val http4sVersion = "0.20.15"
val grpcVersion = "1.27.0"
val circeVersion = "0.13.0"
val http4sVersion = "0.21.1"
val akkaHttp = "10.1.5" // DO NOT upgrade to 10.1.[67] - will cause https://github.com/scala/community-builds/issues/825
}

Expand Down Expand Up @@ -110,8 +110,8 @@ lazy val core = (project in file("core")).settings(
"io.grpc" % "grpc-core" % Versions.grpcVersion,
"io.grpc" % "grpc-protobuf" % Versions.grpcVersion,
"io.grpc" % "grpc-stub" % Versions.grpcVersion,
"org.typelevel" %% "cats-core" % "1.6.0",
"org.typelevel" %% "cats-effect" % "1.3.0",
"org.typelevel" %% "cats-core" % "2.1.1",
"org.typelevel" %% "cats-effect" % "2.1.1",
"com.kailuowang" %% "mainecoon-core" % "0.6.4",
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.2",
"org.slf4j" % "jul-to-slf4j" % "1.7.26",
Expand Down Expand Up @@ -176,5 +176,5 @@ def grpcExeFileName: String = {
}

val grpcArtifactId = "protoc-gen-grpc-java"
val grpcExeUrl = url(s"http://repo1.maven.org/maven2/io/grpc/$grpcArtifactId/${Versions.grpcVersion}/$grpcExeFileName")
val grpcExeUrl = url(s"https://repo1.maven.org/maven2/io/grpc/$grpcArtifactId/${Versions.grpcVersion}/$grpcExeFileName")
val grpcExePath = SettingKey[xsbti.api.Lazy[File]]("grpcExePath")
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cats.implicits._
import com.avast.grpc.jsonbridge.GrpcJsonBridge.GrpcMethodName
import com.avast.grpc.jsonbridge.ReflectionGrpcJsonBridge.{HandlerFunc, ServiceHandlers}
import com.avast.grpc.jsonbridge.{BridgeError, JavaGenericHelper, ReflectionGrpcJsonBridge}
import com.fasterxml.jackson.core.{JsonParseException, JsonProcessingException}
import com.fasterxml.jackson.core.JsonProcessingException
import com.typesafe.scalalogging.StrictLogging
import io.grpc._
import io.grpc.protobuf.ProtoFileDescriptorSupplier
Expand All @@ -22,7 +22,7 @@ import scala.util.control.NonFatal
import scala.util.{Failure, Success}

private[jsonbridge] object ScalaPBServiceHandlers extends ServiceHandlers with StrictLogging {
def createServiceHandlers[F[+ _]](ec: ExecutionContext)(inProcessChannel: ManagedChannel)(ssd: ServerServiceDefinition)(
def createServiceHandlers[F[_]](ec: ExecutionContext)(inProcessChannel: ManagedChannel)(ssd: ServerServiceDefinition)(
implicit F: Async[F]): Map[GrpcMethodName, HandlerFunc[F]] = {
if (ssd.getServiceDescriptor.getName == "grpc.reflection.v1alpha.ServerReflection") {
logger.debug("Reflection endpoint service cannot be bridged because its implementation is not ScalaPB-based")
Expand Down Expand Up @@ -86,7 +86,7 @@ private[jsonbridge] object ScalaPBServiceHandlers extends ServiceHandlers with S
Seq(servicePackage + "." + fileNameWithoutExtension + "." + serviceName + "Grpc$", servicePackage + "." + serviceName + "Grpc$")
}

private def createHandler[F[+ _]](ec: ExecutionContext)(futureStubCtor: () => AbstractStub[_])(method: ServerMethodDefinition[_, _])(
private def createHandler[F[_]](ec: ExecutionContext)(futureStubCtor: () => AbstractStub[_])(method: ServerMethodDefinition[_, _])(
implicit F: Async[F]): (GrpcMethodName, HandlerFunc[F]) = {
val requestCompanion = getRequestCompanion(method)
val requestClass = Class.forName(requestCompanion.getClass.getName.stripSuffix("$"))
Expand Down Expand Up @@ -144,7 +144,7 @@ private[jsonbridge] object ScalaPBServiceHandlers extends ServiceHandlers with S
companionField.get(requestMarshaller).asInstanceOf[GeneratedMessageCompanion[_]]
}

private def fromScalaFuture[F[+ _], A](ec: ExecutionContext)(fsf: F[Future[A]])(implicit F: Async[F]): F[A] = fsf.flatMap { sf =>
private def fromScalaFuture[F[_], A](ec: ExecutionContext)(fsf: F[Future[A]])(implicit F: Async[F]): F[A] = fsf.flatMap { sf =>
F.async { cb =>
sf.onComplete {
case Success(r) => cb(Right(r))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private[jsonbridge] object JavaServiceHandlers extends ServiceHandlers with Stri
JsonFormat.printer().includingDefaultValueFields().omittingInsignificantWhitespace()
}

def createServiceHandlers[F[+ _]](ec: ExecutionContext)(inProcessChannel: ManagedChannel)(ssd: ServerServiceDefinition)(
def createServiceHandlers[F[_]](ec: ExecutionContext)(inProcessChannel: ManagedChannel)(ssd: ServerServiceDefinition)(
implicit F: Async[F]): Map[GrpcMethodName, HandlerFunc[F]] = {
val futureStubCtor = createFutureStubCtor(ssd.getServiceDescriptor, inProcessChannel)
ssd.getMethods.asScala
Expand All @@ -42,7 +42,7 @@ private[jsonbridge] object JavaServiceHandlers extends ServiceHandlers with Stri
method.invoke(null, inProcessChannel).asInstanceOf[AbstractStub[_]]
}

private def createHandler[F[+ _]](ec: ExecutionContext)(futureStubCtor: () => AbstractStub[_])(method: ServerMethodDefinition[_, _])(
private def createHandler[F[_]](ec: ExecutionContext)(futureStubCtor: () => AbstractStub[_])(method: ServerMethodDefinition[_, _])(
implicit F: Async[F]): (GrpcMethodName, HandlerFunc[F]) = {
val requestMessagePrototype = getRequestMessagePrototype(method)
val javaMethod = futureStubCtor().getClass
Expand All @@ -62,7 +62,7 @@ private[jsonbridge] object JavaServiceHandlers extends ServiceHandlers with Stri
methodName.substring(0, 1).toLowerCase + methodName.substring(1)
}

private def coreHandler[F[+ _]](requestMessagePrototype: Message, execute: (Message, Map[String, String]) => F[MessageOrBuilder])(
private def coreHandler[F[_]](requestMessagePrototype: Message, execute: (Message, Map[String, String]) => F[MessageOrBuilder])(
implicit F: Async[F]): HandlerFunc[F] = { (json, headers) =>
{
parseRequest(json, requestMessagePrototype) match {
Expand All @@ -86,7 +86,7 @@ private[jsonbridge] object JavaServiceHandlers extends ServiceHandlers with Stri
}
}

private def executeRequest[F[+ _]](ec: ExecutionContext)(futureStubCtor: () => AbstractStub[_], javaMethod: Method)(
private def executeRequest[F[_]](ec: ExecutionContext)(futureStubCtor: () => AbstractStub[_], javaMethod: Method)(
req: Message,
headers: Map[String, String])(implicit F: Async[F]): F[MessageOrBuilder] = {
val metaData = {
Expand All @@ -107,7 +107,7 @@ private[jsonbridge] object JavaServiceHandlers extends ServiceHandlers with Stri
requestBuilder.build()
}

private def fromListenableFuture[F[+ _], A](ec: ExecutionContext)(flf: F[ListenableFuture[A]])(implicit F: Async[F]): F[A] = flf.flatMap {
private def fromListenableFuture[F[_], A](ec: ExecutionContext)(flf: F[ListenableFuture[A]])(implicit F: Async[F]): F[A] = flf.flatMap {
lf =>
F.async { cb =>
Futures.addCallback(lf, new FutureCallback[A] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avast.grpc.jsonbridge

import cats.effect._
import cats.syntax.all._
import com.avast.grpc.jsonbridge.GrpcJsonBridge.GrpcMethodName
import com.avast.grpc.jsonbridge.ReflectionGrpcJsonBridge.{HandlerFunc, ServiceHandlers}
import com.typesafe.scalalogging.StrictLogging
Expand All @@ -10,14 +11,14 @@ import io.grpc.inprocess.{InProcessChannelBuilder, InProcessServerBuilder}

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.language.{existentials, higherKinds}
import scala.language.higherKinds

object ReflectionGrpcJsonBridge extends ReflectionGrpcJsonBridge(JavaServiceHandlers) {
// JSON body and headers to a response (fail status or JSON response)
type HandlerFunc[F[+ _]] = (String, Map[String, String]) => F[Either[BridgeError.Narrow, String]]
type HandlerFunc[F[_]] = (String, Map[String, String]) => F[Either[BridgeError.Narrow, String]]

trait ServiceHandlers {
def createServiceHandlers[F[+ _]](ec: ExecutionContext)(inProcessChannel: ManagedChannel)(ssd: ServerServiceDefinition)(
def createServiceHandlers[F[_]](ec: ExecutionContext)(inProcessChannel: ManagedChannel)(ssd: ServerServiceDefinition)(
implicit F: Async[F]): Map[GrpcMethodName, HandlerFunc[F]]
}

Expand All @@ -26,11 +27,11 @@ object ReflectionGrpcJsonBridge extends ReflectionGrpcJsonBridge(JavaServiceHand

private[jsonbridge] class ReflectionGrpcJsonBridge(serviceHandlers: ServiceHandlers) extends StrictLogging {

def createFromServer[F[+ _]](ec: ExecutionContext)(grpcServer: io.grpc.Server)(implicit F: Async[F]): Resource[F, GrpcJsonBridge[F]] = {
def createFromServer[F[_]](ec: ExecutionContext)(grpcServer: io.grpc.Server)(implicit F: Async[F]): Resource[F, GrpcJsonBridge[F]] = {
createFromServices(ec)(grpcServer.getImmutableServices.asScala: _*)
}

def createFromServices[F[+ _]](ec: ExecutionContext)(services: ServerServiceDefinition*)(
def createFromServices[F[_]](ec: ExecutionContext)(services: ServerServiceDefinition*)(
implicit F: Async[F]): Resource[F, GrpcJsonBridge[F]] = {
for {
inProcessServiceName <- Resource.liftF(F.delay { s"ReflectionGrpcJsonBridge-${System.nanoTime()}" })
Expand All @@ -43,14 +44,14 @@ private[jsonbridge] class ReflectionGrpcJsonBridge(serviceHandlers: ServiceHandl
} yield bridge
}

def createFromHandlers[F[+ _]](handlersPerMethod: Map[GrpcMethodName, HandlerFunc[F]])(implicit F: Async[F]): GrpcJsonBridge[F] = {
def createFromHandlers[F[_]](handlersPerMethod: Map[GrpcMethodName, HandlerFunc[F]])(implicit F: Async[F]): GrpcJsonBridge[F] = {
new GrpcJsonBridge[F] {

override def invoke(methodName: GrpcJsonBridge.GrpcMethodName,
body: String,
headers: Map[String, String]): F[Either[BridgeError, String]] =
handlersPerMethod.get(methodName) match {
case Some(handler) => handler(body, headers)
case Some(handler) => handler(body, headers).map(x => x: Either[BridgeError, String])
case None => F.pure(Left(BridgeError.GrpcMethodNotFound))
}

Expand All @@ -62,7 +63,7 @@ private[jsonbridge] class ReflectionGrpcJsonBridge(serviceHandlers: ServiceHandl
}
}

private def createInProcessServer[F[+ _]](ec: ExecutionContext)(inProcessServiceName: String, services: Seq[ServerServiceDefinition])(
private def createInProcessServer[F[_]](ec: ExecutionContext)(inProcessServiceName: String, services: Seq[ServerServiceDefinition])(
implicit F: Sync[F]): Resource[F, Server] =
Resource {
F.delay {
Expand All @@ -73,12 +74,12 @@ private[jsonbridge] class ReflectionGrpcJsonBridge(serviceHandlers: ServiceHandl
}
}

private def createInProcessChannel[F[+ _]](ec: ExecutionContext)(inProcessServiceName: String)(
private def createInProcessChannel[F[_]](ec: ExecutionContext)(inProcessServiceName: String)(
implicit F: Sync[F]): Resource[F, ManagedChannel] =
Resource[F, ManagedChannel] {
F.delay {
val c = InProcessChannelBuilder.forName(inProcessServiceName).executor(ec.execute(_)).build()
(c, F.delay { c.shutdown() })
(c, F.delay { val _ = c.shutdown() })
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.avast.grpc.jsonbridge.http4s

import cats.Applicative
import cats.data.NonEmptyList
import cats.effect._
import cats.syntax.all._
import cats._
import com.avast.grpc.jsonbridge.GrpcJsonBridge.GrpcMethodName
import com.avast.grpc.jsonbridge.{BridgeError, BridgeErrorResponse, GrpcJsonBridge}
import com.typesafe.scalalogging.LazyLogging
Expand All @@ -15,7 +15,7 @@ import org.http4s.dsl.Http4sDsl
import org.http4s.dsl.impl.EntityResponseGenerator
import org.http4s.headers.{`Content-Type`, `WWW-Authenticate`}
import org.http4s.server.middleware.{CORS, CORSConfig}
import org.http4s.{Challenge, EntityEncoder, Header, Headers, HttpRoutes, MediaType, Response, Status}
import org.http4s._

import scala.language.{higherKinds, implicitConversions}

Expand Down Expand Up @@ -108,6 +108,10 @@ object Http4s extends LazyLogging {

private def mapStatus[F[_]: Sync](s: GrpcStatus, configuration: Configuration)(implicit h: Http4sDsl[F]): F[Response[F]] = {
import h._
val ClientClosedRequest = Status(499, "Client Closed Request")
final case class ClientClosedRequestOps(status: ClientClosedRequest.type) extends EntityResponseGenerator[F, F] {
val liftG: F ~> F = h.liftG
}

val description = BridgeErrorResponse.fromGrpcStatus(s)

Expand All @@ -133,9 +137,6 @@ object Http4s extends LazyLogging {
}
}

val ClientClosedRequest = Status(499, "Client Closed Request")
final case class ClientClosedRequestOps[F[_], G[_]](status: ClientClosedRequest.type) extends AnyVal with EntityResponseGenerator[F, G]

private implicit def grpcStatusJsonEntityEncoder[F[_]: Applicative]: EntityEncoder[F, BridgeErrorResponse] =
jsonEncoderOf[F, BridgeErrorResponse]
}
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.8
sbt.version=1.3.8

0 comments on commit 900f9a0

Please sign in to comment.