diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9344af3..9417053 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -21,21 +21,12 @@ jobs: with: version: 3.17 repo-token: ${{ secrets.GITHUB_TOKEN }} - - run: | - go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 - go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 - go install golang.org/x/tools/cmd/goimports@latest - name: Generate Files run: | - go generate ./dataapiv1 - if [ -n "$(git status --porcelain)" ]; then - echo 'To fix this check, run "go generate ./dataapiv1"' - git status # Show the files that failed to pass the check. - exit 1 - fi + go generate ./... - name: goimports run: | - goimports -w . + go run golang.org/x/tools/cmd/goimports@latest -w . find . -name go.mod -execdir go mod tidy \; if [ -n "$(git status --porcelain)" ]; then echo 'To fix this check, run "goimports -w . && find . -name go.mod -execdir go mod tidy \;"' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 262e1b3..fe5a150 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -41,6 +41,9 @@ jobs: go install github.com/matryer/moq@latest - name: Install Dependencies run: go get ./... + - name: Generate Files + run: | + go generate ./... - name: Initialize Test Cluster run: ./.github/bin/init-cluster - name: Check Test Cluster diff --git a/Makefile b/Makefile index 738eaa9..ff86825 100644 --- a/Makefile +++ b/Makefile @@ -33,9 +33,12 @@ lint: go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINT_VERSION) $(GOBIN)/golangci-lint run -check: fmt lint test +check: generate fmt lint test -build: +generate: + go generate ./... + +build: generate for platform in linux darwin ; do \ for arch in amd64 arm64 ; do \ echo "Building $$platform $$arch binary " ; \ @@ -56,4 +59,4 @@ dist: image-artifacts container: build docker build -f Dockerfile -t ${DOCKER_USER}/cloud-native-gateway:${DOCKER_TAG} . -.PHONY: all test fmt lint check build \ No newline at end of file +.PHONY: all test fmt lint check generate build \ No newline at end of file diff --git a/README.md b/README.md index 2a7be8c..8cb55e8 100644 --- a/README.md +++ b/README.md @@ -20,35 +20,42 @@ Provides translation capabilities for Protostellar to legacy protocols. Building the gateway: ``` +> go generate ./... > go build -o stellar-gateway ./cmd/gateway ``` Basic execution which will run generate a self-signed certificate and run against a Couchbase Server instance on localhost: + ``` > ./stellar-gateway --self-sign ``` To turn up the log level: + ``` > ./stellar-gateway --self-sign --log-level debug ``` To add extra debug information to errors: + ``` > ./stellar-gateway --self-sign --debug ``` To execute against a remote Couchbase Server instance: + ``` > ./stellar-gateway --self-sign --cb-host 192.168.107.128 ``` -To execute using an existing certificate (used to connect to the gateway by clients): +To execute using an existing certificate (used to connect to the gateway by clients): + ``` > ./stellar-gateway --cert /path/to/cert --key /path/to/key ``` To execute without having to perform a build after every change + ``` > go run ./cmd/gateway --self-sign ``` diff --git a/dataapiv1/.gitignore b/dataapiv1/.gitignore new file mode 100644 index 0000000..ee5096d --- /dev/null +++ b/dataapiv1/.gitignore @@ -0,0 +1 @@ +spec.gen.go \ No newline at end of file diff --git a/dataapiv1/spec.gen.go b/dataapiv1/spec.gen.go deleted file mode 100644 index b57b1d6..0000000 --- a/dataapiv1/spec.gen.go +++ /dev/null @@ -1,1099 +0,0 @@ -// Package dataapiv1 provides primitives to interact with the openapi HTTP API. -// -// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.3.0 DO NOT EDIT. -package dataapiv1 - -import ( - "context" - "fmt" - "io" - "net/http" - - "github.com/gorilla/mux" - "github.com/oapi-codegen/runtime" - strictnethttp "github.com/oapi-codegen/runtime/strictmiddleware/nethttp" -) - -// Defines values for DocumentEncoding. -const ( - DocumentEncodingIdentity DocumentEncoding = "identity" - DocumentEncodingSnappy DocumentEncoding = "snappy" -) - -// Defines values for DurabilityLevel. -const ( - DurabilityLevelMajority DurabilityLevel = "Majority" - DurabilityLevelMajorityAndPersistOnMaster DurabilityLevel = "MajorityAndPersistOnMaster" - DurabilityLevelNone DurabilityLevel = "None" - DurabilityLevelPersitToMajority DurabilityLevel = "PersitToMajority" -) - -// DocumentEncoding DocumentEncoding specifies the compression used for the the document. -type DocumentEncoding string - -// DurabilityLevel DurabilityLevel specifies the level of synchronous replication to use. -type DurabilityLevel string - -// DeleteDocumentParams defines parameters for DeleteDocument. -type DeleteDocumentParams struct { - // IfMatch The CAS of the document to check before updating. - IfMatch *string `json:"If-Match,omitempty"` - - // XCBDurabilityLevel The level of durability required for this write operation. - XCBDurabilityLevel *DurabilityLevel `json:"X-CB-DurabilityLevel,omitempty"` - - // Authorization Header for authentication - Authorization string `json:"Authorization"` -} - -// GetDocumentParams defines parameters for GetDocument. -type GetDocumentParams struct { - // Project Specific fields to project from the document. - Project *[]string `form:"project,omitempty" json:"project,omitempty"` - - // AcceptEncoding The encoding of the document - AcceptEncoding *[]DocumentEncoding `json:"Accept-Encoding,omitempty"` - - // Authorization Header for authentication - Authorization string `json:"Authorization"` -} - -// CreateDocumentParams defines parameters for CreateDocument. -type CreateDocumentParams struct { - // XCBDurabilityLevel The level of durability required for this write operation. - XCBDurabilityLevel *DurabilityLevel `form:"X-CB-DurabilityLevel,omitempty" json:"X-CB-DurabilityLevel,omitempty"` - - // ContentEncoding The encoding of the document - ContentEncoding *DocumentEncoding `json:"Content-Encoding,omitempty"` - - // Expires The expiry time of the document. - Expires *string `json:"Expires,omitempty"` - - // XCBFlags The Flags of the document. - XCBFlags *uint32 `json:"X-CB-Flags,omitempty"` - - // Authorization Header for authentication - Authorization string `json:"Authorization"` -} - -// UpdateDocumentParams defines parameters for UpdateDocument. -type UpdateDocumentParams struct { - // ContentEncoding The encoding of the document - ContentEncoding *DocumentEncoding `json:"Content-Encoding,omitempty"` - - // IfMatch The CAS of the document to check before updating. - IfMatch *string `json:"If-Match,omitempty"` - - // Expires The expiry time of the document. - Expires *string `json:"Expires,omitempty"` - - // XCBFlags The Flags of the document. - XCBFlags *uint32 `json:"X-CB-Flags,omitempty"` - - // XCBDurabilityLevel The level of durability required for this write operation. - XCBDurabilityLevel *DurabilityLevel `json:"X-CB-DurabilityLevel,omitempty"` - - // Authorization Header for authentication - Authorization string `json:"Authorization"` -} - -// ServerInterface represents all server handlers. -type ServerInterface interface { - - // (DELETE /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - DeleteDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params DeleteDocumentParams) - - // (GET /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - GetDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params GetDocumentParams) - - // (POST /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - CreateDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params CreateDocumentParams) - - // (PUT /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - UpdateDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params UpdateDocumentParams) -} - -// ServerInterfaceWrapper converts contexts to parameters. -type ServerInterfaceWrapper struct { - Handler ServerInterface - HandlerMiddlewares []MiddlewareFunc - ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error) -} - -type MiddlewareFunc func(http.Handler) http.Handler - -// DeleteDocument operation middleware -func (siw *ServerInterfaceWrapper) DeleteDocument(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - var err error - - // ------------- Path parameter "bucketName" ------------- - var bucketName string - - err = runtime.BindStyledParameterWithOptions("simple", "bucketName", mux.Vars(r)["bucketName"], &bucketName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "bucketName", Err: err}) - return - } - - // ------------- Path parameter "scopeName" ------------- - var scopeName string - - err = runtime.BindStyledParameterWithOptions("simple", "scopeName", mux.Vars(r)["scopeName"], &scopeName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "scopeName", Err: err}) - return - } - - // ------------- Path parameter "collectionName" ------------- - var collectionName string - - err = runtime.BindStyledParameterWithOptions("simple", "collectionName", mux.Vars(r)["collectionName"], &collectionName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "collectionName", Err: err}) - return - } - - // ------------- Path parameter "DocumentKey" ------------- - var documentKey string - - err = runtime.BindStyledParameterWithOptions("simple", "DocumentKey", mux.Vars(r)["DocumentKey"], &documentKey, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "DocumentKey", Err: err}) - return - } - - // Parameter object where we will unmarshal all parameters from the context - var params DeleteDocumentParams - - headers := r.Header - - // ------------- Optional header parameter "If-Match" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("If-Match")]; found { - var IfMatch string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "If-Match", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "If-Match", valueList[0], &IfMatch, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "If-Match", Err: err}) - return - } - - params.IfMatch = &IfMatch - - } - - // ------------- Optional header parameter "X-CB-DurabilityLevel" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("X-CB-DurabilityLevel")]; found { - var XCBDurabilityLevel DurabilityLevel - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "X-CB-DurabilityLevel", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "X-CB-DurabilityLevel", valueList[0], &XCBDurabilityLevel, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "X-CB-DurabilityLevel", Err: err}) - return - } - - params.XCBDurabilityLevel = &XCBDurabilityLevel - - } - - // ------------- Required header parameter "Authorization" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Authorization")]; found { - var Authorization string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Authorization", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Authorization", valueList[0], &Authorization, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Authorization", Err: err}) - return - } - - params.Authorization = Authorization - - } else { - err = fmt.Errorf("Header parameter Authorization is required, but not found") - siw.ErrorHandlerFunc(w, r, &RequiredHeaderError{ParamName: "Authorization", Err: err}) - return - } - - handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.DeleteDocument(w, r, bucketName, scopeName, collectionName, documentKey, params) - })) - - for _, middleware := range siw.HandlerMiddlewares { - handler = middleware(handler) - } - - handler.ServeHTTP(w, r.WithContext(ctx)) -} - -// GetDocument operation middleware -func (siw *ServerInterfaceWrapper) GetDocument(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - var err error - - // ------------- Path parameter "bucketName" ------------- - var bucketName string - - err = runtime.BindStyledParameterWithOptions("simple", "bucketName", mux.Vars(r)["bucketName"], &bucketName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "bucketName", Err: err}) - return - } - - // ------------- Path parameter "scopeName" ------------- - var scopeName string - - err = runtime.BindStyledParameterWithOptions("simple", "scopeName", mux.Vars(r)["scopeName"], &scopeName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "scopeName", Err: err}) - return - } - - // ------------- Path parameter "collectionName" ------------- - var collectionName string - - err = runtime.BindStyledParameterWithOptions("simple", "collectionName", mux.Vars(r)["collectionName"], &collectionName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "collectionName", Err: err}) - return - } - - // ------------- Path parameter "DocumentKey" ------------- - var documentKey string - - err = runtime.BindStyledParameterWithOptions("simple", "DocumentKey", mux.Vars(r)["DocumentKey"], &documentKey, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "DocumentKey", Err: err}) - return - } - - // Parameter object where we will unmarshal all parameters from the context - var params GetDocumentParams - - // ------------- Optional query parameter "project" ------------- - - err = runtime.BindQueryParameter("form", false, false, "project", r.URL.Query(), ¶ms.Project) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "project", Err: err}) - return - } - - headers := r.Header - - // ------------- Optional header parameter "Accept-Encoding" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Accept-Encoding")]; found { - var AcceptEncoding []DocumentEncoding - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Accept-Encoding", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Accept-Encoding", valueList[0], &AcceptEncoding, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Accept-Encoding", Err: err}) - return - } - - params.AcceptEncoding = &AcceptEncoding - - } - - // ------------- Required header parameter "Authorization" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Authorization")]; found { - var Authorization string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Authorization", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Authorization", valueList[0], &Authorization, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Authorization", Err: err}) - return - } - - params.Authorization = Authorization - - } else { - err = fmt.Errorf("Header parameter Authorization is required, but not found") - siw.ErrorHandlerFunc(w, r, &RequiredHeaderError{ParamName: "Authorization", Err: err}) - return - } - - handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.GetDocument(w, r, bucketName, scopeName, collectionName, documentKey, params) - })) - - for _, middleware := range siw.HandlerMiddlewares { - handler = middleware(handler) - } - - handler.ServeHTTP(w, r.WithContext(ctx)) -} - -// CreateDocument operation middleware -func (siw *ServerInterfaceWrapper) CreateDocument(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - var err error - - // ------------- Path parameter "bucketName" ------------- - var bucketName string - - err = runtime.BindStyledParameterWithOptions("simple", "bucketName", mux.Vars(r)["bucketName"], &bucketName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "bucketName", Err: err}) - return - } - - // ------------- Path parameter "scopeName" ------------- - var scopeName string - - err = runtime.BindStyledParameterWithOptions("simple", "scopeName", mux.Vars(r)["scopeName"], &scopeName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "scopeName", Err: err}) - return - } - - // ------------- Path parameter "collectionName" ------------- - var collectionName string - - err = runtime.BindStyledParameterWithOptions("simple", "collectionName", mux.Vars(r)["collectionName"], &collectionName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "collectionName", Err: err}) - return - } - - // ------------- Path parameter "DocumentKey" ------------- - var documentKey string - - err = runtime.BindStyledParameterWithOptions("simple", "DocumentKey", mux.Vars(r)["DocumentKey"], &documentKey, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "DocumentKey", Err: err}) - return - } - - // Parameter object where we will unmarshal all parameters from the context - var params CreateDocumentParams - - // ------------- Optional query parameter "X-CB-DurabilityLevel" ------------- - - err = runtime.BindQueryParameter("form", true, false, "X-CB-DurabilityLevel", r.URL.Query(), ¶ms.XCBDurabilityLevel) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "X-CB-DurabilityLevel", Err: err}) - return - } - - headers := r.Header - - // ------------- Optional header parameter "Content-Encoding" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Content-Encoding")]; found { - var ContentEncoding DocumentEncoding - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Content-Encoding", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Content-Encoding", valueList[0], &ContentEncoding, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Content-Encoding", Err: err}) - return - } - - params.ContentEncoding = &ContentEncoding - - } - - // ------------- Optional header parameter "Expires" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Expires")]; found { - var Expires string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Expires", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Expires", valueList[0], &Expires, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Expires", Err: err}) - return - } - - params.Expires = &Expires - - } - - // ------------- Optional header parameter "X-CB-Flags" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("X-CB-Flags")]; found { - var XCBFlags uint32 - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "X-CB-Flags", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "X-CB-Flags", valueList[0], &XCBFlags, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "X-CB-Flags", Err: err}) - return - } - - params.XCBFlags = &XCBFlags - - } - - // ------------- Required header parameter "Authorization" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Authorization")]; found { - var Authorization string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Authorization", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Authorization", valueList[0], &Authorization, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Authorization", Err: err}) - return - } - - params.Authorization = Authorization - - } else { - err = fmt.Errorf("Header parameter Authorization is required, but not found") - siw.ErrorHandlerFunc(w, r, &RequiredHeaderError{ParamName: "Authorization", Err: err}) - return - } - - handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.CreateDocument(w, r, bucketName, scopeName, collectionName, documentKey, params) - })) - - for _, middleware := range siw.HandlerMiddlewares { - handler = middleware(handler) - } - - handler.ServeHTTP(w, r.WithContext(ctx)) -} - -// UpdateDocument operation middleware -func (siw *ServerInterfaceWrapper) UpdateDocument(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - var err error - - // ------------- Path parameter "bucketName" ------------- - var bucketName string - - err = runtime.BindStyledParameterWithOptions("simple", "bucketName", mux.Vars(r)["bucketName"], &bucketName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "bucketName", Err: err}) - return - } - - // ------------- Path parameter "scopeName" ------------- - var scopeName string - - err = runtime.BindStyledParameterWithOptions("simple", "scopeName", mux.Vars(r)["scopeName"], &scopeName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "scopeName", Err: err}) - return - } - - // ------------- Path parameter "collectionName" ------------- - var collectionName string - - err = runtime.BindStyledParameterWithOptions("simple", "collectionName", mux.Vars(r)["collectionName"], &collectionName, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "collectionName", Err: err}) - return - } - - // ------------- Path parameter "DocumentKey" ------------- - var documentKey string - - err = runtime.BindStyledParameterWithOptions("simple", "DocumentKey", mux.Vars(r)["DocumentKey"], &documentKey, runtime.BindStyledParameterOptions{Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "DocumentKey", Err: err}) - return - } - - // Parameter object where we will unmarshal all parameters from the context - var params UpdateDocumentParams - - headers := r.Header - - // ------------- Optional header parameter "Content-Encoding" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Content-Encoding")]; found { - var ContentEncoding DocumentEncoding - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Content-Encoding", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Content-Encoding", valueList[0], &ContentEncoding, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Content-Encoding", Err: err}) - return - } - - params.ContentEncoding = &ContentEncoding - - } - - // ------------- Optional header parameter "If-Match" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("If-Match")]; found { - var IfMatch string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "If-Match", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "If-Match", valueList[0], &IfMatch, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "If-Match", Err: err}) - return - } - - params.IfMatch = &IfMatch - - } - - // ------------- Optional header parameter "Expires" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Expires")]; found { - var Expires string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Expires", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Expires", valueList[0], &Expires, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Expires", Err: err}) - return - } - - params.Expires = &Expires - - } - - // ------------- Optional header parameter "X-CB-Flags" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("X-CB-Flags")]; found { - var XCBFlags uint32 - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "X-CB-Flags", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "X-CB-Flags", valueList[0], &XCBFlags, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "X-CB-Flags", Err: err}) - return - } - - params.XCBFlags = &XCBFlags - - } - - // ------------- Optional header parameter "X-CB-DurabilityLevel" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("X-CB-DurabilityLevel")]; found { - var XCBDurabilityLevel DurabilityLevel - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "X-CB-DurabilityLevel", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "X-CB-DurabilityLevel", valueList[0], &XCBDurabilityLevel, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: false}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "X-CB-DurabilityLevel", Err: err}) - return - } - - params.XCBDurabilityLevel = &XCBDurabilityLevel - - } - - // ------------- Required header parameter "Authorization" ------------- - if valueList, found := headers[http.CanonicalHeaderKey("Authorization")]; found { - var Authorization string - n := len(valueList) - if n != 1 { - siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Authorization", Count: n}) - return - } - - err = runtime.BindStyledParameterWithOptions("simple", "Authorization", valueList[0], &Authorization, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true}) - if err != nil { - siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Authorization", Err: err}) - return - } - - params.Authorization = Authorization - - } else { - err = fmt.Errorf("Header parameter Authorization is required, but not found") - siw.ErrorHandlerFunc(w, r, &RequiredHeaderError{ParamName: "Authorization", Err: err}) - return - } - - handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.UpdateDocument(w, r, bucketName, scopeName, collectionName, documentKey, params) - })) - - for _, middleware := range siw.HandlerMiddlewares { - handler = middleware(handler) - } - - handler.ServeHTTP(w, r.WithContext(ctx)) -} - -type UnescapedCookieParamError struct { - ParamName string - Err error -} - -func (e *UnescapedCookieParamError) Error() string { - return fmt.Sprintf("error unescaping cookie parameter '%s'", e.ParamName) -} - -func (e *UnescapedCookieParamError) Unwrap() error { - return e.Err -} - -type UnmarshalingParamError struct { - ParamName string - Err error -} - -func (e *UnmarshalingParamError) Error() string { - return fmt.Sprintf("Error unmarshaling parameter %s as JSON: %s", e.ParamName, e.Err.Error()) -} - -func (e *UnmarshalingParamError) Unwrap() error { - return e.Err -} - -type RequiredParamError struct { - ParamName string -} - -func (e *RequiredParamError) Error() string { - return fmt.Sprintf("Query argument %s is required, but not found", e.ParamName) -} - -type RequiredHeaderError struct { - ParamName string - Err error -} - -func (e *RequiredHeaderError) Error() string { - return fmt.Sprintf("Header parameter %s is required, but not found", e.ParamName) -} - -func (e *RequiredHeaderError) Unwrap() error { - return e.Err -} - -type InvalidParamFormatError struct { - ParamName string - Err error -} - -func (e *InvalidParamFormatError) Error() string { - return fmt.Sprintf("Invalid format for parameter %s: %s", e.ParamName, e.Err.Error()) -} - -func (e *InvalidParamFormatError) Unwrap() error { - return e.Err -} - -type TooManyValuesForParamError struct { - ParamName string - Count int -} - -func (e *TooManyValuesForParamError) Error() string { - return fmt.Sprintf("Expected one value for %s, got %d", e.ParamName, e.Count) -} - -// Handler creates http.Handler with routing matching OpenAPI spec. -func Handler(si ServerInterface) http.Handler { - return HandlerWithOptions(si, GorillaServerOptions{}) -} - -type GorillaServerOptions struct { - BaseURL string - BaseRouter *mux.Router - Middlewares []MiddlewareFunc - ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error) -} - -// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux. -func HandlerFromMux(si ServerInterface, r *mux.Router) http.Handler { - return HandlerWithOptions(si, GorillaServerOptions{ - BaseRouter: r, - }) -} - -func HandlerFromMuxWithBaseURL(si ServerInterface, r *mux.Router, baseURL string) http.Handler { - return HandlerWithOptions(si, GorillaServerOptions{ - BaseURL: baseURL, - BaseRouter: r, - }) -} - -// HandlerWithOptions creates http.Handler with additional options -func HandlerWithOptions(si ServerInterface, options GorillaServerOptions) http.Handler { - r := options.BaseRouter - - if r == nil { - r = mux.NewRouter() - } - if options.ErrorHandlerFunc == nil { - options.ErrorHandlerFunc = func(w http.ResponseWriter, r *http.Request, err error) { - http.Error(w, err.Error(), http.StatusBadRequest) - } - } - wrapper := ServerInterfaceWrapper{ - Handler: si, - HandlerMiddlewares: options.Middlewares, - ErrorHandlerFunc: options.ErrorHandlerFunc, - } - - r.HandleFunc(options.BaseURL+"/v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}", wrapper.DeleteDocument).Methods("DELETE") - - r.HandleFunc(options.BaseURL+"/v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}", wrapper.GetDocument).Methods("GET") - - r.HandleFunc(options.BaseURL+"/v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}", wrapper.CreateDocument).Methods("POST") - - r.HandleFunc(options.BaseURL+"/v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}", wrapper.UpdateDocument).Methods("PUT") - - return r -} - -type DeleteDocumentRequestObject struct { - BucketName string `json:"bucketName"` - ScopeName string `json:"scopeName"` - CollectionName string `json:"collectionName"` - DocumentKey string `json:"DocumentKey"` - Params DeleteDocumentParams -} - -type DeleteDocumentResponseObject interface { - VisitDeleteDocumentResponse(w http.ResponseWriter) error -} - -type DeleteDocument200ResponseHeaders struct { - ETag string - XCBMutationToken string -} - -type DeleteDocument200Response struct { - Headers DeleteDocument200ResponseHeaders -} - -func (response DeleteDocument200Response) VisitDeleteDocumentResponse(w http.ResponseWriter) error { - w.Header().Set("ETag", fmt.Sprint(response.Headers.ETag)) - w.Header().Set("X-CB-MutationToken", fmt.Sprint(response.Headers.XCBMutationToken)) - w.WriteHeader(200) - return nil -} - -type GetDocumentRequestObject struct { - BucketName string `json:"bucketName"` - ScopeName string `json:"scopeName"` - CollectionName string `json:"collectionName"` - DocumentKey string `json:"DocumentKey"` - Params GetDocumentParams -} - -type GetDocumentResponseObject interface { - VisitGetDocumentResponse(w http.ResponseWriter) error -} - -type GetDocument200ResponseHeaders struct { - ContentEncoding DocumentEncoding - ETag string - Expires string - XCBFlags uint32 -} - -type GetDocument200AsteriskResponse struct { - Body io.Reader - Headers GetDocument200ResponseHeaders - ContentType string - ContentLength int64 -} - -func (response GetDocument200AsteriskResponse) VisitGetDocumentResponse(w http.ResponseWriter) error { - w.Header().Set("Content-Type", response.ContentType) - if response.ContentLength != 0 { - w.Header().Set("Content-Length", fmt.Sprint(response.ContentLength)) - } - w.Header().Set("Content-Encoding", fmt.Sprint(response.Headers.ContentEncoding)) - w.Header().Set("ETag", fmt.Sprint(response.Headers.ETag)) - w.Header().Set("Expires", fmt.Sprint(response.Headers.Expires)) - w.Header().Set("X-CB-Flags", fmt.Sprint(response.Headers.XCBFlags)) - w.WriteHeader(200) - - if closer, ok := response.Body.(io.ReadCloser); ok { - defer closer.Close() - } - _, err := io.Copy(w, response.Body) - return err -} - -type CreateDocumentRequestObject struct { - BucketName string `json:"bucketName"` - ScopeName string `json:"scopeName"` - CollectionName string `json:"collectionName"` - DocumentKey string `json:"DocumentKey"` - Params CreateDocumentParams - ContentType string - Body io.Reader -} - -type CreateDocumentResponseObject interface { - VisitCreateDocumentResponse(w http.ResponseWriter) error -} - -type CreateDocument200ResponseHeaders struct { - ETag string - XCBMutationToken string -} - -type CreateDocument200Response struct { - Headers CreateDocument200ResponseHeaders -} - -func (response CreateDocument200Response) VisitCreateDocumentResponse(w http.ResponseWriter) error { - w.Header().Set("ETag", fmt.Sprint(response.Headers.ETag)) - w.Header().Set("X-CB-MutationToken", fmt.Sprint(response.Headers.XCBMutationToken)) - w.WriteHeader(200) - return nil -} - -type UpdateDocumentRequestObject struct { - BucketName string `json:"bucketName"` - ScopeName string `json:"scopeName"` - CollectionName string `json:"collectionName"` - DocumentKey string `json:"DocumentKey"` - Params UpdateDocumentParams - ContentType string - Body io.Reader -} - -type UpdateDocumentResponseObject interface { - VisitUpdateDocumentResponse(w http.ResponseWriter) error -} - -type UpdateDocument200ResponseHeaders struct { - ETag string - XCBMutationToken string -} - -type UpdateDocument200Response struct { - Headers UpdateDocument200ResponseHeaders -} - -func (response UpdateDocument200Response) VisitUpdateDocumentResponse(w http.ResponseWriter) error { - w.Header().Set("ETag", fmt.Sprint(response.Headers.ETag)) - w.Header().Set("X-CB-MutationToken", fmt.Sprint(response.Headers.XCBMutationToken)) - w.WriteHeader(200) - return nil -} - -// StrictServerInterface represents all server handlers. -type StrictServerInterface interface { - - // (DELETE /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - DeleteDocument(ctx context.Context, request DeleteDocumentRequestObject) (DeleteDocumentResponseObject, error) - - // (GET /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - GetDocument(ctx context.Context, request GetDocumentRequestObject) (GetDocumentResponseObject, error) - - // (POST /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - CreateDocument(ctx context.Context, request CreateDocumentRequestObject) (CreateDocumentResponseObject, error) - - // (PUT /v1/buckets/{bucketName}/scopes/{scopeName}/collections/{collectionName}/documents/{DocumentKey}) - UpdateDocument(ctx context.Context, request UpdateDocumentRequestObject) (UpdateDocumentResponseObject, error) -} - -type StrictHandlerFunc = strictnethttp.StrictHTTPHandlerFunc -type StrictMiddlewareFunc = strictnethttp.StrictHTTPMiddlewareFunc - -type StrictHTTPServerOptions struct { - RequestErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error) - ResponseErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error) -} - -func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface { - return &strictHandler{ssi: ssi, middlewares: middlewares, options: StrictHTTPServerOptions{ - RequestErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) { - http.Error(w, err.Error(), http.StatusBadRequest) - }, - ResponseErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) { - http.Error(w, err.Error(), http.StatusInternalServerError) - }, - }} -} - -func NewStrictHandlerWithOptions(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc, options StrictHTTPServerOptions) ServerInterface { - return &strictHandler{ssi: ssi, middlewares: middlewares, options: options} -} - -type strictHandler struct { - ssi StrictServerInterface - middlewares []StrictMiddlewareFunc - options StrictHTTPServerOptions -} - -// DeleteDocument operation middleware -func (sh *strictHandler) DeleteDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params DeleteDocumentParams) { - var request DeleteDocumentRequestObject - - request.BucketName = bucketName - request.ScopeName = scopeName - request.CollectionName = collectionName - request.DocumentKey = documentKey - request.Params = params - - handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { - return sh.ssi.DeleteDocument(ctx, request.(DeleteDocumentRequestObject)) - } - for _, middleware := range sh.middlewares { - handler = middleware(handler, "DeleteDocument") - } - - response, err := handler(r.Context(), w, r, request) - - if err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } else if validResponse, ok := response.(DeleteDocumentResponseObject); ok { - if err := validResponse.VisitDeleteDocumentResponse(w); err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } - } else if response != nil { - sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) - } -} - -// GetDocument operation middleware -func (sh *strictHandler) GetDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params GetDocumentParams) { - var request GetDocumentRequestObject - - request.BucketName = bucketName - request.ScopeName = scopeName - request.CollectionName = collectionName - request.DocumentKey = documentKey - request.Params = params - - handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { - return sh.ssi.GetDocument(ctx, request.(GetDocumentRequestObject)) - } - for _, middleware := range sh.middlewares { - handler = middleware(handler, "GetDocument") - } - - response, err := handler(r.Context(), w, r, request) - - if err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } else if validResponse, ok := response.(GetDocumentResponseObject); ok { - if err := validResponse.VisitGetDocumentResponse(w); err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } - } else if response != nil { - sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) - } -} - -// CreateDocument operation middleware -func (sh *strictHandler) CreateDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params CreateDocumentParams) { - var request CreateDocumentRequestObject - - request.BucketName = bucketName - request.ScopeName = scopeName - request.CollectionName = collectionName - request.DocumentKey = documentKey - request.Params = params - request.ContentType = r.Header.Get("Content-Type") - - request.Body = r.Body - - handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { - return sh.ssi.CreateDocument(ctx, request.(CreateDocumentRequestObject)) - } - for _, middleware := range sh.middlewares { - handler = middleware(handler, "CreateDocument") - } - - response, err := handler(r.Context(), w, r, request) - - if err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } else if validResponse, ok := response.(CreateDocumentResponseObject); ok { - if err := validResponse.VisitCreateDocumentResponse(w); err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } - } else if response != nil { - sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) - } -} - -// UpdateDocument operation middleware -func (sh *strictHandler) UpdateDocument(w http.ResponseWriter, r *http.Request, bucketName string, scopeName string, collectionName string, documentKey string, params UpdateDocumentParams) { - var request UpdateDocumentRequestObject - - request.BucketName = bucketName - request.ScopeName = scopeName - request.CollectionName = collectionName - request.DocumentKey = documentKey - request.Params = params - request.ContentType = r.Header.Get("Content-Type") - - request.Body = r.Body - - handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { - return sh.ssi.UpdateDocument(ctx, request.(UpdateDocumentRequestObject)) - } - for _, middleware := range sh.middlewares { - handler = middleware(handler, "UpdateDocument") - } - - response, err := handler(r.Context(), w, r, request) - - if err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } else if validResponse, ok := response.(UpdateDocumentResponseObject); ok { - if err := validResponse.VisitUpdateDocumentResponse(w); err != nil { - sh.options.ResponseErrorHandlerFunc(w, r, err) - } - } else if response != nil { - sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) - } -}