Routing 'synonyms', aka All Routes Lead to Rome

211 views
Skip to first unread message

Elisabeth Anderson

unread,
Jul 25, 2016, 10:09:41 AM7/25/16
to spray.io User List
I'm updating some routes in my app which will obviously break some existing clients so I want to do the right thing and have the old routes there as a 'forward' from the old route to the new one. Is there a method of specifying the old route something like:

val routes = path("new" / "route" / Segment) { id =>
  get {
    complete(s"$id")
  }
} ~ path("old" / "route" / Segment) { id =>
  get {
    forward(s"new/route/$id")
  }
}

...so a request to both of these would work:


I can find a redirect but I'd rather have this happen under the hood inside Spray rather than do a hard redirect of the client, if it's possible?

Thanks!

Age Mooij

unread,
Jul 25, 2016, 10:29:15 AM7/25/16
to spray...@googlegroups.com
Hi Elisabeth

The most direct way to achieve this in spray/akka-htp is to factor out the shared bits into vals or defs and just reference them from both paths, i.e.:

def sharedRoute(id: String) = get { complete(s"$id") }

val routes = {
  path("new" / "route" / Segment) { id =>
    sharedroute(id)
  } ~ path("old" / "route" / Segment) { id =>
    sharedroute(id)
  }
}

Routes are just functions so this kind of composition is designed in from the start.

Hope this helps
Age


--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at https://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/ed7426ad-82ed-4fde-9d12-87684f336e3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elisabeth Anderson

unread,
Jul 26, 2016, 4:33:26 AM7/26/16
to spray.io User List
Ah yes, of course :)

Thanks!
Reply all
Reply to author
Forward
0 new messages