Skip to content

Commit

Permalink
Reduces Boilerplate in Server creation (#98)
Browse files Browse the repository at this point in the history
* Reduces boilerplate when creating a server. Aditionally:
* Unifies test utilities to DRY between @tagless and @service tests
* Removes ETA Expansion warnings
* Removes bootstrapFuture syntax
* Removes all those imports that are not used anymore
* Others
* Releases new patch version, 0.4.2
  • Loading branch information
juanpedromoreno authored Dec 18, 2017
1 parent 4597dfc commit 4c2e012
Show file tree
Hide file tree
Showing 22 changed files with 225 additions and 317 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ lazy val rpc = project
.settings(scalaMetaSettings: _*)
.settings(
Seq(
scalacOptions += "-Ywarn-unused-import",
libraryDependencies ++= commonDeps ++
Seq(
%%("frees-core", freesV),
Expand Down
2 changes: 0 additions & 2 deletions rpc/src/main/scala/client/ChannelConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import java.util.concurrent.{Executor, TimeUnit}
import cats.implicits._
import freestyle._
import freestyle.config.ConfigM
import freestyle.config.implicits._
import freestyle.rpc.client._
import io.grpc._

sealed trait ManagedChannelFor extends Product with Serializable
Expand Down
12 changes: 7 additions & 5 deletions rpc/src/main/scala/client/handlers/TaskMHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import monix.execution.Scheduler
class TaskMHandler[F[_]](implicit AC: AsyncContext[F], S: Scheduler) extends FSHandler[Task, F] {

override def apply[A](fa: Task[A]): F[A] = AC.runAsync { cb =>
fa.runAsync(new Callback[A] {
override def onSuccess(value: A): Unit = cb(Right(value))

override def onError(ex: Throwable): Unit = cb(Left(ex))
})
fa.runAsync {
new Callback[A] {
override def onSuccess(value: A): Unit = cb(Right(value))

override def onError(ex: Throwable): Unit = cb(Left(ex))
}
}
(): Unit
}
}
1 change: 0 additions & 1 deletion rpc/src/main/scala/internal/MonixAdapters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package internal
import cats.instances.future._
import cats.~>
import io.grpc.stub.StreamObserver
import monix.eval.Callback
import monix.execution.Ack.{Continue, Stop}
import monix.execution.{Ack, Scheduler}
import monix.reactive.Observable.Operator
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/main/scala/internal/service/service.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private[internal] case class RPCRequest(

val encodersImport: Import = serialization match {
case Protobuf =>
q"import _root_.pbdirect._, _root_.freestyle.rpc.internal.service.encoders.pbd._"
q"import _root_.freestyle.rpc.internal.service.encoders.pbd._"
case Avro =>
q"import _root_.freestyle.rpc.internal.service.encoders.avro._"
}
Expand Down
1 change: 0 additions & 1 deletion rpc/src/main/scala/proto/annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package object protocol {

@compileTimeOnly("enable macro paradise to expand @free macro annotations")
class service extends StaticAnnotation {
import scala.meta._

inline def apply(defn: Any): Any = meta { serviceImpl.service(defn) }
}
Expand Down
1 change: 0 additions & 1 deletion rpc/src/main/scala/server/ServerConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package server
import cats.implicits._
import freestyle._
import freestyle.config.ConfigM
import freestyle.config.implicits._
import io.grpc.Server

case class ServerW(port: Int, configList: List[GrpcConfig]) {
Expand Down
16 changes: 9 additions & 7 deletions rpc/src/main/scala/server/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ import freestyle._
import freestyle.implicits._
import freestyle.logging._
import freestyle.loggingJVM.implicits._

import scala.concurrent.Future
import freestyle.rpc.server.handlers.GrpcServerHandler

@module
trait GrpcServerApp {
val server: GrpcServer
val log: LoggingM
}

trait ServerImplicits {

implicit def grpcServerHandler[M[_]: Capture](implicit SW: ServerW): GrpcServer.Op ~> M =
new GrpcServerHandler[M] andThen new GrpcKInterpreter[M](SW.server)

}

trait Syntax {

implicit def serverOps(server: FreeS[GrpcServerApp.Op, Unit]): ServerOps = new ServerOps(server)
Expand All @@ -40,11 +46,6 @@ trait Syntax {
def bootstrapM[M[_]: Monad](implicit handler: GrpcServer.Op ~> M): M[Unit] =
server.interpret[M]

def bootstrapFuture(
implicit MF: Monad[Future],
handler: GrpcServer.Op ~> Future): Future[Unit] =
server.interpret[Future]

}
}

Expand All @@ -69,6 +70,7 @@ object implicits
with RPCAsyncImplicits
with Syntax
with Helpers
with ServerImplicits
with freestyle.Interpreters
with freestyle.FreeSInstances
with freestyle.loggingJVM.Implicits
111 changes: 111 additions & 0 deletions rpc/src/test/scala/CommonUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2017 47 Degrees, LLC. <http://www.47deg.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package freestyle.rpc

import cats.effect.IO
import freestyle.rpc.client.{ChannelConfig, ConfigForAddress, ManagedChannelFor}
import freestyle.rpc.server._

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}

trait CommonUtils {

import cats.implicits._
import freestyle._
import freestyle.implicits._
import freestyle.config.implicits._

type ConcurrentMonad[A] = IO[A]

object database {

val i: Int = 5
val a1: A = A(1, 2)
val a2: A = A(10, 20)
val a3: A = A(100, 200)
val a4: A = A(1000, 2000)
val c1: C = C("foo1", a1)
val c2: C = C("foo2", a1)
val e1: E = E(a3, "foo3")
val e2: E = E(a4, "foo4")

val cList = List(c1, c2)
val eList = List(e1, e2)

val dResult: D = D(6)
}

def createManagedChannelFor: ManagedChannelFor =
ConfigForAddress[ChannelConfig.Op]("rpc.client.host", "rpc.client.port")
.interpret[Try] match {
case Success(c) => c
case Failure(e) =>
e.printStackTrace()
throw new RuntimeException("Unable to load the client configuration", e)
}

def createServerConf(grpcConfigs: List[GrpcConfig]): ServerW =
BuildServerFromConfig[ServerConfig.Op]("rpc.server.port", grpcConfigs)
.interpret[Try] match {
case Success(c) => c
case Failure(e) =>
e.printStackTrace()
throw new RuntimeException("Unable to load the server configuration", e)
}

def serverStart[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = {
val server = APP.server
val log = APP.log
for {
_ <- server.start()
port <- server.getPort
_ <- log.info(s"Server started, listening on $port")
} yield ()
}

def serverStop[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = {
val server = APP.server
val log = APP.log
for {
port <- server.getPort
_ <- log.info(s"Stopping server listening on $port")
_ <- server.shutdownNow()
} yield ()
}

def debug(str: String): Unit =
println(s"\n\n$str\n\n")

trait CommonRuntime {

import freestyle._
import freestyle.rpc.server.implicits._

implicit val S: monix.execution.Scheduler = monix.execution.Scheduler.Implicits.global

implicit class InterpreterOps[F[_], A](fs: FreeS[F, A])(
implicit H: FSHandler[F, ConcurrentMonad]) {

def runF: A = Await.result(fs.interpret[ConcurrentMonad].unsafeToFuture(), Duration.Inf)

}

}

}
6 changes: 2 additions & 4 deletions rpc/src/test/scala/RPCTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import freestyle.rpc.protocol.Empty

class RPCTests extends RpcBaseTestSuite with BeforeAndAfterAll {

import cats.implicits._
import freestyle.rpc.Utils.service._
import freestyle.rpc.Utils._
import freestyle.rpc.Utils.database._
import freestyle.rpc.Utils.helpers._
import freestyle.rpc.Utils.implicits._

override protected def beforeAll(): Unit = {
Expand Down Expand Up @@ -180,4 +178,4 @@ class RPCTests extends RpcBaseTestSuite with BeforeAndAfterAll {

}

}
}
6 changes: 2 additions & 4 deletions rpc/src/test/scala/TaglessRPCTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import freestyle.rpc.protocol.Empty

class FreesRPCTests extends RpcBaseTestSuite with BeforeAndAfterAll {

import cats.implicits._
import freestyle.rpc.TaglessUtils.service._
import freestyle.rpc.Utils._
import freestyle.rpc.TaglessUtils.database._
import freestyle.rpc.TaglessUtils.helpers._
import freestyle.rpc.TaglessUtils.implicits._

override protected def beforeAll(): Unit = {
Expand Down Expand Up @@ -180,4 +178,4 @@ class FreesRPCTests extends RpcBaseTestSuite with BeforeAndAfterAll {

}

}
}
Loading

0 comments on commit 4c2e012

Please sign in to comment.