-
-
Save johanandren/abb594cafd10a266138730180ddf93c9 to your computer and use it in GitHub Desktop.
Example swapping a complete route tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.server._ | |
import akka.stream.ActorMaterializer | |
class DynamicTree extends Directives { | |
@volatile private var root: Route = reject | |
private val apply = (requestContext: RequestContext) => root(requestContext) | |
def dynamicRoot(initialRoute: Route): Route = { | |
root = initialRoute.apply | |
apply | |
} | |
def swapDynamic(newRoot: Route): Unit = { | |
root = newRoot.apply | |
} | |
} | |
object SwapRoutes extends App { | |
implicit val system = ActorSystem() | |
implicit val mat = ActorMaterializer() | |
import Directives._ | |
val dynamicTree = new DynamicTree | |
val alternative: Route = get { | |
complete("alternative everything") | |
} | |
val route = | |
dynamicTree.dynamicRoot( | |
get { | |
concat( | |
path("swap") { | |
dynamicTree.swapDynamic(alternative) | |
complete("swapped behaviors") | |
}, | |
// all other paths | |
complete("initial tree") | |
) | |
} | |
) | |
Http().bindAndHandle(route, "127.0.0.1", 8080) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment