@@ -46,10 +46,10 @@ abstract class Main{
4646
4747 implicit def log : cask.util.Logger = new cask.util.Logger .Console ()
4848
49- def routeTries = Main .prepareRouteTries (allRoutes)
49+ def dispatchTrie = Main .prepareDispatchTrie (allRoutes)
5050
5151 def defaultHandler = new BlockingHandler (
52- new Main .DefaultHandler (routeTries , mainDecorators, debugMode, handleNotFound, handleMethodNotAllowed, handleEndpointError)
52+ new Main .DefaultHandler (dispatchTrie , mainDecorators, debugMode, handleNotFound, handleMethodNotAllowed, handleEndpointError)
5353 )
5454
5555 def handleNotFound () = Main .defaultHandleNotFound()
@@ -74,7 +74,7 @@ abstract class Main{
7474}
7575
7676object Main {
77- class DefaultHandler (routeTries : Map [String , DispatchTrie [ (Routes , EndpointMetadata [_])]],
77+ class DefaultHandler (dispatchTrie : DispatchTrie [ Map [String , (Routes , EndpointMetadata [_])]],
7878 mainDecorators : Seq [Decorator [_, _, _]],
7979 debugMode : Boolean ,
8080 handleNotFound : () => Response .Raw ,
@@ -101,35 +101,30 @@ object Main{
101101 (r : Any ) => Main .writeResponse(exchange, r.asInstanceOf [Response .Raw ])
102102 )
103103
104- val dispatchTrie : DispatchTrie [(Routes , EndpointMetadata [_])] = routeTries.get(effectiveMethod) match {
105- case None =>
106- Main .writeResponse(exchange, handleMethodNotAllowed())
107- return
108- case Some (trie) => trie
109- }
110-
111104 dispatchTrie.lookup(Util .splitPath(exchange.getRequestPath).toList, Map ()) match {
112105 case None => Main .writeResponse(exchange, handleNotFound())
113- case Some (((routes, metadata), routeBindings, remaining)) =>
114- Decorator .invoke(
115- Request (exchange, remaining),
116- metadata.endpoint,
117- metadata.entryPoint.asInstanceOf [EntryPoint [Routes , _]],
118- routes,
119- routeBindings,
120- (mainDecorators ++ routes.decorators ++ metadata.decorators).toList,
121- Nil
122- ) match {
123- case Result .Success (res) => runner(res)
124- case e : Result .Error =>
125- Main .writeResponse(
126- exchange,
127- handleError(routes, metadata, e)
128- )
129- None
106+ case Some ((methodMap, routeBindings, remaining)) =>
107+ methodMap.get(effectiveMethod) match {
108+ case None => Main .writeResponse(exchange, handleMethodNotAllowed())
109+ case Some ((routes, metadata)) =>
110+ Decorator .invoke(
111+ Request (exchange, remaining),
112+ metadata.endpoint,
113+ metadata.entryPoint.asInstanceOf [EntryPoint [Routes , _]],
114+ routes,
115+ routeBindings,
116+ (mainDecorators ++ routes.decorators ++ metadata.decorators).toList,
117+ Nil
118+ ) match {
119+ case Result .Success (res) => runner(res)
120+ case e : Result .Error =>
121+ Main .writeResponse(
122+ exchange,
123+ handleError(routes, metadata, e)
124+ )
125+ }
130126 }
131127 }
132- // println("Completed Request: " + exchange.getRequestPath)
133128 }catch {case e : Throwable =>
134129 e.printStackTrace()
135130 }
@@ -149,23 +144,25 @@ object Main{
149144 )
150145 }
151146
152- def prepareRouteTries (allRoutes : Seq [Routes ]): Map [String , DispatchTrie [ (Routes , EndpointMetadata [_])]] = {
153- val routeList = for {
147+ def prepareDispatchTrie (allRoutes : Seq [Routes ]): DispatchTrie [ Map [String , (Routes , EndpointMetadata [_])]] = {
148+ val flattenedRoutes = for {
154149 routes <- allRoutes
155- route <- routes.caskMetadata.value.map(x => x : EndpointMetadata [_])
156- } yield (routes, route)
150+ metadata <- routes.caskMetadata.value
151+ } yield {
152+ val segments = Util .splitPath(metadata.endpoint.path)
153+ val methodMap = metadata.endpoint.methods.map(_ -> (routes, metadata : EndpointMetadata [_])).toMap
154+ (segments, methodMap, metadata.endpoint.subpath)
155+ }
157156
158- val allMethods : Set [String ] =
159- routeList.flatMap(_._2.endpoint.methods).map(_.toLowerCase).toSet
157+ val dispatchInputs = flattenedRoutes.groupBy(_._1).map { case (segments, values) =>
158+ val methodMap = values.map(_._2).flatten.toMap
159+ val hasSubpath = values.map(_._3).contains(true )
160+ (segments, methodMap, hasSubpath)
161+ }.toSeq
160162
161- allMethods
162- .map { method =>
163- method -> DispatchTrie .construct[(Routes , EndpointMetadata [_])](0 ,
164- for ((route, metadata) <- routeList if metadata.endpoint.methods.contains(method))
165- yield (Util .splitPath(metadata.endpoint.path): collection.IndexedSeq [String ], (route, metadata), metadata.endpoint.subpath)
166- )
167- }.toMap
163+ DispatchTrie .construct(0 , dispatchInputs)
168164 }
165+
169166 def writeResponse (exchange : HttpServerExchange , response : Response .Raw ) = {
170167 response.data.headers.foreach{case (k, v) =>
171168 exchange.getResponseHeaders.put(new HttpString (k), v)
0 commit comments