File size: 4,060 Bytes
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import argparse
import webbrowser
import logging
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
from urllib.parse import urlparse

class ChatBotHandler(SimpleHTTPRequestHandler):
    def log_message(self, format, *args):
        # Suppress logging for Chrome DevTools and other browser noise
        if (len(args) > 0 and 
            ('.well-known' in str(args) or 
             'favicon.ico' in str(args) or
             'apple-touch-icon' in str(args) or
             'robots.txt' in str(args) or
             'sitemap.xml' in str(args))):
            return
        # Log other requests normally
        super().log_message(format, *args)
    
    def do_GET(self):
        # Parse the URL path
        parsed_path = urlparse(self.path)
        path = parsed_path.path
        
        # Handle Chrome DevTools and other browser requests silently
        if (path.startswith('/.well-known/') or 
            path.startswith('/favicon.ico') or 
            path.startswith('/apple-touch-icon') or
            path.startswith('/robots.txt') or
            path.startswith('/sitemap.xml')):
            self.send_response(404)
            self.end_headers()
            return
        
        # Handle routing for SPA
        if path == '/':
            # Default to the new landing page
            self.serve_file('landing.html')
        elif path == '/landing' or path == '/landing.html':
            self.serve_file('landing.html')
        elif path == '/index.html':
            self.serve_file('index.html')
        elif path == '/login':
            self.serve_file('login.html')
        elif path == '/register':
            self.serve_file('register.html')
        elif path == '/admin_login.html':
            self.serve_file('admin_login.html')
        elif path == '/admin_dashboard.html':
            self.serve_file('admin_dashboard.html')
        elif path == '/professional_login.html':
            self.serve_file('professional_login.html')
        elif path == '/professional_dashboard.html':
            self.serve_file('professional_dashboard.html')
        elif path.startswith('/') and '.' in path:
            # Static file request (css, js, etc.)
            super().do_GET()
        else:
            # Fallback to index.html for SPA routing
            self.serve_file('index.html')
    
    def serve_file(self, filename):
        """Serve a specific HTML file"""
        try:
            if os.path.exists(filename):
                self.path = '/' + filename
                super().do_GET()
            else:
                self.send_error(404, f"File not found: {filename}")
        except Exception as e:
            self.send_error(500, f"Server error: {str(e)}")

def run_server(port: int):
    base_dir = os.path.dirname(os.path.abspath(__file__))
    chatbot_dir = os.path.join(base_dir, "chatbot")
    if not os.path.isdir(chatbot_dir):
        print("ERROR: chatbot/ directory not found. Create c:\\aimhsa-rag\\chatbot with index.html, style.css, app.js")
        return
    
    # Change to chatbot directory so files are served correctly
    os.chdir(chatbot_dir)
    
    addr = ("", port)
    httpd = ThreadingHTTPServer(addr, ChatBotHandler)
    url = f"http://localhost:{port}/"
    print(f"Serving frontend at {url} (serving directory: {chatbot_dir})")
    print("Routes available:")
    print(f"  - {url} (landing)")
    print(f"  - {url}landing (landing page)")
    print(f"  - {url}login (login page)")
    print(f"  - {url}register (register page)")
    
    try:
        webbrowser.open(url)
    except Exception:
        pass
    
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("Shutting down server...")
        httpd.server_close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Serve the chatbot frontend with proper routing.")
    parser.add_argument("--port", "-p", type=int, default=8000, help="Port to serve the frontend on (default: 8000)")
    args = parser.parse_args()
    run_server(args.port)