You've already forked openaccounting-server
mirror of
https://github.com/openaccounting/oa-server.git
synced 2025-12-09 00:50:59 +13:00
initial commit
This commit is contained in:
48
core/api/version.go
Normal file
48
core/api/version.go
Normal file
@@ -0,0 +1,48 @@
|
||||
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)
|
||||
}
|
||||
|
||||
serverVersion, _ := semver.NewVersion("1.0.0")
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user