Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Allow to override the API endpoint #12

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ make test
For any requests, bug or comments, please [open an issue][issues] or [submit a
pull request][pulls].

## Development

When developing new APIs locally, you can overwrite the API endpoint.
Specify `MONDOO_API_ENDPOINT` environment variable for the `generate` command, e.g.,:
```
MONDOO_API_ENDPOINT=http://127.0.0.1 make generate
```

## Kudos

This implementation is heavily inspired by the [GitHub GraphQL Go Client](https://github.com/shurcooL/githubv4).
Expand Down
12 changes: 11 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -176,10 +177,19 @@ fragment TypeRef on __Type {
}
}
`
apiEndpoint := "https://" + apiHost + "/query"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should define apiHost := "us.api.mondoo.com" close to this code since its otherwise difficult to read

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should parse the url and prepend https if no schema is defined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

endpoint, ok := os.LookupEnv("MONDOO_API_ENDPOINT")
if ok {
apiEndpoint, err = url.JoinPath(endpoint, "/query")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should apply that logic to the default api host too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

if err != nil {
log.Fatalf("invalid MONDOO_API_ENDPOINT: %v", err)
}
}
fmt.Printf("using endpoint %s\n", apiEndpoint)
// do introspection query
req, err := http.NewRequest(
"POST",
"https://"+apiHost+"/query",
apiEndpoint,
strings.NewReader(`{"query":`+strconv.Quote(introspection)+`}`),
)
if err != nil {
Expand Down
Loading