-
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) | ||||||||||||
|
||||||||||||
| default: | |
| // 保守回退为 GET(或者根据需要 panic/返回 error) | |
| rb = ws.GET(path).To(handler) | |
| default: | |
| panic("unsupported HTTP method: " + method) |
Outdated
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.
I think we should move these functions to another file to make them reusable
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