Skip to content
Open

GitHub #1140

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions internal/ghmcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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 {
Expand Down Expand Up @@ -395,6 +430,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()
}
Expand Down