Skip to content

Commit

Permalink
Merge pull request #101 from wepala/WEOS-1249
Browse files Browse the repository at this point in the history
feature/Weos 1249 - Add handler to allow user to get swagger configuration as html or json
  • Loading branch information
shaniah868 authored Feb 17, 2022
2 parents 72d0532 + 74142d8 commit c0154df
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 13 deletions.
32 changes: 32 additions & 0 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/rakyll/statik/fs"
weoscontext "github.com/wepala/weos/context"
"github.com/wepala/weos/projections/dialects"
"gorm.io/driver/clickhouse"
Expand Down Expand Up @@ -272,6 +273,31 @@ func (p *RESTAPI) GetEntityFactories() map[string]model.EntityFactory {
return p.entityFactories
}

const SWAGGERUIENDPOINT = "/_discover/"
const SWAGGERJSONENDPOINT = "/_discover_json"

//RegisterSwaggerAPI creates default swagger api from binary
func (p *RESTAPI) RegisterDefaultSwaggerAPI() error {
statikFS, err := fs.New()
if err != nil {
return NewControllerError("Got an error formatting response", err, http.StatusInternalServerError)
}
static := http.FileServer(statikFS)
sh := http.StripPrefix(SWAGGERUIENDPOINT, static)
handler := echo.WrapHandler(sh)
p.e.GET(SWAGGERUIENDPOINT+"*", handler)

return nil
}

//RegisterDefaultSwaggerJson registers a default swagger json response
func (p *RESTAPI) RegisterDefaultSwaggerJSON() error {
p.e.GET(SWAGGERJSONENDPOINT, func(c echo.Context) error {
return c.JSON(http.StatusOK, p.Swagger)
})
return nil
}

//Initialize and setup configurations for RESTAPI
func (p *RESTAPI) Initialize(ctxt context.Context) error {
//register standard controllers
Expand All @@ -283,6 +309,7 @@ func (p *RESTAPI) Initialize(ctxt context.Context) error {
p.RegisterController("HealthCheck", HealthCheck)
p.RegisterController("CreateBatchController", CreateBatchController)
p.RegisterController("APIDiscovery", APIDiscovery)

//register standard middleware
p.RegisterMiddleware("Context", Context)
p.RegisterMiddleware("CreateMiddleware", CreateMiddleware)
Expand All @@ -300,6 +327,11 @@ func (p *RESTAPI) Initialize(ctxt context.Context) error {
p.RegisterOperationInitializer(RouteInitializer)
//register standard post path initializers
p.RegisterPostPathInitializer(CORsInitializer)

//make default endpoints for returning swagger configuration to user
p.RegisterDefaultSwaggerAPI()
p.RegisterDefaultSwaggerJSON()

//these are the dynamic struct builders for the schemas in the OpenAPI
var schemas map[string]ds.Builder

Expand Down
5 changes: 3 additions & 2 deletions controllers/rest/controller_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/segmentio/ksuid"
weoscontext "github.com/wepala/weos/context"
"github.com/wepala/weos/model"
_ "github.com/wepala/weos/swaggerui"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -285,9 +286,9 @@ func APIDiscovery(api *RESTAPI, projection projections.Projection, commandDispat
//get content type expected for 200 response
responseType := newContext.Value(weoscontext.RESPONSE_PREFIX + strconv.Itoa(http.StatusOK))
if responseType == "application/json" {
return ctxt.JSON(http.StatusOK, api.Swagger)
return ctxt.Redirect(http.StatusPermanentRedirect, SWAGGERJSONENDPOINT)
} else if responseType == "application/html" {
return ctxt.JSON(http.StatusOK, api.Swagger)
return ctxt.Redirect(http.StatusPermanentRedirect, SWAGGERUIENDPOINT)
}

return NewControllerError("No response format chosen for a valid response", nil, http.StatusBadRequest)
Expand Down
29 changes: 20 additions & 9 deletions end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,16 @@ func aHeaderWithValue(key, value string) error {
func aResponseShouldBeReturned(statusCode int) error {
//check resp first
if resp != nil && resp.StatusCode != statusCode {
if statusCode == http.StatusOK && resp.StatusCode > 300 && resp.StatusCode < 310 {
//redirected
return nil
}
return fmt.Errorf("expected the status code to be '%d', got '%d'", statusCode, resp.StatusCode)
} else if rec != nil && rec.Result().StatusCode != statusCode {
if statusCode == http.StatusOK && rec.Result().StatusCode > 300 && rec.Result().StatusCode < 310 {
//redirected
return nil
}
return fmt.Errorf("expected the status code to be '%d', got '%d'", statusCode, rec.Result().StatusCode)
}
return nil
Expand Down Expand Up @@ -1427,17 +1435,19 @@ func theTotalNoEventsAndProcessedAndFailuresShouldBeReturned() error {
}

func theApiAsJsonShouldBeShown() error {
contentEntity := map[string]interface{}{}
err := json.NewDecoder(rec.Body).Decode(&contentEntity)

if err != nil {
return err
url := rec.HeaderMap.Get("Location")
if url != api.SWAGGERJSONENDPOINT {
return fmt.Errorf("the json result should have been returned")
}
return godog.ErrPending
return nil
}

func theSwaggerUiShouldBeShown() error {
return godog.ErrPending
url := rec.HeaderMap.Get("Location")
if url != api.SWAGGERUIENDPOINT {
return fmt.Errorf("the html result should have been returned")
}
return nil
}

func InitializeScenario(ctx *godog.ScenarioContext) {
Expand Down Expand Up @@ -1530,8 +1540,9 @@ func TestBDD(t *testing.T) {
TestSuiteInitializer: InitializeSuite,
Options: &godog.Options{
Format: "pretty",
//Tags: "~skipped && ~long",
Tags: "WEOS-1127",
Tags: "~long && ~skipped",
//Tags: "focus1",
//Tags: "WEOS-1110 && ~skipped",
},
}.Run()
if status != 0 {
Expand Down
8 changes: 6 additions & 2 deletions features/api-discovery.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ Feature: Get API Details
report-caller: true
formatter: json
database:
driver: sqlite3
database: e2e.db
database: "%s"
driver: "%s"
host: "%s"
password: "%s"
username: "%s"
port: %d
event-source:
- title: default
driver: service
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/ompluscator/dynamic-struct v1.3.0
github.com/ory/dockertest/v3 v3.6.0
github.com/proullon/ramsql v0.0.0-20181213202341-817cee58a244
github.com/rakyll/statik v0.1.7 // indirect
github.com/segmentio/ksuid v1.0.3
github.com/testcontainers/testcontainers-go v0.12.0
go.uber.org/zap v1.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/proullon/ramsql v0.0.0-20181213202341-817cee58a244 h1:fdX2U+a2Rmc4BjRYcOKzjYXtYTE4ga1B2lb8i7BlefU=
github.com/proullon/ramsql v0.0.0-20181213202341-817cee58a244/go.mod h1:jG8oAQG0ZPHPyxg5QlMERS31airDC+ZuqiAe8DUvFVo=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
Expand Down
12 changes: 12 additions & 0 deletions swaggerui/swaggerui.go

Large diffs are not rendered by default.

0 comments on commit c0154df

Please sign in to comment.