mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-06 21:12:24 +00:00
Refactor handler methods to use instance methods for logging and error handling; add utility functions for UUID generation and JSON error responses.
This commit is contained in:
@@ -12,24 +12,24 @@ import (
|
||||
)
|
||||
|
||||
func (h *HandlerV1) _handle() {
|
||||
uuid16 := newUUID()
|
||||
_log.Info("Received request", slog.String("version", "v1"), slog.String("connection-uuid", uuid16), slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()))
|
||||
uuid16 := h.newUUID()
|
||||
h.log.Info("Received request", slog.String("version", "v1"), slog.String("connection-uuid", uuid16), slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()))
|
||||
|
||||
cmd := chi.URLParam(h.r, "cmd")
|
||||
if !allowedCmd.MatchString(string([]rune(cmd)[0])) {
|
||||
writeJSONError(h.w, http.StatusBadRequest, "invalid command")
|
||||
_log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "invalid command"), slog.String("cmd", cmd), slog.Int("status", http.StatusBadRequest))
|
||||
if !h.allowedCmd.MatchString(string([]rune(cmd)[0])) {
|
||||
h.writeJSONError(http.StatusBadRequest, "invalid command")
|
||||
h.log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "invalid command"), slog.String("cmd", cmd), slog.Int("status", http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
if !listAllowedCmd.MatchString(cmd) {
|
||||
writeJSONError(h.w, http.StatusBadRequest, "invalid command")
|
||||
_log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "invalid command"), slog.String("cmd", cmd), slog.Int("status", http.StatusBadRequest))
|
||||
if !h.listAllowedCmd.MatchString(cmd) {
|
||||
h.writeJSONError(http.StatusBadRequest, "invalid command")
|
||||
h.log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "invalid command"), slog.String("cmd", cmd), slog.Int("status", http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
scriptPath := filepath.Join(cfg.ComDir, cmd+".lua")
|
||||
scriptPath := filepath.Join(h.cfg.ComDir, cmd+".lua")
|
||||
if _, err := os.Stat(scriptPath); err != nil {
|
||||
writeJSONError(h.w, http.StatusNotFound, "command not found")
|
||||
_log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "command not found"), slog.String("cmd", cmd), slog.Int("status", http.StatusNotFound))
|
||||
h.writeJSONError(http.StatusNotFound, "command not found")
|
||||
h.log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "command not found"), slog.String("cmd", cmd), slog.Int("status", http.StatusNotFound))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ func (h *HandlerV1) _handle() {
|
||||
`)
|
||||
|
||||
if err := L.DoFile(scriptPath); err != nil {
|
||||
writeJSONError(h.w, http.StatusInternalServerError, "lua error: "+err.Error())
|
||||
_log.Error("Failed to execute lua script", slog.String("connection-uuid", uuid16), slog.String("error", err.Error()))
|
||||
h.writeJSONError(http.StatusInternalServerError, "lua error: "+err.Error())
|
||||
h.log.Error("Failed to execute lua script", slog.String("connection-uuid", uuid16), slog.String("error", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -78,15 +78,15 @@ func (h *HandlerV1) _handle() {
|
||||
})
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
h.w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(h.w).Encode(out)
|
||||
switch out["status"] {
|
||||
case "error":
|
||||
_log.Info("Command executed with error", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
|
||||
h.log.Info("Command executed with error", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
|
||||
case "ok":
|
||||
_log.Info("Command executed successfully", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
|
||||
h.log.Info("Command executed successfully", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
|
||||
default:
|
||||
_log.Info("Command executed and returned an unknown status", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
|
||||
h.log.Info("Command executed and returned an unknown status", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
|
||||
}
|
||||
_log.Info("Session completed", slog.String("connection-uuid", uuid16), slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()))
|
||||
h.log.Info("Session completed", slog.String("connection-uuid", uuid16), slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user