Hi all. Jumping on the akka-streams/akka-http bandwagon and very excited! 
However, I've got a couple of puzzlers that stopped me short at the door. 

Quick context: 

* I'm making the server for a location-sharing app for activists 
<https://github.com/the-learning-collective/whereat-server/tree/a884bb2388d659624bfde167c752dbd0cccb8c53>
 
with a bit of help from fellow Recurse Center <https://www.recurse.com/> 
alums
* My first user story is to make an endpoint that responds to a POST of a 
user's location by echoing the location back.
* I'm running akka-http 1.0-RC4 and spray-json 1.3.2 (this is my sbt.build 
<https://github.com/the-learning-collective/whereat-server/blob/a884bb2388d659624bfde167c752dbd0cccb8c53/build.sbt>
 
file)

Now to problem number 1 (will address problem 2 in a separate thread):

I've got the following definition of a `Location`: 

package model

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol

case class Location(id: String, lat: Double, lon: Double, time: Long)

object LocationJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport {
  implicit val locationFormat = jsonFormat4(Location)
}



That works just fine when I plop it into a route in my Main object:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import model.Location
import model.LocationJsonProtocol._
import util.Config

object Main extends App with Config {

  import system.dispatcher
  implicit val system = ActorSystem("whereat-server")
  implicit val materializer = ActorMaterializer()

  def echo (loc: Location)(completer: Location ⇒ Unit) = completer(loc)

  val route =
    path("hello") {
      get {
        complete {
          "hello world!" }
      }
    } ~
    path("locations") {
      post {
        entity(as[Location]) { loc ⇒
          completeWith(instanceOf[Location]) {
            completer ⇒ echo(loc)(completer)
          }
        }
      }
    }

  Http().bindAndHandle(route, httpInterface, httpPort)

  println(s"Server online at http://localhost:$httpPort";)

}


*However:* when I try to mix in `route` with a trait (so that I can start 
to compose routes like I'm told I should with this awesome routing DSL!), 
like so...

package routes

import akka.http.scaladsl.server.Directives._
import model.Location
import model.LocationJsonProtocol._

trait Routes {

  def echo (loc: Location)(completer: Location ⇒ Unit) = completer(loc)

  val route =
    path("hello") {
      get {
        complete {
          "hello world!"
        }
      }
    } ~
    path("locations") {
      post {
        entity(as[Location]) { loc ⇒
          completeWith(instanceOf[Location]) {
            completer ⇒ echo(loc)(completer)
          }
        }
      }
    }
}


... the JSON marshalling/unmarshalling breaks because it doesn't have the 
implicit in scope, and I get the following compile errors:

Error:(27, 18) could not find implicit value for parameter um: 
akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[model.Location]
        entity(as[Location]) { loc ⇒
                 ^
Error:(27, 18) not enough arguments for method as: (implicit um: 
akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[model.Location])akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[model.Location].
Unspecified value parameter um.
        entity(as[Location]) { loc ⇒
                 ^


The code in both instances is almost identical. I double and triple checked 
that I've done all the same imports and that the code in Main actually 
works. The only thing I can see that is different about these two bits of 
code is where they're located! Can anyone help point me to how to get out 
of this pickle? (If I have to keep all of my routes in the Main object, 
that's going to get unwieldy soon as the application grows and defeats the 
whole purpose of having composable routes in the first place!

/a/

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to