Akka-http: Support dynamic route changing without need to restart service

Created on 10 Oct 2017  路  2Comments  路  Source: akka/akka-http

Currently I came to a scenario where I need to dynamically compose or set new route while web-server continues to run without restarting it. As I understand it is not currently supported. I think this can be a very useful feature.

Most helpful comment

Routes are just functions RequestContext => Future[RouteResult], so you can implement all the dynamic behavior you'd like to have. In the simplest case you could read a (volatile) var for every request with the current route and execute that.

E.g.

private[this] @volatile var _currentRoute: Route = initialRoute

val rootRoute: Route = ctx => _currentRoute(ctx)
// pass rootRoute to bindAndHandle

// or:
// val rootRoute = pass { _currentRoute }
// where `pass` is a predefined directive that just executes the given route

This could probably be packaged nicer to hide the mutable state. Would that work for you?

All 2 comments

Routes are just functions RequestContext => Future[RouteResult], so you can implement all the dynamic behavior you'd like to have. In the simplest case you could read a (volatile) var for every request with the current route and execute that.

E.g.

private[this] @volatile var _currentRoute: Route = initialRoute

val rootRoute: Route = ctx => _currentRoute(ctx)
// pass rootRoute to bindAndHandle

// or:
// val rootRoute = pass { _currentRoute }
// where `pass` is a predefined directive that just executes the given route

This could probably be packaged nicer to hide the mutable state. Would that work for you?

Hmmm... yeah this should work, I will try it! Thanks, a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MichaelZinsmaier picture MichaelZinsmaier  路  6Comments

gihankarunarathne picture gihankarunarathne  路  5Comments

fmeriaux picture fmeriaux  路  4Comments

schmitch picture schmitch  路  6Comments

jrudolph picture jrudolph  路  3Comments