Skip to content

Commit 4b1c3ed

Browse files
committed
moved from regulard expressions to <>
1 parent 7a44227 commit 4b1c3ed

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

gurgleapps_webserver.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

main.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
status = True
1010
led = Pin("LED", Pin.OUT)
1111

12+
async def example_func(writer, param1, param2):
13+
print("example_func")
14+
print("param1: " + param1)
15+
print("param2: " + param2)
16+
writer.write('HTTP/1.0 200 OK\r\nContent-type: application/json\r\n\r\n')
17+
response = json.dumps({"param1": param1, "param2": param2})
18+
writer.write(response)
19+
await writer.drain()
20+
await writer.wait_closed()
21+
22+
1223
async def send_status(writer):
1324
writer.write('HTTP/1.0 200 OK\r\nContent-type: application/json\r\n\r\n')
1425
# send boolean status and number frequency
@@ -44,14 +55,22 @@ async def background_task():
4455
led.toggle()
4556
else:
4657
led.off()
58+
4759

4860
async def run():
4961
await asyncio.gather(main(), background_task())
5062

5163
server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD)
52-
server.add_function_route("^/set-delay/(\d+(?:\.\d+)?)$", set_delay)
64+
server.add_function_route("/set-delay/<delay>", set_delay)
65+
server.add_function_route("/stop", stop_flashing)
66+
server.add_function_route("/start", start_flashing)
67+
server.add_function_route("/status", send_status)
68+
server.add_function_route("/example/func/<param1>/<param2>", example_func)
69+
70+
""" server.add_function_route("^/set-delay/(\d+(?:\.\d+)?)$", set_delay) #23.78 2 23 float
5371
server.add_function_route("^/stop$", stop_flashing)
5472
server.add_function_route("^/start$", start_flashing)
55-
server.add_function_route("^/status$", send_status)
73+
server.add_function_route("^/status$", send_status) """
74+
5675

5776
asyncio.run(run())

0 commit comments

Comments
 (0)