Cats Effect/FS2

The fs2 part contains functions using FS2 to create Cats-Effects / Resources.

Usage

If you’re using sbt just add the following to your build definition:

libraryDependencies ++= Seq(
  "co.fs2"            %% "fs2-core"  % "3.2.4",
  "com.github.mkroli" %% "dns4s-fs2" % "0.21.0-SNAPSHOT"
)

Imports

Use the following additional imports to get started:

import cats.effect._
import com.comcast.ip4s._

import com.github.mkroli.dns4s.dsl._
import com.github.mkroli.dns4s.fs2.Dns

Server

The following is an excerpt from examples/simple-fs2/../DnsServer.scala:

object DnsServer extends IOApp {
  override def run(args: List[String]): IO[ExitCode] = {
    Dns
      .server[IO](port = Some(port"5354")) {
        case Query(q) ~ Questions(QName(host) ~ TypeA() :: Nil) =>
          IO.pure(Response(q) ~ Answers(RRName(host) ~ ARecord("1.2.3.4")))
      }
      .as(ExitCode.Success)
  }
}

Client

The following is an excerpt from examples/simple-fs2-client/../DnsClient.scala:

object DnsClient extends IOApp {
  override def run(args: List[String]): IO[ExitCode] = {
    Dns
      .client[IO](SocketAddress(ip"8.8.8.8", port"53"))
      .use { dns =>
        for {
          address <- dns.queryFor(Query ~ Questions(QName("google.com"))) {
            case Response(Answers(answers)) => IO.fromOption {
              answers.collectFirst {
                case ARecord(arecord) => arecord.address.getHostAddress
              }
            }(new RuntimeException("Response doesn't contain A-Record"))
          }
          _ <- IO.println(address)
        } yield ExitCode.Success
      }
  }
}