Can Spray.io routes be split into multiple "Controllers"?

crockpotveggies picture crockpotveggies · Feb 1, 2013 · Viewed 8.3k times · Source

I haven't found a solid example or structure to splitting up Spray.io routes into multiple files. I am finding that the current structure of my routes are going to become very cumbersome, and it would be nice to abstract them into different "Controllers" for a very simple REST API app.

Docs don't seem to help too much: http://spray.io/documentation/spray-routing/key-concepts/directives/#directives

Here's what I have so far:

class AccountServiceActor extends Actor with AccountService {

  def actorRefFactory = context

  def receive = handleTimeouts orElse runRoute(demoRoute)

  def handleTimeouts: Receive = {
    case Timeout(x: HttpRequest) =>
      sender ! HttpResponse(StatusCodes.InternalServerError, "Request timed out.")
  }
}


// this trait defines our service behavior independently from the service actor
trait AccountService extends HttpService {

  val demoRoute = {
    get {
      path("") {
        respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
          complete(index)
        }
      } ~
      path("ping") {
        complete("PONG!")
      } ~
      path("timeout") { ctx =>
        // we simply let the request drop to provoke a timeout
      } ~
      path("crash") { ctx =>
        throw new RuntimeException("crash boom bang")
      } ~
      path("fail") {
        failWith(new RuntimeException("aaaahhh"))
      } ~
      path("riaktestsetup") {
        Test.setupTestData
        complete("SETUP!")
      } ~
      path("riaktestfetch" / Rest) { id =>
        complete(Test.read(id))
      }
    }
  }
}

Thanks for help on this!

Answer

Adrien Aubel picture Adrien Aubel · Aug 11, 2014

I personally use this for large APIs:

class ApiActor extends Actor with Api {
  override val actorRefFactory: ActorRefFactory = context

  def receive = runRoute(route)
}

/**
 * API endpoints
 *
 * Individual APIs are created in traits that are mixed here
 */
trait Api extends ApiService
  with AccountApi with SessionApi
  with ContactsApi with GroupsApi
  with GroupMessagesApi with OneToOneMessagesApi
  with PresenceApi
  with EventsApi
  with IosApi
  with TelephonyApi
  with TestsApi {
  val route = {
    presenceApiRouting ~
    oneToOneMessagesApiRouting ~
    groupMessagesApiRouting ~
    eventsApiRouting ~
    accountApiRouting ~
    groupsApiRouting ~
    sessionApiRouting ~
    contactsApiRouting ~
    iosApiRouting ~
    telephonyApiRouting ~
    testsApiRouting
  }
}

I would recommend putting the most common routes first, and use pathPrefix as soon as you can in the sub-routes, so that you reduce the number of tests that Spray runs for each incoming request.

You'll find below a route that I believe is optimized:

  val groupsApiRouting = {
    pathPrefix("v3" / "groups") {
      pathEnd {
        get {
          traceName("GROUPS - Get joined groups list") { listJoinedGroups }
        } ~
        post {
          traceName("GROUPS - Create group") { createGroup }
        }
      } ~
      pathPrefix(LongNumber) { groupId =>
        pathEnd {
          get {
            traceName("GROUPS - Get by ID") { getGroupInformation(groupId) }
          } ~
          put {
            traceName("GROUPS - Edit by ID") { editGroup(groupId) }
          } ~
          delete {
            traceName("GROUPS - Delete by ID") { deleteGroup(groupId) }
          }
        } ~
        post {
          path("invitations" / LongNumber) { invitedUserId =>
            traceName("GROUPS - Invite user to group") { inviteUserToGroup(groupId, invitedUserId) }
          } ~
          path("invitations") {
            traceName("GROUPS - Invite multiple users") { inviteUsersToGroup(groupId) }
          }
        } ~
        pathPrefix("members") {
          pathEnd {
            get {
              traceName("GROUPS - Get group members list") { listGroupMembers(groupId) }
            }
          } ~
          path("me") {
            post {
              traceName("GROUPS - Join group") { joinGroup(groupId) }
            } ~
            delete {
              traceName("GROUPS - Leave group") { leaveGroup(groupId) }
            }
          } ~
          delete {
            path(LongNumber) { removedUserId =>
              traceName("GROUPS - Remove group member") { removeGroupMember(groupId, removedUserId) }
            }
          }
        } ~
        path("coverPhoto") {
          get {
            traceName("GROUPS - Request a new cover photo upload") { getGroupCoverPhotoUploadUrl(groupId) }
          } ~
          put {
            traceName("GROUPS - Confirm a cover photo upload") { confirmCoverPhotoUpload(groupId) }
          }
        } ~
        get {
          path("attachments" / "new") {
            traceName("GROUPS - Request attachment upload") { getGroupAttachmentUploadUrl(groupId) }
          }
        }
      }
    }
  }