@@ -74,7 +74,7 @@ abstract class Main{
74
74
}
75
75
76
76
object Main {
77
- class DefaultHandler (routeTries : Map [String , DispatchTrie [ (Routes , EndpointMetadata [_])]],
77
+ class DefaultHandler (routeTries : DispatchTrie [ Map [String , (Routes , EndpointMetadata [_])]],
78
78
mainDecorators : Seq [Decorator [_, _, _]],
79
79
debugMode : Boolean ,
80
80
handleNotFound : () => Response .Raw ,
@@ -101,16 +101,10 @@ object Main{
101
101
(r : Any ) => Main .writeResponse(exchange, r.asInstanceOf [Response .Raw ])
102
102
)
103
103
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
-
111
- dispatchTrie.lookup(Util .splitPath(exchange.getRequestPath).toList, Map ()) match {
104
+ routeTries.lookup(Util .splitPath(exchange.getRequestPath).toList, Map ()) match {
112
105
case None => Main .writeResponse(exchange, handleNotFound())
113
- case Some (((routes, metadata), routeBindings, remaining)) =>
106
+ case Some ((methodMap, routeBindings, remaining)) if methodMap.contains(effectiveMethod) =>
107
+ val (routes, metadata) = methodMap(effectiveMethod)
114
108
Decorator .invoke(
115
109
Request (exchange, remaining),
116
110
metadata.endpoint,
@@ -128,8 +122,8 @@ object Main{
128
122
)
129
123
None
130
124
}
125
+ case _ => Main .writeResponse(exchange, handleMethodNotAllowed())
131
126
}
132
- // println("Completed Request: " + exchange.getRequestPath)
133
127
}catch {case e : Throwable =>
134
128
e.printStackTrace()
135
129
}
@@ -149,23 +143,25 @@ object Main{
149
143
)
150
144
}
151
145
152
- def prepareRouteTries (allRoutes : Seq [Routes ]): Map [String , DispatchTrie [ (Routes , EndpointMetadata [_])]] = {
153
- val routeList = for {
146
+ def prepareRouteTries (allRoutes : Seq [Routes ]): DispatchTrie [ Map [String , (Routes , EndpointMetadata [_])]] = {
147
+ val flattenedRoutes = for {
154
148
routes <- allRoutes
155
- route <- routes.caskMetadata.value.map(x => x : EndpointMetadata [_])
156
- } yield (routes, route)
149
+ metadata <- routes.caskMetadata.value
150
+ } yield {
151
+ val segments = Util .splitPath(metadata.endpoint.path)
152
+ val methodMap = metadata.endpoint.methods.map(_ -> (routes, metadata : EndpointMetadata [_])).toMap
153
+ (segments, methodMap, metadata.endpoint.subpath)
154
+ }
157
155
158
- val allMethods : Set [String ] =
159
- routeList.flatMap(_._2.endpoint.methods).map(_.toLowerCase).toSet
156
+ val dispatchInputs = flattenedRoutes.groupBy(_._1).map { case (segments, values) =>
157
+ val methodMap = values.map(_._2).flatten.toMap
158
+ val hasSubpath = values.map(_._3).contains(true )
159
+ (segments, methodMap, hasSubpath)
160
+ }.toSeq
160
161
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
162
+ DispatchTrie .construct(0 , dispatchInputs)
168
163
}
164
+
169
165
def writeResponse (exchange : HttpServerExchange , response : Response .Raw ) = {
170
166
response.data.headers.foreach{case (k, v) =>
171
167
exchange.getResponseHeaders.put(new HttpString (k), v)
0 commit comments