1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-21 12:37:45 +00:00
steve/pkg/server/publicapi/apiserver.go

100 lines
2.3 KiB
Go
Raw Normal View History

2019-08-14 18:08:34 +00:00
package publicapi
2019-08-04 17:41:32 +00:00
import (
"net/http"
2019-08-13 23:36:03 +00:00
"strings"
2019-08-12 22:15:19 +00:00
2019-08-04 17:41:32 +00:00
"github.com/rancher/naok/pkg/accesscontrol"
"github.com/rancher/naok/pkg/attributes"
2019-08-08 05:41:31 +00:00
k8sproxy "github.com/rancher/naok/pkg/proxy"
2019-08-13 23:36:03 +00:00
"github.com/rancher/naok/pkg/resources/schema"
2019-08-14 18:08:34 +00:00
"github.com/rancher/naok/pkg/server/router"
2019-08-04 17:41:32 +00:00
"github.com/rancher/norman/pkg/api"
"github.com/rancher/norman/pkg/types"
"github.com/rancher/norman/pkg/urlbuilder"
"k8s.io/apiserver/pkg/authentication/user"
2019-08-08 05:41:31 +00:00
"k8s.io/client-go/rest"
2019-08-04 17:41:32 +00:00
)
2019-08-14 18:08:34 +00:00
func NewHandler(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
2019-08-08 05:41:31 +00:00
var (
err error
)
2019-08-04 17:41:32 +00:00
a := &apiServer{
2019-08-13 23:36:03 +00:00
sf: sf,
2019-08-14 18:08:34 +00:00
server: api.DefaultAPIServer(),
2019-08-04 17:41:32 +00:00
}
2019-08-14 18:08:34 +00:00
a.server.AccessControl = accesscontrol.NewAccessControl()
2019-08-08 05:41:31 +00:00
2019-08-14 18:08:34 +00:00
proxy, err := k8sproxy.Handler("/", cfg)
2019-08-08 05:41:31 +00:00
if err != nil {
return nil, err
}
2019-08-14 18:08:34 +00:00
return router.Routes(router.Handlers{
K8sResource: a.apiHandler(k8sAPI),
GenericResource: a.apiHandler(nil),
K8sProxy: proxy,
2019-08-14 20:39:08 +00:00
APIRoot: a.apiHandler(apiRoot),
2019-08-14 18:08:34 +00:00
}), nil
2019-08-04 17:41:32 +00:00
}
type apiServer struct {
2019-08-13 23:36:03 +00:00
sf schema.Factory
server *api.Server
2019-08-04 17:41:32 +00:00
}
func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.APIRequest, bool) {
user := &user.DefaultInfo{
Name: "admin",
Groups: []string{"system:masters"},
}
2019-08-13 23:36:03 +00:00
schemas, err := a.sf.Schemas(user)
2019-08-04 17:41:32 +00:00
if err != nil {
rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError)
}
urlBuilder, err := urlbuilder.New(req, a, schemas)
if err != nil {
rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError)
return nil, false
}
return &types.APIRequest{
Schemas: schemas,
Request: req,
Response: rw,
URLBuilder: urlBuilder,
}, true
}
func (a *apiServer) Schema(base string, schema *types.Schema) string {
gvr := attributes.GVR(schema)
2019-08-13 23:36:03 +00:00
if gvr.Resource == "" {
return urlbuilder.ConstructBasicURL(base, "v1", schema.PluralName)
2019-08-04 17:41:32 +00:00
}
2019-08-13 23:36:03 +00:00
return urlbuilder.ConstructBasicURL(base, "v1", strings.ToLower(schema.ID))
2019-08-04 17:41:32 +00:00
}
2019-08-14 18:08:34 +00:00
type APIFunc func(schema.Factory, *types.APIRequest)
func (a *apiServer) apiHandler(apiFunc APIFunc) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
a.api(rw, req, apiFunc)
})
}
func (a *apiServer) api(rw http.ResponseWriter, req *http.Request, apiFunc APIFunc) {
apiOp, ok := a.common(rw, req)
if ok {
if apiFunc != nil {
apiFunc(a.sf, apiOp)
}
a.server.Handle(apiOp)
}
}