user9920500
user9920500

Reputation: 695

How to introduce versioning for endpoints for akka http

I have 5 controllers in akka-http. Each endpoint has 5 endpoints(routes). Now I need to introduce versioning for those. All endpoints should be prefixed with /version1. For example if there was an endpoint xyz now it should be /version1/xyz. One of the ways is to add a pathPrefix But it needs to be added to each controller. Is there way to add it at a common place so that it appears for all endpoints.

I am using akka-http with scala.

Upvotes: 1

Views: 363

Answers (2)

Indirect Answer

Aleksey Isachenkov's answer is the correct direct solution.

One alternative is to put versioning in the hostname instead of the path. Once you have "version1" of your Route values in source-control then you can tag that checkin as "version1", deploy it into production, and then use DNS entries to set the service name to version1.myservice.com.

Then, once newer functionality becomes necessary you update your code and tag it in source-control as "version2". Release this updated build and use DNS to set the name as version2.myservice.com, while still keeping the version1 instance running. This would result in two active services running independently.

The benefits of this method are:

  1. Your code does not continuously grow longer as new versions are released.
  2. You can use logging to figure out if a version hasn't been used in a long time and then just kill that running instance of the service to End-Of-Life the version.
  3. You can use DNS to define your current "production" version by having production.myservice.com point to whichever version of the service you want. For example: once you've released version24.myservice.com and tested it for a while you can update the production.myservice.com pointer to go to 24 from 23. The old version can stay running for any users that don't want to upgrade, but anybody who wants the latest version can always use "production".

Upvotes: 0

Aleksey Isachenkov
Aleksey Isachenkov

Reputation: 1240

You can create a base route, that accepts paths like /version1/... and refers to internal routes without path prefix.

val version1Route = path("xyz") {
   ...
}
val version2Route = path("xyz") {
   ...
}
val route = pathPrefix("version1") {
   version1Route
} ~ pathPrefix("version2") {
   version2Route
}

Upvotes: 3

Related Questions