2018-10-19 15:31:41 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/Masterminds/semver"
|
|
|
|
|
"github.com/ant0ine/go-json-rest/rest"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type VersionMiddleware struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MiddlewareFunc makes AuthMiddleware implement the Middleware interface.
|
|
|
|
|
func (mw *VersionMiddleware) MiddlewareFunc(handler rest.HandlerFunc) rest.HandlerFunc {
|
|
|
|
|
return func(writer rest.ResponseWriter, request *rest.Request) {
|
|
|
|
|
version := request.Header.Get("Accept-Version")
|
|
|
|
|
|
|
|
|
|
// Don't require version header for websockets
|
|
|
|
|
if request.URL.String() == "/ws" {
|
|
|
|
|
handler(writer, request)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if version == "" {
|
|
|
|
|
rest.Error(writer, "Accept-Version header required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constraint, err := semver.NewConstraint(version)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
rest.Error(writer, "Invalid version", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 14:09:22 -02:00
|
|
|
serverVersion, _ := semver.NewVersion("1.1.0")
|
2018-10-19 15:31:41 -04:00
|
|
|
// Pre-release versions
|
|
|
|
|
compatVersion, _ := semver.NewVersion("0.1.8")
|
|
|
|
|
|
|
|
|
|
versionMatch := constraint.Check(serverVersion)
|
|
|
|
|
compatMatch := constraint.Check(compatVersion)
|
|
|
|
|
|
|
|
|
|
if versionMatch == false && compatMatch == false {
|
|
|
|
|
rest.Error(writer, "Invalid version", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handler(writer, request)
|
|
|
|
|
}
|
|
|
|
|
}
|