This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include supported API versions in error message
- Loading branch information
1 parent
87031bf
commit c776d48
Showing
3 changed files
with
26 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,42 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/skygeario/skygear-server/pkg/core/apiversion" | ||
"github.com/skygeario/skygear-server/pkg/core/handler" | ||
"github.com/skygeario/skygear-server/pkg/core/skyerr" | ||
) | ||
|
||
var ErrIncompatibleAPIVersion = skyerr.NotFound.WithReason("IncompatibleAPIVersion").New("incompatible API version") | ||
var IncompatibleAPIVersion = skyerr.NotFound.WithReason("IncompatibleAPIVersion") | ||
|
||
// APIVersionMiddleware compares the API version with own version. | ||
type APIVersionMiddleware struct { | ||
// APIVersionName tells how to extract the API version from mux.Vars. | ||
APIVersionName string | ||
// MajorVersion determines the supported major version. | ||
// It should be apiversion.MajorVersion in normal use case. | ||
MajorVersion int | ||
// MinorVersion determines the supported minor version. | ||
// It should be apiversion.MinorVersion in normal use case. | ||
MinorVersion int | ||
// SupportedVersions determines the full list of supported versions. | ||
SupportedVersions []string | ||
// SupportedVersionsJSON is the JSON encoding of SupportedVersions. | ||
SupportedVersionsJSON string | ||
} | ||
|
||
func (m *APIVersionMiddleware) Handle(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
apiVersion := mux.Vars(r)[m.APIVersionName] | ||
major, minor, ok := apiversion.Parse(apiVersion) | ||
|
||
if !ok || major != m.MajorVersion || minor > m.MinorVersion { | ||
handler.WriteResponse(w, handler.APIResponse{ | ||
Error: ErrIncompatibleAPIVersion, | ||
}) | ||
return | ||
for _, supported := range m.SupportedVersions { | ||
if apiVersion == supported { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
} | ||
next.ServeHTTP(w, r) | ||
|
||
handler.WriteResponse(w, handler.APIResponse{ | ||
Error: IncompatibleAPIVersion.New( | ||
fmt.Sprintf("expected API versions: %s", m.SupportedVersionsJSON), | ||
), | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters