feat: different openapi for harness code (#997)

This commit is contained in:
Abhinav Singh 2024-01-30 21:51:44 +00:00 committed by Harness
parent 61ee297c9b
commit 87cae05747
8 changed files with 71 additions and 6 deletions

View File

@ -28,10 +28,18 @@ type (
}
)
var _ Service = (*OpenAPI)(nil)
type OpenAPI struct{}
func NewOpenAPIService() *OpenAPI {
return &OpenAPI{}
}
// Generate is a helper function that constructs the
// openapi specification object, which can be marshaled
// to json or yaml, as needed.
func Generate() *openapi3.Spec {
func (*OpenAPI) Generate() *openapi3.Spec {
reflector := openapi3.Reflector{}
reflector.Spec = &openapi3.Spec{Openapi: "3.0.0"}
reflector.Spec.Info.

View File

@ -0,0 +1,21 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openapi
import "github.com/swaggest/openapi-go/openapi3"
type Service interface {
Generate() *openapi3.Spec
}

28
app/api/openapi/wire.go Normal file
View File

@ -0,0 +1,28 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openapi
import (
"github.com/google/wire"
)
// WireSet provides a wire set for this package.
var WireSet = wire.NewSet(
ProvideOpenAPIService,
)
func ProvideOpenAPIService() Service {
return NewOpenAPIService()
}

View File

@ -33,7 +33,9 @@ type WebHandler interface {
}
// NewWebHandler returns a new WebHandler.
func NewWebHandler(config *types.Config) WebHandler {
func NewWebHandler(config *types.Config,
openapi openapi.Service,
) WebHandler {
// Use go-chi router for inner routing
r := chi.NewRouter()
// create middleware to enforce security best practices for

View File

@ -38,6 +38,7 @@ import (
"github.com/harness/gitness/app/api/controller/upload"
"github.com/harness/gitness/app/api/controller/user"
"github.com/harness/gitness/app/api/controller/webhook"
"github.com/harness/gitness/app/api/openapi"
"github.com/harness/gitness/app/auth/authn"
"github.com/harness/gitness/app/url"
"github.com/harness/gitness/types"
@ -116,6 +117,6 @@ func ProvideAPIHandler(
githookCtrl, saCtrl, userCtrl, principalCtrl, checkCtrl, sysCtrl, blobCtrl, searchCtrl)
}
func ProvideWebHandler(config *types.Config) WebHandler {
return NewWebHandler(config)
func ProvideWebHandler(config *types.Config, openapi openapi.Service) WebHandler {
return NewWebHandler(config, openapi)
}

View File

@ -27,7 +27,8 @@ type swaggerCommand struct {
}
func (c *swaggerCommand) run(*kingpin.ParseContext) error {
spec := openapi.Generate()
openAPIGenerator := openapi.NewOpenAPIService()
spec := openAPIGenerator.Generate()
data, _ := spec.MarshalYAML()
if c.path == "" {
os.Stdout.Write(data)

View File

@ -31,6 +31,7 @@ import (
"github.com/harness/gitness/app/api/controller/upload"
"github.com/harness/gitness/app/api/controller/user"
controllerwebhook "github.com/harness/gitness/app/api/controller/webhook"
"github.com/harness/gitness/app/api/openapi"
"github.com/harness/gitness/app/auth/authn"
"github.com/harness/gitness/app/auth/authz"
"github.com/harness/gitness/app/bootstrap"
@ -179,6 +180,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
keywordsearch.WireSet,
controllerkeywordsearch.WireSet,
usergroup.WireSet,
openapi.WireSet,
)
return &cliserver.System{}, nil
}

View File

@ -30,6 +30,7 @@ import (
"github.com/harness/gitness/app/api/controller/upload"
"github.com/harness/gitness/app/api/controller/user"
webhook2 "github.com/harness/gitness/app/api/controller/webhook"
"github.com/harness/gitness/app/api/openapi"
"github.com/harness/gitness/app/auth/authn"
"github.com/harness/gitness/app/auth/authz"
"github.com/harness/gitness/app/bootstrap"
@ -277,7 +278,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
keywordsearchController := keywordsearch2.ProvideController(authorizer, searcher, repoController, spaceController)
apiHandler := router.ProvideAPIHandler(ctx, config, authenticator, repoController, executionController, logsController, spaceController, pipelineController, secretController, triggerController, connectorController, templateController, pluginController, pullreqController, webhookController, githookController, serviceaccountController, controller, principalController, checkController, systemController, uploadController, keywordsearchController)
gitHandler := router.ProvideGitHandler(provider, authenticator, repoController)
webHandler := router.ProvideWebHandler(config)
openapiService := openapi.ProvideOpenAPIService()
webHandler := router.ProvideWebHandler(config, openapiService)
routerRouter := router.ProvideRouter(apiHandler, gitHandler, webHandler, provider)
serverServer := server2.ProvideServer(config, routerRouter)
executionManager := manager.ProvideExecutionManager(config, executionStore, pipelineStore, provider, streamer, fileService, converterService, logStore, logStream, checkStore, repoStore, schedulerScheduler, secretStore, stageStore, stepStore, principalStore)