Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit 633aee2

Browse files
authored
Replace swagger host with request host and adjust scheme (#2220)
As a follow up for #2217 this change will properly replace the host (currently always `host: "0.0.0.0:8080",`) in the [generated swagger file](https://api.prod-preview.openshift.io/api/swagger.json) with the host of the request that tries to access the swagger file. We strip off the `http://` or `https://` prefix because that's handled in the swagger `schemes` section. We also set the scheme to `https` if the current request was made using `https://`.
1 parent 2f7c863 commit 633aee2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/fabric8-services/fabric8-wit/models"
2929
"github.com/fabric8-services/fabric8-wit/notification"
3030
"github.com/fabric8-services/fabric8-wit/remoteworkitem"
31+
"github.com/fabric8-services/fabric8-wit/rest"
3132
"github.com/fabric8-services/fabric8-wit/sentry"
3233
"github.com/fabric8-services/fabric8-wit/space/authz"
3334
"github.com/fabric8-services/fabric8-wit/swagger"
@@ -415,7 +416,16 @@ func main() {
415416
}
416417

417418
s := string(b)
418-
s = strings.Replace(s, `"host":"openshift.io"`, `"host":"`+config.GetHTTPAddress()+`"`, -1)
419+
// replace swagger host with host from request
420+
newHost := rest.AbsoluteURL(req, "")
421+
newHost = strings.Replace(newHost, "http://", "", -1)
422+
newHost = strings.Replace(newHost, "https://", "", -1)
423+
s = strings.Replace(s, `"host":"openshift.io"`, `"host":"`+newHost+`"`, -1)
424+
425+
// replace schemes in swagger with the current URL scheme
426+
if req.URL != nil && strings.ToLower(req.URL.Scheme) == "https" {
427+
s = strings.Replace(s, `"schemes":["http"]`, `"schemes":["https"]`, -1)
428+
}
419429

420430
res.Header().Set("Access-Control-Allow-Origin", "*")
421431
res.Header().Set("Access-Control-Allow-Methods", "GET")

0 commit comments

Comments
 (0)