44from pathlib import Path
55from urllib .parse import urlparse
66
7+
78# Check if running on Windows.
89def _is_windows () -> bool :
910 return os .name == "nt"
1011
12+
1113# Return executable name, appending .exe on Windows.
1214def _bin_name (base : str ) -> str :
1315 return f"{ base } .exe" if _is_windows () else base
1416
17+
1518# Check if Go is installed and available in PATH.
1619def check_go_installed ():
1720 try :
@@ -24,32 +27,33 @@ def check_go_installed():
2427 except Exception as e :
2528 raise RuntimeError ("Go is not installed or not found in PATH." ) from e
2629
30+
2731# Build the Go proxy binary from source.
2832def build_proxy (go_dir : Path , binary_path : Path ):
2933 print ("Building Go proxy ..." )
3034 if not (go_dir / "go.mod" ).exists ():
3135 raise FileNotFoundError (f"Missing go.mod in { go_dir } " )
3236
3337 cmd = [
34- "go" , "build" ,
38+ "go" ,
39+ "build" ,
3540 "-trimpath" ,
36- "-ldflags" , "-s -w" ,
37- "-o" , binary_path .name ,
41+ "-ldflags" ,
42+ "-s -w" ,
43+ "-o" ,
44+ binary_path .name ,
3845 "." ,
3946 ]
4047 result = subprocess .run (
41- cmd ,
42- cwd = go_dir ,
43- stdout = subprocess .PIPE ,
44- stderr = subprocess .STDOUT ,
45- text = True
48+ cmd , cwd = go_dir , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True
4649 )
4750 if result .returncode != 0 :
4851 raise RuntimeError (f"Failed to build Go proxy:\n { result .stdout } " )
4952
5053 if not _is_windows ():
5154 binary_path .chmod (0o755 )
5255
56+
5357# Check if the Go binary is missing or older than source files.
5458def is_rebuild_needed (go_dir : Path , binary_file : Path ) -> bool :
5559 if not binary_file .exists ():
@@ -65,12 +69,13 @@ def is_rebuild_needed(go_dir: Path, binary_file: Path) -> bool:
6569 return True
6670 return False
6771
72+
6873# Run the Go proxy, rebuilding if needed.
6974def run_proxy (target = "http://localhost:4000" , port = 8080 , ** kwargs ):
7075 # Validate target URL
7176 try :
7277 parsed = urlparse (target )
73- if parsed .scheme not in (' http' , ' https' ):
78+ if parsed .scheme not in (" http" , " https" ):
7479 raise ValueError (f"Target must use http or https scheme, got: { target } " )
7580 if not parsed .netloc :
7681 raise ValueError (f"Invalid target URL (missing host): { target } " )
@@ -116,7 +121,8 @@ def run_proxy(target="http://localhost:4000", port=8080, **kwargs):
116121 if "tls_key_file" in kwargs :
117122 env ["PROXY_TLS_KEY_FILE" ] = str (kwargs ["tls_key_file" ])
118123 if "cors_allow_credentials" in kwargs :
119- env ["PROXY_CORS_ALLOW_CREDENTIALS" ] = str (kwargs ["cors_allow_credentials" ]).lower ()
124+ val = str (kwargs ["cors_allow_credentials" ]).lower ()
125+ env ["PROXY_CORS_ALLOW_CREDENTIALS" ] = val
120126
121127 print (f"Starting Go proxy at http://localhost:{ port } -> { target } " )
122128
@@ -127,7 +133,7 @@ def run_proxy(target="http://localhost:4000", port=8080, **kwargs):
127133 env = env ,
128134 stdout = subprocess .PIPE ,
129135 stderr = subprocess .STDOUT ,
130- text = True
136+ text = True ,
131137 )
132138
133139 # Log output from the proxy process in a background thread.
0 commit comments