-
Notifications
You must be signed in to change notification settings - Fork 41
refactor: simplify role.go route definitions to reduce duplication #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
76a777d
56e5de8
5a1bf39
8ba6923
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,39 +26,110 @@ import ( | |||||||||||
| "github.com/kubeedge/dashboard/errors" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| // --- Route helper 定义 --- | ||||||||||||
| // RouteOpt 是对 RouteBuilder 的配置函数(类似 functional options) | ||||||||||||
| type RouteOpt func(rb *restful.RouteBuilder) | ||||||||||||
|
|
||||||||||||
| // 常用选项:Param / Reads / Writes / Returns / Doc | ||||||||||||
| func WithParam(p *restful.Parameter) RouteOpt { | ||||||||||||
| return func(rb *restful.RouteBuilder) { | ||||||||||||
| rb.Param(p) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| func WithReads(obj interface{}) RouteOpt { | ||||||||||||
| return func(rb *restful.RouteBuilder) { | ||||||||||||
| rb.Reads(obj) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| func WithWrites(obj interface{}) RouteOpt { | ||||||||||||
| return func(rb *restful.RouteBuilder) { | ||||||||||||
| rb.Writes(obj) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| func WithReturns(code int, desc string, obj interface{}) RouteOpt { | ||||||||||||
| return func(rb *restful.RouteBuilder) { | ||||||||||||
| rb.Returns(code, desc, obj) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| func WithDoc(doc string) RouteOpt { | ||||||||||||
| return func(rb *restful.RouteBuilder) { | ||||||||||||
| rb.Doc(doc) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // addRoute 根据 method 动态创建 RouteBuilder 并应用所有 RouteOpt,最后调用 ws.Route(...) | ||||||||||||
| func addRoute(ws *restful.WebService, method string, path string, handler restful.RouteFunction, opts ...RouteOpt) { | ||||||||||||
| var rb *restful.RouteBuilder | ||||||||||||
| switch method { | ||||||||||||
| case http.MethodGet: | ||||||||||||
| rb = ws.GET(path).To(handler) | ||||||||||||
| case http.MethodPost: | ||||||||||||
| rb = ws.POST(path).To(handler) | ||||||||||||
| case http.MethodPut: | ||||||||||||
| rb = ws.PUT(path).To(handler) | ||||||||||||
| case http.MethodDelete: | ||||||||||||
| rb = ws.DELETE(path).To(handler) | ||||||||||||
| default: | ||||||||||||
| // 保守回退为 GET(或者根据需要 panic/返回 error) | ||||||||||||
| rb = ws.GET(path).To(handler) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Since this function is part of the application setup, panicking is an appropriate way to signal a programming error immediately.
Suggested change
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| for _, o := range opts { | ||||||||||||
| o(rb) | ||||||||||||
| } | ||||||||||||
| ws.Route(rb) | ||||||||||||
| } | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should move these functions to another file to make them reusable |
||||||||||||
|
|
||||||||||||
| // --- 使用 helper 的路由注册 --- | ||||||||||||
| // 将原来的 addRoleRoutes 用下面实现替换 | ||||||||||||
| func (apiHandler *APIHandler) addRoleRoutes(apiV1Ws *restful.WebService) *APIHandler { | ||||||||||||
| apiV1Ws.Route( | ||||||||||||
| apiV1Ws.GET("/role").To(apiHandler.handleGetRoles). | ||||||||||||
| Writes(rbacv1.RoleList{}). | ||||||||||||
| Returns(http.StatusOK, "OK", rbacv1.RoleList{})) | ||||||||||||
| apiV1Ws.Route( | ||||||||||||
| apiV1Ws.GET("/role/{namespace}").To(apiHandler.handleGetRoles). | ||||||||||||
| Param(apiV1Ws.PathParameter("namespace", "Name of the namespace")). | ||||||||||||
| Writes(rbacv1.RoleList{}). | ||||||||||||
| Returns(http.StatusOK, "OK", rbacv1.RoleList{})) | ||||||||||||
| apiV1Ws.Route( | ||||||||||||
| apiV1Ws.GET("/role/{namespace}/{name}").To(apiHandler.handleGetRole). | ||||||||||||
| Param(apiV1Ws.PathParameter("namespace", "Name of the namespace")). | ||||||||||||
| Param(apiV1Ws.PathParameter("name", "Name of the role")). | ||||||||||||
| Writes(rbacv1.Role{}). | ||||||||||||
| Returns(http.StatusOK, "OK", rbacv1.Role{})) | ||||||||||||
| apiV1Ws.Route( | ||||||||||||
| apiV1Ws.POST("/role/{namespace}").To(apiHandler.handleCreateRole). | ||||||||||||
| Param(apiV1Ws.PathParameter("namespace", "Name of the namespace")). | ||||||||||||
| Reads(rbacv1.Role{}). | ||||||||||||
| Writes(rbacv1.Role{}). | ||||||||||||
| Returns(http.StatusCreated, "Created", rbacv1.Role{})) | ||||||||||||
| apiV1Ws.Route( | ||||||||||||
| apiV1Ws.PUT("/role/{namespace}").To(apiHandler.handleUpdateRole). | ||||||||||||
| Param(apiV1Ws.PathParameter("namespace", "Name of the namespace")). | ||||||||||||
| Reads(rbacv1.Role{}). | ||||||||||||
| Writes(rbacv1.Role{}). | ||||||||||||
| Returns(http.StatusOK, "OK", rbacv1.Role{})) | ||||||||||||
| apiV1Ws.Route( | ||||||||||||
| apiV1Ws.DELETE("/role/{namespace}/{name}").To(apiHandler.handleDeleteRole). | ||||||||||||
| Param(apiV1Ws.PathParameter("namespace", "Name of the namespace")). | ||||||||||||
| Param(apiV1Ws.PathParameter("name", "Name of the role")). | ||||||||||||
| Returns(http.StatusNoContent, "No Content", nil)) | ||||||||||||
| // 复用的 path 参数(避免多次创建同样的 Parameter) | ||||||||||||
| nsParam := apiV1Ws.PathParameter("namespace", "Name of the namespace") | ||||||||||||
| nameParam := apiV1Ws.PathParameter("name", "Name of the role") | ||||||||||||
|
|
||||||||||||
| // GET /role -> 列表(跨 namespace) | ||||||||||||
| addRoute(apiV1Ws, http.MethodGet, "/role", apiHandler.handleGetRoles, | ||||||||||||
| WithWrites(rbacv1.RoleList{}), | ||||||||||||
| WithReturns(http.StatusOK, "OK", rbacv1.RoleList{}), | ||||||||||||
| WithDoc("Get all roles")) | ||||||||||||
|
|
||||||||||||
| // GET /role/{namespace} -> 某 namespace 下的角色列表 | ||||||||||||
| addRoute(apiV1Ws, http.MethodGet, "/role/{namespace}", apiHandler.handleGetRoles, | ||||||||||||
| WithParam(nsParam), | ||||||||||||
| WithWrites(rbacv1.RoleList{}), | ||||||||||||
| WithReturns(http.StatusOK, "OK", rbacv1.RoleList{}), | ||||||||||||
| WithDoc("Get roles in a namespace")) | ||||||||||||
|
|
||||||||||||
| // GET /role/{namespace}/{name} -> 单个角色 | ||||||||||||
| addRoute(apiV1Ws, http.MethodGet, "/role/{namespace}/{name}", apiHandler.handleGetRole, | ||||||||||||
| WithParam(nsParam), | ||||||||||||
| WithParam(nameParam), | ||||||||||||
| WithWrites(rbacv1.Role{}), | ||||||||||||
| WithReturns(http.StatusOK, "OK", rbacv1.Role{}), | ||||||||||||
| WithDoc("Get a role by name")) | ||||||||||||
|
|
||||||||||||
| // POST /role/{namespace} -> 创建 | ||||||||||||
| addRoute(apiV1Ws, http.MethodPost, "/role/{namespace}", apiHandler.handleCreateRole, | ||||||||||||
| WithParam(nsParam), | ||||||||||||
| WithReads(rbacv1.Role{}), | ||||||||||||
| WithWrites(rbacv1.Role{}), | ||||||||||||
| WithReturns(http.StatusCreated, "Created", rbacv1.Role{}), | ||||||||||||
| WithDoc("Create a role in a namespace")) | ||||||||||||
|
|
||||||||||||
| // PUT /role/{namespace} -> 更新(保留原路径行为) | ||||||||||||
| addRoute(apiV1Ws, http.MethodPut, "/role/{namespace}", apiHandler.handleUpdateRole, | ||||||||||||
| WithParam(nsParam), | ||||||||||||
| WithReads(rbacv1.Role{}), | ||||||||||||
| WithWrites(rbacv1.Role{}), | ||||||||||||
| WithReturns(http.StatusOK, "OK", rbacv1.Role{}), | ||||||||||||
| WithDoc("Update a role in a namespace")) | ||||||||||||
|
|
||||||||||||
| // DELETE /role/{namespace}/{name} -> 删除 | ||||||||||||
| addRoute(apiV1Ws, http.MethodDelete, "/role/{namespace}/{name}", apiHandler.handleDeleteRole, | ||||||||||||
| WithParam(nsParam), | ||||||||||||
| WithParam(nameParam), | ||||||||||||
| WithReturns(http.StatusNoContent, "No Content", nil), | ||||||||||||
| WithDoc("Delete a role")) | ||||||||||||
|
|
||||||||||||
| return apiHandler | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write comments in English