Skip to content

Commit

Permalink
chore: add KONG_CUSTOM_DOMAIN handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Nov 19, 2024
1 parent 7742961 commit 4f50eb7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .speakeasy/gen.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ id: b81e5def-5b1e-4753-ae7c-0efccc2e6f61
management:
docChecksum: 621f79fe092199d80a95ca5aa0238ebc
docVersion: 0.0.1
speakeasyVersion: 1.439.0
generationVersion: 2.457.9
releaseVersion: 0.1.6
configChecksum: 5464a3ce17d764c785662a6628d0b0ec
speakeasyVersion: 1.441.0
generationVersion: 2.460.1
releaseVersion: 0.1.7
configChecksum: db304f60e3a64fc47b514a7de5d55c5b
features:
go:
additionalDependencies: 0.1.0
Expand Down
2 changes: 1 addition & 1 deletion .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ generation:
oAuth2ClientCredentialsEnabled: false
oAuth2PasswordEnabled: false
go:
version: 0.1.6
version: 0.1.7
additionalDependencies: {}
allowUnknownFieldsInWeakUnions: false
clientServerStatusCodesAsErrors: true
Expand Down
17 changes: 17 additions & 0 deletions internal/hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Configuration through environment variables

This SDK can be configured via the environment variables.

## Konnect domain

Konnect domain can be customized via `KONG_CUSTOM_DOMAIN` environment variable.
This is useful when testing against a development environment or a custom domain.
This is optional and the default value is `konghq.com`.

## Request and response logging

Request and response logging can be enabled via:

- `KONNECT_SDK_HTTP_DUMP_REQUEST`: Set to `true` to log request bodies.
- `KONNECT_SDK_HTTP_DUMP_RESPONSE`: Set to `true` to log response bodies.
- `KONNECT_SDK_HTTP_DUMP_RESPONSE_ERROR`: Set to `true` to log response bodies only when the response status code is an error.
4 changes: 3 additions & 1 deletion internal/hooks/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import "os"
func initHooks(h *Hooks) {
h.registerBeforeRequestHook(&UserAgentPreRequestHook{})

h.registerBeforeRequestHook(&APIURLRequestHook{})
h.registerBeforeRequestHook(&APIURLRequestHook{
CustomDomain: os.Getenv("KONG_CUSTOM_DOMAIN"),
})

if os.Getenv("KONNECT_SDK_HTTP_DUMP_REQUEST") == "true" {
h.registerBeforeRequestHook(&HTTPDumpRequestHook{})
Expand Down
19 changes: 8 additions & 11 deletions internal/hooks/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import (
"strings"
)

type APIURLRequestHook struct{}
type APIURLRequestHook struct {
CustomDomain string
}

var _ beforeRequestHook = (*APIURLRequestHook)(nil)

// Replace `.konghq.com` with the custom domain set in the `KONG_CUSTOM_DOMAIN` environment variable.
func (i *APIURLRequestHook) BeforeRequest(hookCtx BeforeRequestContext, req *http.Request) (*http.Request, error) {
switch hookCtx.OperationID {
case "get-organizations-me":
// NOTE(pmalek): This is because we merge OpenAPI specs and /organizations/me
// is only served by the global API.
// @mheap mentioned that we can add operation specific URLs to do away with this.
if strings.HasSuffix(req.URL.Host, "api.konghq.tech") {
req.URL.Host = "global.api.konghq.tech"
} else {
req.URL.Host = "global.api.konghq.com"
}
if i.CustomDomain != "" {
req.URL.Host = strings.Replace(req.URL.Host, ".konghq.com", "."+i.CustomDomain, 1)
req.Host = strings.Replace(req.Host, ".konghq.com", "."+i.CustomDomain, 1)
}

return req, nil
}
6 changes: 3 additions & 3 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ func New(opts ...SDKOption) *SDK {
sdkConfiguration: sdkConfiguration{
Language: "go",
OpenAPIDocVersion: "0.0.1",
SDKVersion: "0.1.6",
GenVersion: "2.457.9",
UserAgent: "speakeasy-sdk/go 0.1.6 2.457.9 0.0.1 github.com/Kong/sdk-konnect-go",
SDKVersion: "0.1.7",
GenVersion: "2.460.1",
UserAgent: "speakeasy-sdk/go 0.1.7 2.460.1 0.0.1 github.com/Kong/sdk-konnect-go",
Hooks: hooks.New(),
},
}
Expand Down

0 comments on commit 4f50eb7

Please sign in to comment.