Coder Social home page Coder Social logo

zio-akka-http-interop's Introduction

zio-akka-http-interop

CI Maven Central

Small library, that provides interop between akka-http and ZIO: you'll be able to use ZIO values in your akka-http routes instead of Futures

How to use

Include zio-akka-http-interop in your build:

libraryDependencies += "io.scalac" %% "zio-akka-http-interop" % "0.4.0"

Then just mix akka.http.interop.ZIOSupport into the class, that defines your routes:

import akka.http.interop.ZIOSupport
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.Directives._
import zio._

object Api extends ZIOSupport {

  val routes: Route =
    pathPrefix("a") {
      get {
        val res: Task[String] = ZIO.succeed("OK")
        complete(res)
      }
    }
}

This will work with Throwable on the error channel.

Custom domain errors

If you want to use ZIO values with custom error type, you'll need some additional setup: interop code will have to know how to translate your domain error into HttpResponse. You can provide that knowledge by defining an akka.http.interop.ErrorResponse typeclass instance:

import akka.http.interop._
import akka.http.scaladsl.model.{ HttpResponse, StatusCodes }
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import zio.{ IO, ZIO }

object Api extends ZIOSupport {

  sealed trait DomainError
  case object FatalError extends DomainError
  case object BadData    extends DomainError

  implicit val domainErrorResponse: ErrorResponse[DomainError] = {
    case FatalError => HttpResponse(StatusCodes.InternalServerError)
    case BadData    => HttpResponse(StatusCodes.BadRequest)
  }

  val routes: Route =
    pathPrefix("ok") {
      get {
        val res: IO[DomainError, String] = ZIO.succeed("OK")
        complete(res)
      }
    } ~ pathPrefix("internal_server_error") {
      get {
        val res: IO[DomainError, String] = ZIO.fail(FatalError)
        complete(res)
      }
    } ~
      pathPrefix("bad_request") {
        get {
          val res: IO[DomainError, String] = ZIO.fail(BadData)
          complete(res)
        }
      }
}

Developed by Scalac

zio-akka-http-interop's People

Contributors

scala-steward avatar vpavkin avatar jczuchnowski avatar mtsokol avatar redroy44 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.