Skip to content

Commit

Permalink
feat: add a toggle to access the OpenAPI documentation (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm authored Jun 24, 2024
1 parent dfb3870 commit 76f174e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const (
// PprofEnabledKey indicates the configuration key of the /debug/pprof debugging api/
PprofEnabledKey = "pprof.enabled"

// OpenapiDocEnabledKey indicates the configuration key of the /openapi.json debugging api/
OpenapiDocEnabledKey = "openapidoc.enabled"

// ClusterTaskDisabledKey specifies the key for the clustertask creation feature.
// When set to false, the creation of clustertasks is disabled and new clustertasks cannot be created and can only be updated.
ClusterTaskCreationEnabledKey = "clustertask.creation.enabled"
Expand Down
20 changes: 17 additions & 3 deletions plugin/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"github.com/katanomi/pkg/config"
"github.com/katanomi/pkg/plugin/client"
"github.com/katanomi/pkg/plugin/component/metrics"
"github.com/katanomi/pkg/plugin/component/tracing"
Expand Down Expand Up @@ -522,11 +523,24 @@ func NewDefaultService(ctx context.Context) *restful.WebService {
return ws
}

// NewDocService go restful api doc
func NewDocService(webservices ...*restful.WebService) *restful.WebService {
// NewDocServiceWithCtx go restful api doc
func NewDocServiceWithCtx(ctx context.Context, webservices ...*restful.WebService) *restful.WebService {
configManager := config.KatanomiConfigManager(ctx)

configFilter := NoOpFilter
if ctx != nil && configManager != nil {
configFilter = config.ConfigFilter(ctx, configManager, config.OpenapiDocEnabledKey, config.ConfigFilterNotFoundWhenNotTrue)
}

config := restfulspec.Config{
WebServices: webservices,
APIPath: "/openapi.json",
}
return restfulspec.NewOpenAPIService(config)
return restfulspec.NewOpenAPIService(config).Filter(configFilter)
}

// NewDocService go restful api doc
// Deprecated: use NewDocServiceWithCtx instead
func NewDocService(webservices ...*restful.WebService) *restful.WebService {
return NewDocServiceWithCtx(context.Background(), webservices...)
}
2 changes: 1 addition & 1 deletion sharedmain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func (a *AppBuilder) Run(startFuncs ...func(context.Context) error) error {
a.container.Add(route.NewDefaultService(a.Context))

if len(a.container.RegisteredWebServices()) > 0 {
a.container.Add(route.NewDocService(a.container.RegisteredWebServices()...))
a.container.Add(route.NewDocServiceWithCtx(a.Context, a.container.RegisteredWebServices()...))
}

a.startFunc = append(a.startFunc, func(ctx context.Context) error {
Expand Down

0 comments on commit 76f174e

Please sign in to comment.