Skip to content

Commit ede9296

Browse files
authored
Merge pull request #98 from golf-mcp/asch/remove-platform
Asch/remove platform
2 parents cff4727 + 11e28de commit ede9296

File tree

9 files changed

+143
-14
lines changed

9 files changed

+143
-14
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ dist/
1111
Makefile
1212
htmlcov/
1313
.ruff_cache/
14-
new/
14+
new/
15+
16+
# Generated endpoints file (created by editable install)
17+
src/golf/_endpoints.py
18+
build.sh

setup.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""Custom setup.py for golf-mcp with URL injection."""
2+
3+
from setuptools import setup
4+
from setuptools.command.build_py import build_py as _build_py
5+
from setuptools.command.develop import develop as _develop
6+
import os
7+
import pathlib
8+
import sys
9+
10+
TEMPLATE_REL = "src/golf/_endpoints.py.in"
11+
PACKAGE_OUT_REL = "golf/_endpoints.py"
12+
13+
14+
def render_endpoints(require_env_vars: bool = True):
15+
"""Render the endpoints template with environment variables."""
16+
tpl_path = pathlib.Path(TEMPLATE_REL)
17+
if not tpl_path.exists():
18+
raise FileNotFoundError(f"Template not found: {tpl_path}")
19+
20+
# Get environment variables
21+
platform_url = os.environ.get("GOLF_PLATFORM_API_URL")
22+
otel_url = os.environ.get("GOLF_OTEL_ENDPOINT")
23+
24+
# For production builds, require environment variables
25+
# For development/editable installs, use fallback values
26+
if require_env_vars and (not platform_url or not otel_url):
27+
raise SystemExit(
28+
"Missing required environment variables for URL injection:\n"
29+
" GOLF_PLATFORM_API_URL\n"
30+
" GOLF_OTEL_ENDPOINT\n"
31+
"Set these before building the package."
32+
)
33+
34+
# Use environment variables if available, otherwise fallback to development URLs
35+
values = {
36+
"PLATFORM_API_URL": platform_url or "http://localhost:8000/api/resources",
37+
"OTEL_ENDPOINT": otel_url or "http://localhost:4318/v1/traces",
38+
}
39+
40+
try:
41+
rendered = tpl_path.read_text(encoding="utf-8").format(**values)
42+
except KeyError as e:
43+
raise SystemExit(f"Missing template key: {e}") from e
44+
45+
return rendered
46+
47+
48+
class build_py(_build_py):
49+
"""Custom build_py that renders endpoints into the build_lib (wheel contents)."""
50+
51+
def run(self):
52+
# First run the normal build
53+
super().run()
54+
55+
# Then render endpoints into the build_lib
56+
# Skip env var requirement if in CI environment or if this looks like a test install
57+
is_ci = os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS")
58+
rendered = render_endpoints(require_env_vars=not is_ci)
59+
out_file = pathlib.Path(self.build_lib) / PACKAGE_OUT_REL
60+
out_file.parent.mkdir(parents=True, exist_ok=True)
61+
out_file.write_text(rendered, encoding="utf-8")
62+
print(f"Generated {PACKAGE_OUT_REL} with injected URLs", file=sys.stderr)
63+
64+
65+
class develop(_develop):
66+
"""Custom develop for editable installs - generates endpoints file in source tree."""
67+
68+
def run(self):
69+
# Run normal develop command first
70+
super().run()
71+
72+
# Generate a working copy file for editable installs (use fallback URLs if env vars missing)
73+
rendered = render_endpoints(require_env_vars=False)
74+
# For editable installs, write into the source tree so imports work
75+
src_file = pathlib.Path("src") / PACKAGE_OUT_REL
76+
src_file.parent.mkdir(parents=True, exist_ok=True)
77+
src_file.write_text(rendered, encoding="utf-8")
78+
print(f"Generated dev-time {PACKAGE_OUT_REL} in source tree", file=sys.stderr)
79+
print(f"Note: {src_file} is gitignored and should not be committed", file=sys.stderr)
80+
81+
82+
setup(
83+
cmdclass={
84+
"build_py": build_py,
85+
"develop": develop,
86+
}
87+
)

src/golf/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
__version__ = "0.2.0"
2+
3+
# Import endpoints with fallback for dev mode
4+
try:
5+
# In built wheels, this exists (generated from _endpoints.py.in)
6+
from . import _endpoints
7+
except ImportError:
8+
# In editable/dev installs, fall back to env-based values
9+
from . import _endpoints_fallback as _endpoints

src/golf/_endpoints.py.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Auto-generated at build time by setup.py:build_py
2+
# This template contains placeholders that are replaced during build
3+
4+
# Platform endpoints
5+
PLATFORM_API_URL = "{PLATFORM_API_URL}"
6+
OTEL_ENDPOINT = "{OTEL_ENDPOINT}"

src/golf/_endpoints_fallback.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Fallback endpoints for development/editable installs."""
2+
3+
import os
4+
5+
# These are used when the generated _endpoints.py doesn't exist (dev mode)
6+
# or when environment variables override the built-in values
7+
8+
PLATFORM_API_URL = os.getenv("GOLF_PLATFORM_API_URL", "http://localhost:8000/api/resources")
9+
10+
OTEL_ENDPOINT = os.getenv("GOLF_OTEL_ENDPOINT", "http://localhost:4318/v1/traces")

