@@ -26,39 +26,110 @@ import (
2626 "github.com/kubeedge/dashboard/errors"
2727)
2828
29+ // --- Route helper 定义 ---
30+ // RouteOpt 是对 RouteBuilder 的配置函数(类似 functional options)
31+ type RouteOpt func (rb * restful.RouteBuilder )
32+
33+ // 常用选项:Param / Reads / Writes / Returns / Doc
34+ func WithParam (p * restful.Parameter ) RouteOpt {
35+ return func (rb * restful.RouteBuilder ) {
36+ rb .Param (p )
37+ }
38+ }
39+ func WithReads (obj interface {}) RouteOpt {
40+ return func (rb * restful.RouteBuilder ) {
41+ rb .Reads (obj )
42+ }
43+ }
44+ func WithWrites (obj interface {}) RouteOpt {
45+ return func (rb * restful.RouteBuilder ) {
46+ rb .Writes (obj )
47+ }
48+ }
49+ func WithReturns (code int , desc string , obj interface {}) RouteOpt {
50+ return func (rb * restful.RouteBuilder ) {
51+ rb .Returns (code , desc , obj )
52+ }
53+ }
54+ func WithDoc (doc string ) RouteOpt {
55+ return func (rb * restful.RouteBuilder ) {
56+ rb .Doc (doc )
57+ }
58+ }
59+
60+ // addRoute 根据 method 动态创建 RouteBuilder 并应用所有 RouteOpt,最后调用 ws.Route(...)
61+ func addRoute (ws * restful.WebService , method string , path string , handler restful.RouteFunction , opts ... RouteOpt ) {
62+ var rb * restful.RouteBuilder
63+ switch method {
64+ case http .MethodGet :
65+ rb = ws .GET (path ).To (handler )
66+ case http .MethodPost :
67+ rb = ws .POST (path ).To (handler )
68+ case http .MethodPut :
69+ rb = ws .PUT (path ).To (handler )
70+ case http .MethodDelete :
71+ rb = ws .DELETE (path ).To (handler )
72+ default :
73+ // 保守回退为 GET(或者根据需要 panic/返回 error)
74+ rb = ws .GET (path ).To (handler )
75+ }
76+
77+ for _ , o := range opts {
78+ o (rb )
79+ }
80+ ws .Route (rb )
81+ }
82+
83+ // --- 使用 helper 的路由注册 ---
84+ // 将原来的 addRoleRoutes 用下面实现替换
2985func (apiHandler * APIHandler ) addRoleRoutes (apiV1Ws * restful.WebService ) * APIHandler {
30- apiV1Ws .Route (
31- apiV1Ws .GET ("/role" ).To (apiHandler .handleGetRoles ).
32- Writes (rbacv1.RoleList {}).
33- Returns (http .StatusOK , "OK" , rbacv1.RoleList {}))
34- apiV1Ws .Route (
35- apiV1Ws .GET ("/role/{namespace}" ).To (apiHandler .handleGetRoles ).
36- Param (apiV1Ws .PathParameter ("namespace" , "Name of the namespace" )).
37- Writes (rbacv1.RoleList {}).
38- Returns (http .StatusOK , "OK" , rbacv1.RoleList {}))
39- apiV1Ws .Route (
40- apiV1Ws .GET ("/role/{namespace}/{name}" ).To (apiHandler .handleGetRole ).
41- Param (apiV1Ws .PathParameter ("namespace" , "Name of the namespace" )).
42- Param (apiV1Ws .PathParameter ("name" , "Name of the role" )).
43- Writes (rbacv1.Role {}).
44- Returns (http .StatusOK , "OK" , rbacv1.Role {}))
45- apiV1Ws .Route (
46- apiV1Ws .POST ("/role/{namespace}" ).To (apiHandler .handleCreateRole ).
47- Param (apiV1Ws .PathParameter ("namespace" , "Name of the namespace" )).
48- Reads (rbacv1.Role {}).
49- Writes (rbacv1.Role {}).
50- Returns (http .StatusCreated , "Created" , rbacv1.Role {}))
51- apiV1Ws .Route (
52- apiV1Ws .PUT ("/role/{namespace}" ).To (apiHandler .handleUpdateRole ).
53- Param (apiV1Ws .PathParameter ("namespace" , "Name of the namespace" )).
54- Reads (rbacv1.Role {}).
55- Writes (rbacv1.Role {}).
56- Returns (http .StatusOK , "OK" , rbacv1.Role {}))
57- apiV1Ws .Route (
58- apiV1Ws .DELETE ("/role/{namespace}/{name}" ).To (apiHandler .handleDeleteRole ).
59- Param (apiV1Ws .PathParameter ("namespace" , "Name of the namespace" )).
60- Param (apiV1Ws .PathParameter ("name" , "Name of the role" )).
61- Returns (http .StatusNoContent , "No Content" , nil ))
86+ // 复用的 path 参数(避免多次创建同样的 Parameter)
87+ nsParam := apiV1Ws .PathParameter ("namespace" , "Name of the namespace" )
88+ nameParam := apiV1Ws .PathParameter ("name" , "Name of the role" )
89+
90+ // GET /role -> 列表(跨 namespace)
91+ addRoute (apiV1Ws , http .MethodGet , "/role" , apiHandler .handleGetRoles ,
92+ WithWrites (rbacv1.RoleList {}),
93+ WithReturns (http .StatusOK , "OK" , rbacv1.RoleList {}),
94+ WithDoc ("Get all roles" ))
95+
96+ // GET /role/{namespace} -> 某 namespace 下的角色列表
97+ addRoute (apiV1Ws , http .MethodGet , "/role/{namespace}" , apiHandler .handleGetRoles ,
98+ WithParam (nsParam ),
99+ WithWrites (rbacv1.RoleList {}),
100+ WithReturns (http .StatusOK , "OK" , rbacv1.RoleList {}),
101+ WithDoc ("Get roles in a namespace" ))
102+
103+ // GET /role/{namespace}/{name} -> 单个角色
104+ addRoute (apiV1Ws , http .MethodGet , "/role/{namespace}/{name}" , apiHandler .handleGetRole ,
105+ WithParam (nsParam ),
106+ WithParam (nameParam ),
107+ WithWrites (rbacv1.Role {}),
108+ WithReturns (http .StatusOK , "OK" , rbacv1.Role {}),
109+ WithDoc ("Get a role by name" ))
110+
111+ // POST /role/{namespace} -> 创建
112+ addRoute (apiV1Ws , http .MethodPost , "/role/{namespace}" , apiHandler .handleCreateRole ,
113+ WithParam (nsParam ),
114+ WithReads (rbacv1.Role {}),
115+ WithWrites (rbacv1.Role {}),
116+ WithReturns (http .StatusCreated , "Created" , rbacv1.Role {}),
117+ WithDoc ("Create a role in a namespace" ))
118+
119+ // PUT /role/{namespace} -> 更新(保留原路径行为)
120+ addRoute (apiV1Ws , http .MethodPut , "/role/{namespace}" , apiHandler .handleUpdateRole ,
121+ WithParam (nsParam ),
122+ WithReads (rbacv1.Role {}),
123+ WithWrites (rbacv1.Role {}),
124+ WithReturns (http .StatusOK , "OK" , rbacv1.Role {}),
125+ WithDoc ("Update a role in a namespace" ))
126+
127+ // DELETE /role/{namespace}/{name} -> 删除
128+ addRoute (apiV1Ws , http .MethodDelete , "/role/{namespace}/{name}" , apiHandler .handleDeleteRole ,
129+ WithParam (nsParam ),
130+ WithParam (nameParam ),
131+ WithReturns (http .StatusNoContent , "No Content" , nil ),
132+ WithDoc ("Delete a role" ))
62133
63134 return apiHandler
64135}
0 commit comments