Skip to content

Commit 503afe7

Browse files
committed
feat: add dashboard API v2 pagination endpoints (#5351)
1 parent 9ea1d86 commit 503afe7

5 files changed

Lines changed: 752 additions & 0 deletions

File tree

pkg/util/http/handler.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ type GeneralResponse struct {
2626
Msg string
2727
}
2828

29+
type V2Response struct {
30+
Code int `json:"code"`
31+
Msg string `json:"msg"`
32+
Data any `json:"data"`
33+
}
34+
2935
// APIHandler is a handler function that returns a response object or an error.
3036
type APIHandler func(ctx *Context) (any, error)
3137

@@ -64,3 +70,27 @@ func MakeHTTPHandlerFunc(handler APIHandler) http.HandlerFunc {
6470
}
6571
}
6672
}
73+
74+
// MakeHTTPHandlerFuncV2 wraps a handler response in the dashboard API v2 envelope.
75+
func MakeHTTPHandlerFuncV2(handler APIHandler) http.HandlerFunc {
76+
return func(w http.ResponseWriter, r *http.Request) {
77+
ctx := NewContext(w, r)
78+
res, err := handler(ctx)
79+
if err != nil {
80+
log.Warnf("http response [%s]: error: %v", r.URL.Path, err)
81+
code := http.StatusInternalServerError
82+
if e, ok := err.(*Error); ok {
83+
code = e.Code
84+
}
85+
86+
w.Header().Set("Content-Type", "application/json")
87+
w.WriteHeader(code)
88+
_ = json.NewEncoder(w).Encode(V2Response{Code: code, Msg: err.Error(), Data: nil})
89+
return
90+
}
91+
92+
w.Header().Set("Content-Type", "application/json")
93+
w.WriteHeader(http.StatusOK)
94+
_ = json.NewEncoder(w).Encode(V2Response{Code: http.StatusOK, Msg: "success", Data: res})
95+
}
96+
}

server/api_router.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper)
4848
subRouter.HandleFunc("/api/clients/{key}", httppkg.MakeHTTPHandlerFunc(apiController.APIClientDetail)).Methods("GET")
4949
subRouter.HandleFunc("/api/proxies", httppkg.MakeHTTPHandlerFunc(apiController.DeleteProxies)).Methods("DELETE")
5050

51+
subRouter.HandleFunc("/api/v2/users", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2UserList)).Methods("GET")
52+
subRouter.HandleFunc("/api/v2/clients", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ClientList)).Methods("GET")
53+
subRouter.HandleFunc("/api/v2/clients/{key}", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ClientDetail)).Methods("GET")
54+
subRouter.HandleFunc("/api/v2/proxies", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ProxyList)).Methods("GET")
55+
subRouter.HandleFunc("/api/v2/proxies/{name}", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ProxyDetail)).Methods("GET")
56+
5157
// view
5258
subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET")
5359
subRouter.PathPrefix("/static/").Handler(

0 commit comments

Comments
 (0)