/covenant

Primary LanguageScalaMIT LicenseMIT

covenant

Simply create HTTP or Websocket Server and client in scala.

Server-side is JVM only and uses akka, client-side additionally supports scala-js.

Get via jitpack (add the following to your build.sbt):

resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies ++= Seq(
  "com.github.cornerman.covenant" %%% "covenant-http" % "master-SNAPSHOT",
  "com.github.cornerman.covenant" %%% "covenant-ws" % "master-SNAPSHOT"
)

Http

Define a trait as your Api:

trait Api {
    def fun(a: Int): Future[Int]
}

Server

Implement your Api:

object ApiImpl extends Api {
    def fun(a: Int): Future[Int] = Future.successful(a)
}

Define a router with sloth using e.g. boopickle for serialization:

import sloth._
import boopickle.Default._
import chameleon.ext.boopickle._
import java.nio.ByteBuffer
import cats.implicits._

val router = Router[ByteBuffer, Future].route[Api](ApiImpl)

Plug the router into your akka-http server route:

import akka.http.scaladsl.Http
import akka.http.scaladsl.server.RouteResult._
import covenant.http._

Http().bindAndHandle(AkkaHttpRoute.fromFutureRouter(router), interface = "0.0.0.0", port = port)

Client

Let sloth implement your Api on the client side:

import sloth._
import boopickle.Default._
import chameleon.ext.boopickle._
import java.nio.ByteBuffer
import cats.implicits._
import covenant.http._

val client = HttpClient[ByteBuffer](yourUrl)
val api: Api = client.wire[Api]

Make requests to the server like normal method calls:

api.fun(1).foreach { num =>
  println(s"Got response: $num")
}

Websocket

TODO: documentation

See: