@@ -8,6 +8,7 @@ use crate::executors::execute_http_function;
88use crate :: types:: function_info:: FunctionInfo ;
99use crate :: types:: headers:: Headers ;
1010use crate :: types:: request:: Request ;
11+ use crate :: types:: cookie:: Cookies ;
1112use crate :: types:: response:: Response ;
1213use crate :: types:: HttpMethod ;
1314use anyhow:: Context ;
@@ -125,8 +126,20 @@ impl Router<Response, HttpMethod> for ConstRouter {
125126 Ok ( ( ) )
126127 }
127128
128- fn get_route ( & self , _route_method : & HttpMethod , _route : & str ) -> Option < Response > {
129- None
129+ fn get_route ( & self , route_method : & HttpMethod , route : & str ) -> Option < Response > {
130+ let cached = self . get_cached_route ( route_method, route) ?;
131+ let mut resp = Response {
132+ status_code : cached. status . as_u16 ( ) ,
133+ response_type : "text" . to_string ( ) ,
134+ headers : Headers :: new ( None ) ,
135+ description : cached. body . to_vec ( ) ,
136+ file_path : None ,
137+ cookies : Cookies :: new ( ) ,
138+ } ;
139+ for ( k, v) in cached. headers . as_ref ( ) {
140+ resp. headers . set ( k. clone ( ) , v. clone ( ) ) ;
141+ }
142+ Some ( resp)
130143 }
131144}
132145
@@ -167,25 +180,41 @@ impl ConstRouter {
167180 if extra_headers. is_empty ( ) {
168181 return ;
169182 }
170- for fast_table in self . fast_routes . values ( ) {
183+ for ( method , fast_table) in & self . fast_routes {
171184 let mut map = fast_table. write ( ) ;
172185 for cached in map. values_mut ( ) {
173186 let mut combined = cached. headers . as_ref ( ) . clone ( ) ;
174187 combined. extend ( extra_headers. iter ( ) . cloned ( ) ) ;
175188 cached. headers = Arc :: new ( combined) ;
176189 }
190+
191+ if let Some ( route_table) = self . routes . get ( method) {
192+ let mut new_router = MatchItRouter :: new ( ) ;
193+ for ( route, cached) in map. iter ( ) {
194+ let _ = new_router. insert ( route. clone ( ) , cached. clone ( ) ) ;
195+ }
196+ * route_table. write ( ) = new_router;
197+ }
177198 }
178199 }
179200
180- /// Ultra-fast lookup: flat HashMap, no matchit regex, no path-param extraction .
201+ /// Fast lookup: tries exact HashMap first, falls back to matchit for parameterized/wildcard routes .
181202 #[ inline( always) ]
182203 pub fn get_cached_route (
183204 & self ,
184205 route_method : & HttpMethod ,
185206 route : & str ,
186207 ) -> Option < CachedResponse > {
187- let table = self . fast_routes . get ( route_method) ?;
188- let map = table. read ( ) ;
189- map. get ( route) . cloned ( )
208+ let fast_table = self . fast_routes . get ( route_method) ?;
209+ {
210+ let map = fast_table. read ( ) ;
211+ if let Some ( cached) = map. get ( route) {
212+ return Some ( cached. clone ( ) ) ;
213+ }
214+ }
215+
216+ let route_table = self . routes . get ( route_method) ?;
217+ let router = route_table. read ( ) ;
218+ router. at ( route) . ok ( ) . map ( |matched| matched. value . clone ( ) )
190219 }
191220}
0 commit comments