37 Star 411 Fork 76

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"
v3 "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))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.11-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385