Skip to content

Commit ea50f45

Browse files
committed
added a response to simplifly everything
1 parent 11ebadc commit ea50f45

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

gurgleapps_webserver.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import uos
77
import uasyncio as asyncio
88
import _thread
9-
9+
import ujson as json
10+
from response import Response
1011

1112
class GurgleAppsWebserver:
1213

@@ -74,6 +75,7 @@ async def serve_request(self, reader, writer):
7475
content_length = 0
7576
# Read the request line by line because we want the post data potentially
7677
headers = []
78+
post_data = None
7779
while True:
7880
line = await reader.readline()
7981
line = line.decode('utf-8').strip()
@@ -98,17 +100,18 @@ async def serve_request(self, reader, writer):
98100
content_length = int(match.group(1))
99101
print("content_length: "+str(content_length))
100102
# Read the POST data if there's any
101-
post_data = None
102103
if content_length > 0:
103-
post_data = await reader.readexactly(content_length)
104-
print("POST data:", post_data)
104+
post_data_raw = await reader.readexactly(content_length)
105+
print("POST data:", post_data_raw)
106+
post_data = json.loads(post_data_raw)
107+
response = Response(writer, post_data)
105108
# check if the url is a function route and if so run the function
106109
path_components = self.get_path_components(url)
107110
print("path_components: "+str(path_components))
108111
route_function, params = self.match_route(path_components)
109112
if route_function:
110113
print("calling function: "+str(route_function)+" with params: "+str(params))
111-
await route_function(writer, *params)
114+
await route_function(response, *params)
112115
return
113116
# perhaps it is a file
114117
file = self.get_file(self.doc_root + url)

main.py

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

12-
async def example_func(writer, param1, param2):
12+
async def example_func(response, param1, param2):
1313
print("example_func")
1414
print("param1: " + param1)
1515
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()
16+
response_string = json.dumps({"param1": param1, "param2": param2})
17+
await response.send("200 OK", "application/json", response_string)
18+
2119

2220

23-
async def send_status(writer):
24-
writer.write('HTTP/1.0 200 OK\r\nContent-type: application/json\r\n\r\n')
21+
async def send_status(response):
2522
# send boolean status and number frequency
26-
response = json.dumps({"status": status, "delay": delay})
27-
writer.write(response)
28-
await writer.drain()
29-
await writer.wait_closed()
23+
response_string = json.dumps({"status": status, "delay": delay})
24+
await response.send("200 OK", "application/json", response_string)
25+
3026

31-
async def set_delay(writer, new_delay):
27+
async def set_delay(response, new_delay):
3228
print("new delay: " + new_delay)
3329
global delay
3430
delay = float(new_delay)
35-
await send_status(writer)
31+
await send_status(response)
3632

37-
async def stop_flashing(writer):
33+
async def stop_flashing(response):
3834
global status
3935
status = False
40-
await send_status(writer)
36+
await send_status(response)
4137

42-
async def start_flashing(writer):
38+
async def start_flashing(response):
4339
global status
4440
status = True
45-
await send_status(writer)
41+
await send_status(response)
4642

4743
async def main():
4844
await server.start_server()

response.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# response.py
2+
3+
class Response:
4+
def __init__(self, writer, post_data=None):
5+
self.writer = writer
6+
self.post_data = post_data
7+
8+
async def send(self, status, content_type, content):
9+
self.writer.write(f'HTTP/1.0 {status}\r\nContent-type: {content_type}\r\n\r\n')
10+
self.writer.write(content)
11+
await self.writer.drain()
12+
await self.writer.wait_closed()

0 commit comments

Comments
 (0)