Skip to content

Commit ecbe100

Browse files
committed
using request and response seperation of concerns
1 parent ea50f45 commit ecbe100

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

gurgleapps_webserver.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import _thread
99
import ujson as json
1010
from response import Response
11+
from request import Request
1112

1213
class GurgleAppsWebserver:
1314

@@ -82,20 +83,18 @@ async def serve_request(self, reader, writer):
8283
if line == "":
8384
break
8485
headers.append(line)
85-
86-
request = "\r".join(headers)
87-
print(request)
88-
request = str(request)
86+
request_raw = str("\r".join(headers))
87+
print(request_raw)
8988
request_pattern = re.compile(r"(GET|POST)\s+([^\s]+)\s+HTTP")
90-
match = request_pattern.search(request)
89+
match = request_pattern.search(request_raw)
9190
if match:
9291
method = match.group(1)
9392
url = match.group(2)
9493
print(method, url)
9594
# extract content length for POST requests
9695
if method == "POST":
9796
content_length_pattern = re.compile(r"Content-Length:\s+(\d+)")
98-
match = content_length_pattern.search(request)
97+
match = content_length_pattern.search(request_raw)
9998
if match:
10099
content_length = int(match.group(1))
101100
print("content_length: "+str(content_length))
@@ -104,14 +103,15 @@ async def serve_request(self, reader, writer):
104103
post_data_raw = await reader.readexactly(content_length)
105104
print("POST data:", post_data_raw)
106105
post_data = json.loads(post_data_raw)
107-
response = Response(writer, post_data)
106+
request = Request(post_data)
107+
response = Response(writer)
108108
# check if the url is a function route and if so run the function
109109
path_components = self.get_path_components(url)
110110
print("path_components: "+str(path_components))
111111
route_function, params = self.match_route(path_components)
112112
if route_function:
113113
print("calling function: "+str(route_function)+" with params: "+str(params))
114-
await route_function(response, *params)
114+
await route_function(request, response, *params)
115115
return
116116
# perhaps it is a file
117117
file = self.get_file(self.doc_root + url)

main.py

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

12-
async def example_func(response, param1, param2):
12+
async def example_func(request, response, param1, param2):
1313
print("example_func")
1414
print("param1: " + param1)
1515
print("param2: " + param2)
16-
response_string = json.dumps({"param1": param1, "param2": param2})
16+
response_string = json.dumps({ "param1": param1, "param2": param2, "post_data": request.post_data})
1717
await response.send("200 OK", "application/json", response_string)
1818

1919

2020

21-
async def send_status(response):
21+
async def send_status(request, response):
2222
# send boolean status and number frequency
2323
response_string = json.dumps({"status": status, "delay": delay})
2424
await response.send("200 OK", "application/json", response_string)
2525

2626

27-
async def set_delay(response, new_delay):
27+
async def set_delay(request, response, new_delay):
2828
print("new delay: " + new_delay)
2929
global delay
3030
delay = float(new_delay)
31-
await send_status(response)
31+
await send_status(request, response)
3232

33-
async def stop_flashing(response):
33+
async def stop_flashing(request, response):
3434
global status
3535
status = False
36-
await send_status(response)
36+
await send_status(request, response)
3737

38-
async def start_flashing(response):
38+
async def start_flashing(request, response):
3939
global status
4040
status = True
41-
await send_status(response)
41+
await send_status(request, response)
4242

4343
async def main():
4444
await server.start_server()
@@ -63,10 +63,4 @@ async def run():
6363
server.add_function_route("/status", send_status)
6464
server.add_function_route("/example/func/<param1>/<param2>", example_func)
6565

66-
""" server.add_function_route("^/set-delay/(\d+(?:\.\d+)?)$", set_delay) #23.78 2 23 float
67-
server.add_function_route("^/stop$", stop_flashing)
68-
server.add_function_route("^/start$", start_flashing)
69-
server.add_function_route("^/status$", send_status) """
70-
71-
7266
asyncio.run(run())

request.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# request.py
2+
3+
class Request:
4+
def __init__(self, post_data=None):
5+
self.post_data = post_data

response.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# response.py
22

33
class Response:
4-
def __init__(self, writer, post_data=None):
4+
def __init__(self, writer):
55
self.writer = writer
6-
self.post_data = post_data
76

87
async def send(self, status, content_type, content):
98
self.writer.write(f'HTTP/1.0 {status}\r\nContent-type: {content_type}\r\n\r\n')
109
self.writer.write(content)
1110
await self.writer.drain()
12-
await self.writer.wait_closed()
11+
await self.writer.wait_closed()

0 commit comments

Comments
 (0)