Refactor llama server command handlers to use a common execution function

This commit is contained in:
2025-10-26 11:00:10 +01:00
parent 356c5be2c6
commit 4e587953d8

View File

@@ -235,6 +235,19 @@ func (h *Handler) ParseVllmCommand() http.HandlerFunc {
} }
} }
// executeLlamaServerCommand executes a llama-server command with the specified flag and returns the output
func (h *Handler) executeLlamaServerCommand(flag, errorMsg string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("llama-server", flag)
output, err := cmd.CombinedOutput()
if err != nil {
writeError(w, http.StatusInternalServerError, "command failed", errorMsg+": "+err.Error())
return
}
writeText(w, http.StatusOK, string(output))
}
}
// LlamaServerHelpHandler godoc // LlamaServerHelpHandler godoc
// @Summary Get help for llama server // @Summary Get help for llama server
// @Description Returns the help text for the llama server command // @Description Returns the help text for the llama server command
@@ -245,15 +258,7 @@ func (h *Handler) ParseVllmCommand() http.HandlerFunc {
// @Failure 500 {string} string "Internal Server Error" // @Failure 500 {string} string "Internal Server Error"
// @Router /backends/llama-cpp/help [get] // @Router /backends/llama-cpp/help [get]
func (h *Handler) LlamaServerHelpHandler() http.HandlerFunc { func (h *Handler) LlamaServerHelpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return h.executeLlamaServerCommand("--help", "Failed to get help")
helpCmd := exec.Command("llama-server", "--help")
output, err := helpCmd.CombinedOutput()
if err != nil {
writeError(w, http.StatusInternalServerError, "command_failed", "Failed to get help: "+err.Error())
return
}
writeText(w, http.StatusOK, string(output))
}
} }
// LlamaServerVersionHandler godoc // LlamaServerVersionHandler godoc
@@ -266,15 +271,7 @@ func (h *Handler) LlamaServerHelpHandler() http.HandlerFunc {
// @Failure 500 {string} string "Internal Server Error" // @Failure 500 {string} string "Internal Server Error"
// @Router /backends/llama-cpp/version [get] // @Router /backends/llama-cpp/version [get]
func (h *Handler) LlamaServerVersionHandler() http.HandlerFunc { func (h *Handler) LlamaServerVersionHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return h.executeLlamaServerCommand("--version", "Failed to get version")
versionCmd := exec.Command("llama-server", "--version")
output, err := versionCmd.CombinedOutput()
if err != nil {
writeError(w, http.StatusInternalServerError, "command_failed", "Failed to get version: "+err.Error())
return
}
writeText(w, http.StatusOK, string(output))
}
} }
// LlamaServerListDevicesHandler godoc // LlamaServerListDevicesHandler godoc
@@ -287,13 +284,5 @@ func (h *Handler) LlamaServerVersionHandler() http.HandlerFunc {
// @Failure 500 {string} string "Internal Server Error" // @Failure 500 {string} string "Internal Server Error"
// @Router /backends/llama-cpp/devices [get] // @Router /backends/llama-cpp/devices [get]
func (h *Handler) LlamaServerListDevicesHandler() http.HandlerFunc { func (h *Handler) LlamaServerListDevicesHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return h.executeLlamaServerCommand("--list-devices", "Failed to list devices")
listCmd := exec.Command("llama-server", "--list-devices")
output, err := listCmd.CombinedOutput()
if err != nil {
writeError(w, http.StatusInternalServerError, "command_failed", "Failed to list devices: "+err.Error())
return
}
writeText(w, http.StatusOK, string(output))
}
} }