danbev commited on
Commit
0fdd602
·
unverified ·
1 Parent(s): d53c30c

examples : update server.py to match github pages app [no ci] (#3004)

Browse files

This commit updates examples/server.py which is used to serve the wasm
examples locally. The changes include:

- Added a redirect from the root URL to /whisper.cpp.
So now accessing http://localhost:8000/ will redirect to
http://localhost:8000/whisper.cpp/ which matches the url for the app
deployed to github pages.

- Custom handling for coi-serviceworker.js to serve it to avoid
and error in the console. This file is not strictly necessary
for the local server to work as the headers are provided already but
it is nice to not have an error in the console.

- Fixed the shutdown of the server to ensure it exits cleanly
on Ctrl+C. Previously it would continue to hang onto the port even
after the processed had exited.

Files changed (1) hide show
  1. examples/server.py +85 -9
examples/server.py CHANGED
@@ -1,39 +1,115 @@
1
  import http.server
2
  import socketserver
3
  import os
 
4
  from pathlib import Path
 
5
 
6
  SCRIPT_DIR = Path(__file__).parent.absolute()
7
  DIRECTORY = os.path.join(SCRIPT_DIR, "../build-em/bin")
8
  DIRECTORY = os.path.abspath(DIRECTORY)
9
 
 
 
 
10
  class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
11
  def __init__(self, *args, **kwargs):
12
  super().__init__(*args, directory=DIRECTORY, **kwargs)
13
 
14
  def do_GET(self):
15
- # If requesting a worker file from any subdirectory
16
- if '.worker.js' in self.path:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  worker_file = os.path.basename(self.path)
18
  worker_path = os.path.join(DIRECTORY, worker_file)
19
 
20
  if os.path.exists(worker_path):
21
  self.path = '/' + worker_file
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  return super().do_GET()
24
 
25
  def end_headers(self):
26
  # Add required headers for SharedArrayBuffer
27
  self.send_header("Cross-Origin-Opener-Policy", "same-origin")
28
  self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
29
- self.send_header("Access-Control-Allow-Origin", "*");
30
  super().end_headers()
31
 
32
  PORT = 8000
33
 
34
- with socketserver.TCPServer(("", PORT), CustomHTTPRequestHandler) as httpd:
35
- print(f"Serving directory '{DIRECTORY}' at http://localhost:{PORT}")
36
- try:
37
- httpd.serve_forever()
38
- except KeyboardInterrupt:
39
- print("\nServer stopped.")
 
 
 
 
 
 
 
 
 
 
 
 
1
  import http.server
2
  import socketserver
3
  import os
4
+ import sys
5
  from pathlib import Path
6
+ import urllib.parse
7
 
8
  SCRIPT_DIR = Path(__file__).parent.absolute()
9
  DIRECTORY = os.path.join(SCRIPT_DIR, "../build-em/bin")
10
  DIRECTORY = os.path.abspath(DIRECTORY)
11
 
12
+ # The context root we want for all applications
13
+ CONTEXT_ROOT = "/whisper.cpp"
14
+
15
  class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
16
  def __init__(self, *args, **kwargs):
17
  super().__init__(*args, directory=DIRECTORY, **kwargs)
18
 
19
  def do_GET(self):
20
+ # Redirect root to the context root
21
+ if self.path == '/':
22
+ self.send_response(302)
23
+ self.send_header('Location', CONTEXT_ROOT + '/')
24
+ self.end_headers()
25
+ return
26
+
27
+ # Handle requests under the context root
28
+ if self.path.startswith(CONTEXT_ROOT):
29
+ # Remove the context root prefix to get the actual path
30
+ actual_path = self.path[len(CONTEXT_ROOT):]
31
+
32
+ if not actual_path:
33
+ self.send_response(302)
34
+ self.send_header('Location', CONTEXT_ROOT + '/')
35
+ self.end_headers()
36
+ return
37
+
38
+ if '.worker.js' in actual_path:
39
+ worker_file = os.path.basename(actual_path)
40
+ worker_path = os.path.join(DIRECTORY, worker_file)
41
+
42
+ if os.path.exists(worker_path):
43
+ print(f"Found worker file: {worker_path}")
44
+ self.path = '/' + worker_file
45
+ else:
46
+ print(f"Worker file not found: {worker_path}")
47
+
48
+ elif actual_path == '/':
49
+ self.path = '/whisper.wasm/index.html'
50
+ elif actual_path.startswith('/bench.wasm/') or actual_path.startswith('/command.wasm/') or actual_path.startswith('/stream.wasm/'):
51
+ # Keep the path as is, just remove the context root
52
+ self.path = actual_path
53
+ # For all other paths under the context root
54
+ else:
55
+ # Check if this is a request to a file in whisper.wasm
56
+ potential_file = os.path.join(DIRECTORY, 'whisper.wasm', actual_path.lstrip('/'))
57
+ if os.path.exists(potential_file) and not os.path.isdir(potential_file):
58
+ self.path = '/whisper.wasm' + actual_path
59
+ else:
60
+ # Try to resolve the file from the base directory
61
+ potential_file = os.path.join(DIRECTORY, actual_path.lstrip('/'))
62
+ if os.path.exists(potential_file):
63
+ self.path = actual_path
64
+
65
+ # For direct requests to worker files (without context root as these
66
+ # are in the build-em/bin directory
67
+ elif '.worker.js' in self.path:
68
  worker_file = os.path.basename(self.path)
69
  worker_path = os.path.join(DIRECTORY, worker_file)
70
 
71
  if os.path.exists(worker_path):
72
  self.path = '/' + worker_file
73
 
74
+ # Handle coi-serviceworker.js separately
75
+ if 'coi-serviceworker.js' in self.path:
76
+ worker_file = "coi-serviceworker.js"
77
+ worker_path = os.path.join(SCRIPT_DIR, worker_file)
78
+ if os.path.exists(worker_path):
79
+ self.send_response(200)
80
+ self.send_header('Content-type', 'application/javascript')
81
+ self.end_headers()
82
+ with open(worker_path, 'rb') as file:
83
+ self.wfile.write(file.read())
84
+ return
85
+ else:
86
+ print(f"Warning: Could not find {worker_path}")
87
+
88
  return super().do_GET()
89
 
90
  def end_headers(self):
91
  # Add required headers for SharedArrayBuffer
92
  self.send_header("Cross-Origin-Opener-Policy", "same-origin")
93
  self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
94
+ self.send_header("Access-Control-Allow-Origin", "*")
95
  super().end_headers()
96
 
97
  PORT = 8000
98
 
99
+ # Enable address reuse
100
+ class CustomServer(socketserver.TCPServer):
101
+ allow_reuse_address = True
102
+
103
+ try:
104
+ with CustomServer(("", PORT), CustomHTTPRequestHandler) as httpd:
105
+ print(f"Serving directory '{DIRECTORY}' at http://localhost:{PORT}")
106
+ print(f"Application context root: http://localhost:{PORT}{CONTEXT_ROOT}/")
107
+ try:
108
+ httpd.serve_forever()
109
+ except KeyboardInterrupt:
110
+ print("\nServer stopped.")
111
+ # Force complete exit
112
+ sys.exit(0)
113
+ except OSError as e:
114
+ print(f"Error: {e}")
115
+ sys.exit(1)