File: server.py

package info (click to toggle)
ggml 0.9.5-6
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 19,176 kB
  • sloc: cpp: 120,570; ansic: 44,624; lisp: 10,053; python: 1,591; objc: 1,349; sh: 827; makefile: 73
file content (36 lines) | stat: -rw-r--r-- 1,200 bytes parent folder | download | duplicates (5)
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
import http.server
import socketserver
import os
import sys

DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), 'web'))
PORT = 8000

class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=DIRECTORY, **kwargs)

    def end_headers(self):
        # Add required headers for SharedArrayBuffer
        self.send_header("Cross-Origin-Opener-Policy", "same-origin")
        self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
        self.send_header("Access-Control-Allow-Origin", "*")
        super().end_headers()

# Enable address reuse
class CustomServer(socketserver.TCPServer):
    allow_reuse_address = True

try:
    with CustomServer(("", PORT), CustomHTTPRequestHandler) as httpd:
        print(f"Serving directory '{DIRECTORY}' at http://localhost:{PORT}")
        print(f"Application context root: http://localhost:{PORT}/")
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\nServer stopped.")
            # Force complete exit
            sys.exit(0)
except OSError as e:
    print(f"Error: {e}")
    sys.exit(1)