@@ -79,16 +79,13 @@ async def serve_request(self, reader, writer):
7979 url = match .group (1 )
8080 print (url )
8181 # check if the url is a function route and if so run the function
82- for route in self .function_routes :
83- route_pattern = re .compile (route ["route" ])
84- match = route_pattern .match (url )
85- if match :
86- # Extract the parameters from the URL
87- params = match .groups ()
88- # Pass the parameters to the corresponding function
89- print ("calling function: " + str (route ["function" ])+ ", params: " + str (params ))
90- await route ["function" ](writer , * params )
91- return
82+ path_components = self .get_path_components (url )
83+ print ("path_components: " + str (path_components ))
84+ route_function , params = self .match_route (path_components )
85+ if route_function :
86+ print ("calling function: " + str (route_function )+ " with params: " + str (params ))
87+ await route_function (writer , * params )
88+ return
9289 # perhaps it is a file
9390 file = self .get_file (self .doc_root + url )
9491 print ("file: " + str (file ))
@@ -127,3 +124,30 @@ def get_file(self, filename):
127124 # print the error
128125 print (e )
129126 return False
127+
128+ def get_path_components (self , path ):
129+ return tuple (filter (None , path .split ('/' )))
130+
131+ def match_route (self , path_components ):
132+ for route in self .function_routes :
133+ route_pattern = list (filter (None , route ["route" ].split ("/" )))
134+ print ("route_pattern: " + str (route_pattern ))
135+ if len (route_pattern ) != len (path_components ):
136+ continue
137+ match = True
138+ params = []
139+ for idx , pattern_component in enumerate (route_pattern ):
140+ print ("pattern_component: " + pattern_component + " path_component: " + path_components [idx ])
141+ if pattern_component .startswith ('<' ) and pattern_component .endswith ('>' ):
142+ param_value = path_components [idx ]
143+ params .append (param_value )
144+ else :
145+ if pattern_component != path_components [idx ]:
146+ match = False
147+ break
148+ if match :
149+ return route ["function" ], params
150+ return None , []
151+
152+
153+
0 commit comments