scalafinagletwitter-finagle

Finch Hello World Error: Http not a member of com.twitter.finagle


I'm trying to use the scala finch library to build an API.

I have the following simple code:

package example

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

object HelloWorld extends App {

  val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }

  Http.serve(":8080", api.toService)
}

And a build.sbt file that looks like this:

name := "hello-finch"

version := "1.0"

scalaVersion := "2.10.6"

mainClass in (Compile, run) := Some("example.HelloWorld")

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

// found here: https://github.com/finagle/finch/issues/604
addCompilerPlugin(
  "org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full
)

When I compile and run the code I get this error message:

object Http is not a member of package com.twitter.finagle
[error] import com.twitter.finagle.Http
[error]        ^
[error] /Users/jamesk/Code/hello_finch/hello-finch/src/main/scala/example/Hello.scala:8: wrong number of type arguments for io.finch.Endpoint, should be 2
[error]   val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
[error]            ^
[error] /Users/jamesk/Code/hello_finch/hello-finch/src/main/scala/example/Hello.scala:8: not found: value get
[error]   val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
[error]                               ^
[error] /Users/jamesk/Code/hello_finch/hello-finch/src/main/scala/example/Hello.scala:10: not found: value Http
[error]   Http.serve(":8080", api.toService)
[error]   ^
[error] four errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed Aug 15, 2017 12:56:01 PM

At this point I'm running out of ideas, it looks like a good library but it's a pain getting it working. Any help would be very much appreciated.


Solution

  • I have updated your example to work with the last version of Finch: "com.github.finagle" %% "finch-core" % "0.15.1" and also Scala 2.12

    the build.sbt file:

    name := "hello-finch"
    
    version := "1.0"
    
    scalaVersion := "2.12.2"
    
    mainClass in (Compile, run) := Some("example.HelloWorld")
    
    libraryDependencies ++= Seq(
      "com.github.finagle" %% "finch-core" % "0.15.1"
    )
    

    then, the src/main/scala/example/HelloWorld.scala file:

    package example
    
    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]))
    }
    

    Notice also that having Await.ready() is mandatory - your program would exit right away otherwise.