From aa6783b759a96d12ff7e0b2cb98684311dd7d437 Mon Sep 17 00:00:00 2001 From: chrislusf Date: Sat, 6 Dec 2025 12:30:10 -0800 Subject: refactor: use generic helper for mount/unmount handlers Reduce code duplication by using a generic makePostHandler function that abstracts the common logic of handling POST requests, decoding JSON, calling a manager function, and encoding the JSON response. --- cmd/seaweedfs-mount/main.go | 68 ++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) (limited to 'cmd') diff --git a/cmd/seaweedfs-mount/main.go b/cmd/seaweedfs-mount/main.go index 8d314e9..01ed49d 100644 --- a/cmd/seaweedfs-mount/main.go +++ b/cmd/seaweedfs-mount/main.go @@ -48,47 +48,8 @@ func main() { manager := mountmanager.NewManager(mountmanager.Config{WeedBinary: *weedBinary}) mux := http.NewServeMux() - mux.HandleFunc("/mount", func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - writeError(w, http.StatusMethodNotAllowed, "method not allowed") - return - } - - var req mountmanager.MountRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - writeError(w, http.StatusBadRequest, "invalid request: "+err.Error()) - return - } - - resp, err := manager.Mount(&req) - if err != nil { - writeError(w, http.StatusInternalServerError, err.Error()) - return - } - - writeJSON(w, http.StatusOK, resp) - }) - - mux.HandleFunc("/unmount", func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - writeError(w, http.StatusMethodNotAllowed, "method not allowed") - return - } - - var req mountmanager.UnmountRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - writeError(w, http.StatusBadRequest, "invalid request: "+err.Error()) - return - } - - resp, err := manager.Unmount(&req) - if err != nil { - writeError(w, http.StatusInternalServerError, err.Error()) - return - } - - writeJSON(w, http.StatusOK, resp) - }) + mux.HandleFunc("/mount", makePostHandler(manager.Mount)) + mux.HandleFunc("/unmount", makePostHandler(manager.Unmount)) mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -130,3 +91,28 @@ func writeJSON(w http.ResponseWriter, status int, data interface{}) { func writeError(w http.ResponseWriter, status int, message string) { writeJSON(w, status, mountmanager.ErrorResponse{Error: message}) } + +// makePostHandler creates a generic HTTP POST handler that decodes JSON request, +// calls the manager function, and encodes the JSON response. +func makePostHandler[Req any, Resp any](managerFunc func(*Req) (*Resp, error)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + writeError(w, http.StatusMethodNotAllowed, "method not allowed") + return + } + + var req Req + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeError(w, http.StatusBadRequest, "invalid request: "+err.Error()) + return + } + + resp, err := managerFunc(&req) + if err != nil { + writeError(w, http.StatusInternalServerError, err.Error()) + return + } + + writeJSON(w, http.StatusOK, resp) + } +} -- cgit v1.2.3