Skip to content

Commit eadb566

Browse files
committed
no os.path in micropython uh oh
1 parent d692ddf commit eadb566

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
WIFI_SSID = "your_wifi_ssid"
2-
WIFI_PASSWORD = "your_wifi_password"
2+
WIFI_PASSWORD = "your_wifi_password"
3+
BLINK_IP = False
4+
BLINK_LAST_ONLY = False

gurgleapps_webserver.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,13 @@ async def serve_request(self, reader, writer):
153153
print("content_type: "+str(content_type))
154154
await response.send_file(file_path, content_type=content_type)
155155
return
156-
print("file not found")
156+
if url == "/":
157+
print("root")
158+
files_and_folders = self.list_files_and_folders(self.doc_root)
159+
html = self.generate_root_page_html(files_and_folders)
160+
await response.send(html)
161+
return
162+
print("file not found "+url)
157163
await response.send(self.html % "page not found "+url, status_code=404)
158164
if (url == "/shutdown"):
159165
self.serving = False
@@ -294,3 +300,41 @@ def blink_element(element, pin, duration=0.27):
294300
await asyncio.sleep(delay_between_digits if element != '.' else 2 * delay_between_digits)
295301
await asyncio.sleep(delay_between_repititions)
296302

303+
def list_files_and_folders(self, path):
304+
files_and_folders = os.listdir(path)
305+
return files_and_folders
306+
307+
def generate_root_page_html(self, files_and_folders):
308+
folder_icon_svg = """
309+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="inline-block w-6 h-6">
310+
<path d="M4 4a2 2 0 00-2 2v8a2 2 0 002 2h12a2 2 0 002-2V8a2 2 0 00-2-2H9.586A2 2 0 018 6H4z" />
311+
</svg>
312+
"""
313+
file_icon_svg = """
314+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="inline-block w-6 h-6">
315+
<path d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0016.414 6L13 2.586A2 2 0 0011.414 2H6z" />
316+
</svg>
317+
"""
318+
file_list_html = "<ul class='list-none'>"
319+
for file_or_folder in files_and_folders:
320+
icon = folder_icon_svg if os.path.isdir(os.path.join("www", file_or_folder)) else file_icon_svg
321+
file_list_html += f"<li class='my-2'><a href='/{file_or_folder}' class='text-blue-600 hover:text-blue-800'>{icon} {file_or_folder}</a></li>"
322+
file_list_html += "</ul>"
323+
324+
html_content = f"""
325+
<!DOCTYPE html>
326+
<html>
327+
<head>
328+
<title>GurgleApps.com Webserver</title>
329+
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
330+
</head>
331+
<body class="bg-gray-100">
332+
<div class="container mx-auto p-8">
333+
<h1 class="text-3xl font-bold mb-4">Welcome to GurgleApps.com Webserver</h1>
334+
<h2 class="text-2xl mb-2">File List:</h2>
335+
{file_list_html}
336+
</div>
337+
</body>
338+
</html>
339+
"""
340+
return html_content

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def background_task():
8888

8989
async def run():
9090
#await(server.blink_ip(led_pin=led))
91-
await(server.blink_ip(led_pin = led, last_only = True))
91+
if config.BLINK_IP:
92+
await(server.blink_ip(led_pin = led, last_only = config.BLINK_LAST_ONLY))
93+
9294
await asyncio.gather(main(), background_task())
9395

9496
server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD, port=80, timeout=20, doc_root="/www", log_level=2)

0 commit comments

Comments
 (0)