src/golf/core/platform.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
from golf.core.config import Settings
1313
from golf.core.parser import ComponentType, ParsedComponent
1414

15+
# Import endpoints with fallback for dev mode
16+
try:
17+
# In built wheels, this exists (generated from _endpoints.py.in)
18+
from golf import _endpoints # type: ignore
19+
except ImportError:
20+
# In editable/dev installs, fall back to env-based values
21+
from golf import _endpoints_fallback as _endpoints # type: ignore
22+
1523
console = Console()
1624

1725

@@ -65,7 +73,7 @@ async def register_project_with_platform(
6573
try:
6674
async with httpx.AsyncClient(timeout=10.0) as client:
6775
response = await client.post(
68-
"https://golf-backend.golf-auth-1.authed-qukc4.ryvn.run/api/resources",
76+
_endpoints.PLATFORM_API_URL,
6977
json=metadata,
7078
headers={
7179
"X-Golf-Key": api_key,

src/golf/telemetry/instrumentation.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
from collections import OrderedDict
1414

1515
from opentelemetry import baggage, trace
16+
17+
# Import endpoints with fallback for dev mode
18+
try:
19+
# In built wheels, this exists (generated from _endpoints.py.in)
20+
from golf import _endpoints # type: ignore
21+
except ImportError:
22+
# In editable/dev installs, fall back to env-based values
23+
from golf import _endpoints_fallback as _endpoints # type: ignore
1624
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
1725
from opentelemetry.sdk.resources import Resource
1826
from opentelemetry.sdk.trace import TracerProvider
@@ -70,9 +78,7 @@ def init_telemetry(service_name: str = "golf-mcp-server") -> TracerProvider | No
7078

7179
# Only set endpoint if not already configured (allow user override)
7280
if not os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT"):
73-
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = (
74-
"https://golf-backend.golf-auth-1.authed-qukc4.ryvn.run/api/v1/otel"
75-
)
81+
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = _endpoints.OTEL_ENDPOINT
7682

7783
# Set Golf platform headers (append to existing if present)
7884
existing_headers = os.environ.get("OTEL_EXPORTER_OTLP_HEADERS", "")

tests/core/test_instrumentation.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ def test_init_telemetry_with_golf_platform_auto_config(self, monkeypatch):
5555
provider = init_telemetry("test-service")
5656
assert provider is not None
5757
assert os.environ.get("OTEL_TRACES_EXPORTER") == "otlp_http"
58-
assert (
59-
os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT")
60-
== "https://golf-backend.golf-auth-1.authed-qukc4.ryvn.run/api/v1/otel"
61-
)
58+
from golf import _endpoints
59+
60+
assert os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") == _endpoints.OTEL_ENDPOINT
6261

6362
def test_init_telemetry_with_headers(self, monkeypatch):
6463
"""Test telemetry initialization with custom headers."""
@@ -337,10 +336,9 @@ def test_golf_platform_integration_workflow(self, monkeypatch):
337336

338337
# Verify auto-configuration
339338
assert os.environ.get("OTEL_TRACES_EXPORTER") == "otlp_http"
340-
assert (
341-
os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT")
342-
== "https://golf-backend.golf-auth-1.authed-qukc4.ryvn.run/api/v1/otel"
343-
)
339+
from golf import _endpoints
340+
341+
assert os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") == _endpoints.OTEL_ENDPOINT
344342
assert "X-Golf-Key=golf_test_key_123" in os.environ.get("OTEL_EXPORTER_OTLP_HEADERS", "")
345343

346344
def test_mixed_component_instrumentation(self):

tests/core/test_platform.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def test_function() -> Output:
6767
# Verify the HTTP request was made correctly
6868
mock_context.post.assert_called_once()
6969
call_args = mock_context.post.call_args
70-
assert call_args.args[0] == "https://golf-backend.golf-auth-1.authed-qukc4.ryvn.run/api/resources"
70+
from golf import _endpoints
71+
72+
assert call_args.args[0] == _endpoints.PLATFORM_API_URL
7173

7274
# Verify request headers
7375
headers = call_args.kwargs["headers"]

0 commit comments

Comments
 (0)