Created
September 16, 2023 15:39
-
-
Save Oppodelldog/fc7483400408f21e27bb4280d3d3c53b to your computer and use it in GitHub Desktop.
run testserver for godot web exports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python test-server.py --root "C:GodotProjects\myGame\build\web" --file "mygame.html" | |
pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from http.server import HTTPServer, SimpleHTTPRequestHandler, test # type: ignore | |
from pathlib import Path | |
import os | |
import sys | |
import argparse | |
import subprocess | |
class CORSRequestHandler(SimpleHTTPRequestHandler): | |
def end_headers(self): | |
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() | |
def shell_open(url): | |
if sys.platform == "win32": | |
os.startfile(url) | |
else: | |
opener = "open" if sys.platform == "darwin" else "xdg-open" | |
subprocess.call([opener, url]) | |
def serve(root, port, run_browser,file): | |
os.chdir(root) | |
if run_browser: | |
# Open the served page in the user's default browser. | |
print("Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this).") | |
shell_open(f"http://localhost:{port}/{file}") | |
test(CORSRequestHandler, HTTPServer, port=port) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", "--port", help="port to listen on", default=8060, type=int) | |
parser.add_argument( | |
"-r", "--root", help="path to serve as root (relative to `platform/web/`)", default="../../bin", type=Path | |
) | |
parser.add_argument( | |
"-f", "--file", help="path to serve as root (relative to `platform/web/`)", default="mygame.html", type=Path | |
) | |
browser_parser = parser.add_mutually_exclusive_group(required=False) | |
browser_parser.add_argument( | |
"-n", "--no-browser", help="don't open default web browser automatically", dest="browser", action="store_false" | |
) | |
parser.set_defaults(browser=True) | |
args = parser.parse_args() | |
# Change to the directory where the script is located, | |
# so that the script can be run from any location. | |
os.chdir(Path(__file__).resolve().parent) | |
serve(args.root, args.port, args.browser, args.file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment