diff options
| author | chrislusf <chris.lu@gmail.com> | 2025-12-06 12:30:10 -0800 |
|---|---|---|
| committer | Chris Lu <chrislusf@users.noreply.github.com> | 2025-12-06 18:53:22 -0800 |
| commit | aa6783b759a96d12ff7e0b2cb98684311dd7d437 (patch) | |
| tree | 9ed03009864aac87740751928bd4f7c4d1b53694 /cmd/seaweedfs-mount/main.go | |
| parent | 37123c670b4b97ef5a28417670daca04be91888f (diff) | |
| download | seaweedfs-csi-driver-aa6783b759a96d12ff7e0b2cb98684311dd7d437.tar.xz seaweedfs-csi-driver-aa6783b759a96d12ff7e0b2cb98684311dd7d437.zip | |
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.
Diffstat (limited to 'cmd/seaweedfs-mount/main.go')
| -rw-r--r-- | cmd/seaweedfs-mount/main.go | 68 |
1 files changed, 27 insertions, 41 deletions
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) + } +} |
