From d978bc187b08ddc3ac3d1ebaa10d14562fabf8fe Mon Sep 17 00:00:00 2001 From: William Martin Date: Fri, 20 Jun 2025 21:15:37 +0200 Subject: [PATCH] Support localhost hosts --- internal/ghmcp/server.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/internal/ghmcp/server.go b/internal/ghmcp/server.go index ca38e76b..80b7e426 100644 --- a/internal/ghmcp/server.go +++ b/internal/ghmcp/server.go @@ -249,6 +249,41 @@ type apiHost struct { rawURL *url.URL } +// newLocalhostHost creates a new apiHost for localhost, which is used for development purposes. +func newLocalhostHost(hostname string) (apiHost, error) { + u, err := url.Parse(hostname) + if err != nil { + return apiHost{}, fmt.Errorf("failed to parse localhost URL: %w", err) + } + + restURL, err := url.Parse(fmt.Sprintf("%s://api.%s/", u.Scheme, u.Hostname())) + if err != nil { + return apiHost{}, fmt.Errorf("failed to parse localhost REST URL: %w", err) + } + + gqlURL, err := url.Parse(fmt.Sprintf("%s://api.%s/graphql", u.Scheme, u.Hostname())) + if err != nil { + return apiHost{}, fmt.Errorf("failed to parse localhost GraphQL URL: %w", err) + } + + uploadURL, err := url.Parse(fmt.Sprintf("%s://uploads.%s", u.Scheme, u.Hostname())) + if err != nil { + return apiHost{}, fmt.Errorf("failed to parse localhost Upload URL: %w", err) + } + + rawURL, err := url.Parse(fmt.Sprintf("%s://raw.%s/", u.Scheme, u.Hostname())) + if err != nil { + return apiHost{}, fmt.Errorf("failed to parse localhost Raw URL: %w", err) + } + + return apiHost{ + baseRESTURL: restURL, + graphqlURL: gqlURL, + uploadURL: uploadURL, + rawURL: rawURL, + }, nil +} + func newDotcomHost() (apiHost, error) { baseRestURL, err := url.Parse("https://api.github.com/") if err != nil { @@ -365,6 +400,10 @@ func parseAPIHost(s string) (apiHost, error) { return apiHost{}, fmt.Errorf("host must have a scheme (http or https): %s", s) } + if strings.HasSuffix(u.Hostname(), "localhost") { + return newLocalhostHost(s) + } + if strings.HasSuffix(u.Hostname(), "github.com") { return newDotcomHost() }