Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d978bc1

Browse files
committed
Support localhost hosts
1 parent 3fe88ee commit d978bc1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

internal/ghmcp/server.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,41 @@ type apiHost struct {
249249
rawURL *url.URL
250250
}
251251

252+
// newLocalhostHost creates a new apiHost for localhost, which is used for development purposes.
253+
func newLocalhostHost(hostname string) (apiHost, error) {
254+
u, err := url.Parse(hostname)
255+
if err != nil {
256+
return apiHost{}, fmt.Errorf("failed to parse localhost URL: %w", err)
257+
}
258+
259+
restURL, err := url.Parse(fmt.Sprintf("%s://api.%s/", u.Scheme, u.Hostname()))
260+
if err != nil {
261+
return apiHost{}, fmt.Errorf("failed to parse localhost REST URL: %w", err)
262+
}
263+
264+
gqlURL, err := url.Parse(fmt.Sprintf("%s://api.%s/graphql", u.Scheme, u.Hostname()))
265+
if err != nil {
266+
return apiHost{}, fmt.Errorf("failed to parse localhost GraphQL URL: %w", err)
267+
}
268+
269+
uploadURL, err := url.Parse(fmt.Sprintf("%s://uploads.%s", u.Scheme, u.Hostname()))
270+
if err != nil {
271+
return apiHost{}, fmt.Errorf("failed to parse localhost Upload URL: %w", err)
272+
}
273+
274+
rawURL, err := url.Parse(fmt.Sprintf("%s://raw.%s/", u.Scheme, u.Hostname()))
275+
if err != nil {
276+
return apiHost{}, fmt.Errorf("failed to parse localhost Raw URL: %w", err)
277+
}
278+
279+
return apiHost{
280+
baseRESTURL: restURL,
281+
graphqlURL: gqlURL,
282+
uploadURL: uploadURL,
283+
rawURL: rawURL,
284+
}, nil
285+
}
286+
252287
func newDotcomHost() (apiHost, error) {
253288
baseRestURL, err := url.Parse("https://api.github.com/")
254289
if err != nil {
@@ -365,6 +400,10 @@ func parseAPIHost(s string) (apiHost, error) {
365400
return apiHost{}, fmt.Errorf("host must have a scheme (http or https): %s", s)
366401
}
367402

403+
if strings.HasSuffix(u.Hostname(), "localhost") {
404+
return newLocalhostHost(s)
405+
}
406+
368407
if strings.HasSuffix(u.Hostname(), "github.com") {
369408
return newDotcomHost()
370409
}

0 commit comments

Comments
 (0)