@@ -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
0 commit comments