scalaresttwittermicroservicesfinagle

Simple hello world api using finagle - Error: object twitter is not a member of package com import com.twitter.finagle.Http


Im really new to this microservice world and was trying to learn from scratch. But for some reason not able to call a simple dependency.

Getting the error

object twitter is not a member of package com import com.twitter.finagle._

SBT file, Ive tried three ways

Trial 1

name := "microservicestest"

version := "0.1"

scalaVersion := "2.12.15"

libraryDependencies ++= Seq(
  "com.github.finagle" %% "finch-core" % "0.31.0",
  "com.github.finagle" %% "finch-circe" % "0.31.0",
  "io.circe" %% "circe-generic" % "0.9.0"
)

Trial 2 (puttting just the dependency line in here)

libraryDependencies += "com.twitter" %% "finagle-http" % "21.8.0"

Trial 3

libraryDependencies ++= Seq(
  "com.github.finagle" %% "finch-core" % "0.15.1"
)

Scala file

import io.finch._
import com.twitter.finagle.Http
import com.twitter.util.Await

object HelloWorld extends App {
  val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
  Await.ready(Http.server.serve(":8080", api.toServiceAs[Text.Plain]))
}

Solution

  • SBT file

    libraryDependencies += "com.twitter" %% "finagle-http" % "21.8.0"
    

    Working simple code

    import com.twitter.finagle.{Http, Service}
    import com.twitter.finagle.http
    import com.twitter.util.{Await, Future}
    
    object HelloWorld extends App {
      val service = new Service[http.Request, http.Response] {
        def apply(req: http.Request): Future[http.Response] =
          Future.value(http.Response(req.version, http.Status.Ok))
      }
      val server = Http.serve(":8080", service)
      Await.ready(server)
    }