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

Commit 2f7c863

Browse files
authored
Serve swagger under /api/swagger.json and swagger-ui (#2217)
This serves the generated `swagger.json` under `/api/swagger.json` and replaces the host with the current setting from `config.GetHTTPAddress()`. This should make the swagger output compatible with whatever host it is running on (local, prod-preview, prod, ...). I've also added a swagger-ui container that starts up on `make dev` and is served at [http://localhost:8081/](http://localhost:8081/): See also #165 for an old version of this.
1 parent 7d8ca23 commit 2f7c863

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,10 @@ dev: prebuild-check deps generate $(FRESH_BIN) docker-compose-up
321321
docker-compose-up:
322322
ifeq ($(UNAME_S),Darwin)
323323
@echo "Running docker-compose with macOS network settings"
324-
docker-compose -f docker-compose.macos.yml up -d db auth
324+
docker-compose -f docker-compose.macos.yml up -d db auth swagger_ui
325325
else
326326
@echo "Running docker-compose with Linux network settings"
327-
docker-compose up -d db auth
327+
docker-compose up -d db auth swagger_ui
328328
endif
329329

330330
MINISHIFT_IP = `minishift ip`

docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ services:
3434
- "8089:8089"
3535
network_mode: "host"
3636
depends_on:
37-
- db-auth
37+
- db-auth
38+
swagger_ui:
39+
image: swaggerapi/swagger-ui:latest
40+
ports:
41+
- "8081:8080"
42+
environment:
43+
API_URL: "http://localhost:8080/api/swagger.json"

main.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import (
44
"context"
55
"flag"
66
"net/http"
7+
"net/url"
78
"os"
89
"os/user"
910
"runtime"
11+
"strings"
1012
"time"
1113

12-
"github.com/fabric8-services/fabric8-wit/closeable"
13-
1414
"github.com/fabric8-services/fabric8-wit/account"
1515
"github.com/fabric8-services/fabric8-wit/app"
1616
"github.com/fabric8-services/fabric8-wit/application"
1717
"github.com/fabric8-services/fabric8-wit/auth"
18+
"github.com/fabric8-services/fabric8-wit/closeable"
1819
"github.com/fabric8-services/fabric8-wit/configuration"
1920
"github.com/fabric8-services/fabric8-wit/controller"
2021
witmiddleware "github.com/fabric8-services/fabric8-wit/goamiddleware"
@@ -29,6 +30,7 @@ import (
2930
"github.com/fabric8-services/fabric8-wit/remoteworkitem"
3031
"github.com/fabric8-services/fabric8-wit/sentry"
3132
"github.com/fabric8-services/fabric8-wit/space/authz"
33+
"github.com/fabric8-services/fabric8-wit/swagger"
3234
"github.com/fabric8-services/fabric8-wit/token"
3335
"github.com/goadesign/goa"
3436
"github.com/goadesign/goa/logging/logrus"
@@ -403,6 +405,25 @@ func main() {
403405
featuresCtrl := controller.NewFeaturesController(service, config)
404406
app.MountFeaturesController(service, featuresCtrl)
405407

408+
// serve the swagger.json modified to the current host
409+
service.Mux.Handle("GET", "/api/swagger.json",
410+
func(res http.ResponseWriter, req *http.Request, url url.Values) {
411+
b, err := swagger.Asset("swagger.json")
412+
if err != nil {
413+
res.WriteHeader(404)
414+
return
415+
}
416+
417+
s := string(b)
418+
s = strings.Replace(s, `"host":"openshift.io"`, `"host":"`+config.GetHTTPAddress()+`"`, -1)
419+
420+
res.Header().Set("Access-Control-Allow-Origin", "*")
421+
res.Header().Set("Access-Control-Allow-Methods", "GET")
422+
423+
res.Write([]byte(s))
424+
},
425+
)
426+
406427
log.Logger().Infoln("Git Commit SHA: ", controller.Commit)
407428
log.Logger().Infoln("UTC Build Time: ", controller.BuildTime)
408429
log.Logger().Infoln("UTC Start Time: ", controller.StartTime)

0 commit comments

Comments
 (0)