diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2019-09-15 09:10:38 +0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2019-09-15 09:10:38 +0800 |
commit | 38c6a84a0e8fbc3debb93bbacaf8c092e675ecb3 (patch) | |
tree | fe9b9a21e9c4eb17a3a27dc59a5be138ecebe98e /cask/src/cask/main/Main.scala | |
parent | 9e58e95add96a075d2cb70aa477441261f481ebd (diff) | |
download | cask-38c6a84a0e8fbc3debb93bbacaf8c092e675ecb3.tar.gz cask-38c6a84a0e8fbc3debb93bbacaf8c092e675ecb3.tar.bz2 cask-38c6a84a0e8fbc3debb93bbacaf8c092e675ecb3.zip |
Move recursive Decorator-processing logic into `Decorator.invoke`, document what it does
Avoid reversing annotations list in Router.scala only to reverse the final results again when binding the arguments to `EntryPoint.invoke`
Diffstat (limited to 'cask/src/cask/main/Main.scala')
-rw-r--r-- | cask/src/cask/main/Main.scala | 37 |
1 files changed, 8 insertions, 29 deletions
diff --git a/cask/src/cask/main/Main.scala b/cask/src/cask/main/Main.scala index a138e8a..fbcbff9 100644 --- a/cask/src/cask/main/Main.scala +++ b/cask/src/cask/main/Main.scala @@ -89,35 +89,14 @@ abstract class BaseMain{ routeTries(effectiveMethod).lookup(Util.splitPath(exchange.getRequestPath).toList, Map()) match { case None => writeResponse(exchange, handleNotFound()) case Some(((routes, metadata), routeBindings, remaining)) => - val ctx = Request(exchange, remaining) - def rec(remaining: List[RawDecorator], - bindings: List[Map[String, Any]]): Router.Result[Any] = try { - remaining match { - case head :: rest => - head.wrapFunction( - ctx, - args => rec(rest, args :: bindings) - .asInstanceOf[cask.internal.Router.Result[cask.model.Response.Raw]] - ) - - case Nil => - metadata.endpoint.wrapFunction(ctx, (endpointBindings: Map[String, Any]) => - metadata.entryPoint - .asInstanceOf[EntryPoint[cask.main.Routes, cask.model.Request]] - .invoke( - routes, ctx, - (endpointBindings ++ routeBindings.mapValues(metadata.endpoint.wrapPathSegment)) - :: bindings.reverse - ) - .asInstanceOf[Router.Result[Nothing]] - ) - } - // Make sure we wrap any exceptions that bubble up from decorator - // bodies, so outer decorators do not need to worry about their - // delegate throwing on them - }catch{case e: Throwable => Router.Result.Error.Exception(e) } - - rec((metadata.decorators ++ routes.decorators ++ mainDecorators).toList, Nil)match{ + Decorator.invoke( + Request(exchange, remaining), + metadata, + routes, + routeBindings, + (mainDecorators ++ routes.decorators ++ metadata.decorators).toList, + Nil + ) match{ case Router.Result.Success(res) => runner(res) case e: Router.Result.Error => writeResponse( |