-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduces Boilerplate in Server creation (#98)
* 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
1 parent
4597dfc
commit 4c2e012
Showing
22 changed files
with
225 additions
and
317 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
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
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,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) | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.