37 Star 407 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
router.go 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
Dax McDonald 提交于 2019-06-17 14:49 . Add websocket origin check
package clusterrouter
import (
"encoding/json"
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
"k8s.io/client-go/rest"
)
type Router struct {
serverFactory *factory
}
func New(localConfig *rest.Config, lookup ClusterLookup, dialer dialer.Factory, clusterLister v3.ClusterLister) http.Handler {
serverFactory := newFactory(localConfig, dialer, lookup, clusterLister)
return &Router{
serverFactory: serverFactory,
}
}
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
c, handler, err := r.serverFactory.get(req)
if err != nil {
e, ok := err.(*httperror.APIError)
if ok {
response(rw, e.Code, e.Message)
} else {
response(rw, httperror.ServerError, err.Error())
}
return
}
if c == nil {
response(rw, httperror.NotFound, "No cluster available")
return
}
handler.ServeHTTP(rw, req)
}
func response(rw http.ResponseWriter, code httperror.ErrorCode, message string) {
rw.WriteHeader(code.Status)
rw.Header().Set("content-type", "application/json")
json.NewEncoder(rw).Encode(httperror.NewAPIError(code, message))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.12-rc5

搜索帮助