mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-08 08:29:06 +00:00
Make / and /v1 work
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/rancher/naok/pkg/resources/common"
|
||||
"github.com/rancher/naok/pkg/resources/counts"
|
||||
"github.com/rancher/naok/pkg/resources/schema"
|
||||
"github.com/rancher/norman/pkg/store/apiroot"
|
||||
"github.com/rancher/norman/pkg/store/proxy"
|
||||
"github.com/rancher/norman/pkg/subscribe"
|
||||
"github.com/rancher/norman/pkg/types"
|
||||
@@ -19,6 +20,7 @@ func SchemaFactory(getter proxy.ClientGetter, as *accesscontrol.AccessStore, k8s
|
||||
counts.Register(baseSchema)
|
||||
subscribe.Register(baseSchema)
|
||||
apigroups.Register(baseSchema, k8s.Discovery())
|
||||
apiroot.Register(baseSchema, []string{"v1"}, nil)
|
||||
|
||||
common.Register(collection, getter)
|
||||
|
||||
|
@@ -36,6 +36,7 @@ func NewHandler(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
|
||||
K8sResource: a.apiHandler(k8sAPI),
|
||||
GenericResource: a.apiHandler(nil),
|
||||
K8sProxy: proxy,
|
||||
APIRoot: a.apiHandler(apiRoot),
|
||||
}), nil
|
||||
}
|
||||
|
||||
|
@@ -36,3 +36,7 @@ func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) {
|
||||
apiOp.Namespaces = []string{namespace}
|
||||
}
|
||||
}
|
||||
|
||||
func apiRoot(sf schema.Factory, apiOp *types.APIRequest) {
|
||||
apiOp.Type = "apiRoot"
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
type Handlers struct {
|
||||
K8sResource http.Handler
|
||||
GenericResource http.Handler
|
||||
APIRoot http.Handler
|
||||
K8sProxy http.Handler
|
||||
}
|
||||
|
||||
@@ -18,6 +19,9 @@ func Routes(h Handlers) http.Handler {
|
||||
m.StrictSlash(true)
|
||||
m.NotFoundHandler = h.K8sProxy
|
||||
|
||||
m.Path("/").Handler(h.APIRoot)
|
||||
m.Path("/{name:v1}").Handler(h.APIRoot)
|
||||
|
||||
m.Path("/v1/{type:schemas}/{name:.*}").Handler(h.GenericResource)
|
||||
m.Path("/v1/{group}.{version}.{resource}").Handler(h.K8sResource)
|
||||
m.Path("/v1/{group}.{version}.{resource}/{nameorns}").Handler(h.K8sResource)
|
||||
|
29
vendor/github.com/rancher/norman/pkg/api/builtin/schema.go
generated
vendored
29
vendor/github.com/rancher/norman/pkg/api/builtin/schema.go
generated
vendored
@@ -1,6 +1,8 @@
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rancher/norman/pkg/store/schema"
|
||||
"github.com/rancher/norman/pkg/types"
|
||||
)
|
||||
@@ -52,30 +54,12 @@ var (
|
||||
},
|
||||
}
|
||||
|
||||
APIRoot = types.Schema{
|
||||
ID: "apiRoot",
|
||||
CollectionMethods: []string{"GET"},
|
||||
ResourceMethods: []string{"GET"},
|
||||
ResourceFields: map[string]types.Field{
|
||||
"apiVersion": {Type: "map[json]"},
|
||||
"path": {Type: "string"},
|
||||
},
|
||||
Formatter: APIRootFormatter,
|
||||
Store: NewAPIRootStore(nil, nil),
|
||||
}
|
||||
|
||||
Schemas = types.EmptySchemas().
|
||||
MustAddSchema(Schema).
|
||||
MustAddSchema(Error).
|
||||
MustAddSchema(Collection).
|
||||
MustAddSchema(APIRoot)
|
||||
MustAddSchema(Collection)
|
||||
)
|
||||
|
||||
func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string {
|
||||
version, _ := apiVersion["version"].(string)
|
||||
return version
|
||||
}
|
||||
|
||||
func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
|
||||
schema := apiOp.Schemas.Schema(resource.ID)
|
||||
if schema == nil {
|
||||
@@ -88,6 +72,13 @@ func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
|
||||
}
|
||||
}
|
||||
|
||||
func getSchemaCollectionLink(apiOp *types.APIRequest, schema *types.Schema) string {
|
||||
if schema != nil && contains(schema.CollectionMethods, http.MethodGet) {
|
||||
return apiOp.URLBuilder.Collection(schema)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func contains(list []string, needle string) bool {
|
||||
for _, v := range list {
|
||||
if v == needle {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package builtin
|
||||
package apiroot
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -7,6 +7,20 @@ import (
|
||||
"github.com/rancher/norman/pkg/types"
|
||||
)
|
||||
|
||||
func Register(schemas *types.Schemas, versions, roots []string) {
|
||||
schemas.MustAddSchema(types.Schema{
|
||||
ID: "apiRoot",
|
||||
CollectionMethods: []string{"GET"},
|
||||
ResourceMethods: []string{"GET"},
|
||||
ResourceFields: map[string]types.Field{
|
||||
"apiVersion": {Type: "map[json]"},
|
||||
"path": {Type: "string"},
|
||||
},
|
||||
Formatter: APIRootFormatter,
|
||||
Store: NewAPIRootStore(versions, roots),
|
||||
})
|
||||
}
|
||||
|
||||
func APIRootFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
|
||||
path, _ := resource.Values["path"].(string)
|
||||
if path == "" {
|
||||
@@ -86,6 +100,7 @@ func (a *APIRootStore) List(apiOp *types.APIRequest, schema *types.Schema, opt *
|
||||
|
||||
func apiVersionToAPIRootMap(version string) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"id": version,
|
||||
"type": "apiRoot",
|
||||
"apiVersion": map[string]interface{}{
|
||||
"version": version,
|
||||
@@ -93,3 +108,17 @@ func apiVersionToAPIRootMap(version string) map[string]interface{} {
|
||||
"path": "/" + version,
|
||||
}
|
||||
}
|
||||
|
||||
func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string {
|
||||
version, _ := apiVersion["version"].(string)
|
||||
return version
|
||||
}
|
||||
|
||||
func contains(list []string, needle string) bool {
|
||||
for _, v := range list {
|
||||
if v == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
3
vendor/github.com/rancher/norman/pkg/urlbuilder/url.go
generated
vendored
3
vendor/github.com/rancher/norman/pkg/urlbuilder/url.go
generated
vendored
@@ -99,6 +99,9 @@ func (u *DefaultURLBuilder) Current() string {
|
||||
}
|
||||
|
||||
func (u *DefaultURLBuilder) RelativeToRoot(path string) string {
|
||||
if len(path) > 0 && path[0] != '/' {
|
||||
return u.responseURLBase + "/" + path
|
||||
}
|
||||
return u.responseURLBase + path
|
||||
}
|
||||
|
||||
|
1
vendor/modules.txt
vendored
1
vendor/modules.txt
vendored
@@ -42,6 +42,7 @@ github.com/pkg/errors
|
||||
github.com/rancher/norman/pkg/authorization
|
||||
github.com/rancher/norman/pkg/types
|
||||
github.com/rancher/norman/pkg/types/convert
|
||||
github.com/rancher/norman/pkg/store/apiroot
|
||||
github.com/rancher/norman/pkg/store/proxy
|
||||
github.com/rancher/norman/pkg/subscribe
|
||||
github.com/rancher/norman/pkg/data
|
||||
|
Reference in New Issue
Block a user