Documentation ¶
Overview ¶
Package versionware provides routing and middleware for building versioned HTTP services.
Index ¶
Examples ¶
Constants ¶
View Source
const ( // HeaderSnykVersionRequested is a response header acknowledging the API // version that was requested. HeaderSnykVersionRequested = "snyk-version-requested" // HeaderSnykVersionServed is a response header indicating the actual API // version that was matched and served the response. HeaderSnykVersionServed = "snyk-version-served" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a multiplexing http.Handler that dispatches requests based on the version query parameter according to vervet's API version matching rules.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/snyk/vervet" "github.com/snyk/vervet/versionware" ) func main() { h := versionware.NewHandler([]versionware.VersionHandler{{ Version: vervet.MustParseVersion("2021-10-01"), Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if _, err := w.Write([]byte("oct")); err != nil { panic(err) } }), }, { Version: vervet.MustParseVersion("2021-11-01"), Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if _, err := w.Write([]byte("nov")); err != nil { panic(err) } }), }, { Version: vervet.MustParseVersion("2021-09-01"), Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if _, err := w.Write([]byte("sept")); err != nil { panic(err) } }), }}...) s := httptest.NewServer(h) defer s.Close() resp, err := s.Client().Get(s.URL + "?version=2021-10-31") if err != nil { panic(err) } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } fmt.Print(string(respBody)) }
Output: oct
func NewHandler ¶
func NewHandler(vhs ...VersionHandler) *Handler
NewHandler returns a new Handler instance, which handles versioned requests with the matching version handler.
func (*Handler) HandleErrors ¶
func (h *Handler) HandleErrors(errFunc VersionErrorHandler)
HandleErrors changes the default error handler to the provided function. It may be used to control the format of versioning error responses.
type VersionErrorHandler ¶
VersionErrorHandler defines a function which handles versioning error responses in requests.
Click to show internal directories.
Click to hide internal directories.