From 6f571dd91c39e9670dbe2ee99664696855d9f07a Mon Sep 17 00:00:00 2001 From: LordMathis Date: Thu, 17 Jul 2025 21:46:26 +0200 Subject: [PATCH] Refactor server handlers and routing to use a handler struct --- server/cmd/llamactl.go | 11 +++++- server/pkg/handlers.go | 78 +++++++++++++++++++++++++++++------------- server/pkg/instance.go | 13 +++++++ server/pkg/routes.go | 10 +++--- 4 files changed, 82 insertions(+), 30 deletions(-) diff --git a/server/cmd/llamactl.go b/server/cmd/llamactl.go index dba6bc9..98dfa57 100644 --- a/server/cmd/llamactl.go +++ b/server/cmd/llamactl.go @@ -13,7 +13,16 @@ import ( // @license.url https://opensource.org/license/mit/ // @basePath /api/v1 func main() { - r := llamactl.SetupRouter() + + // Initialize the instance manager + instanceManager := llamactl.NewInstanceManager() + + // Create a new handler with the instance manager + handler := llamactl.NewHandler(instanceManager) + + // Setup the router with the handler + r := llamactl.SetupRouter(handler) + // Start the server with the router fmt.Println("Starting llamactl on port 8080...") http.ListenAndServe(":8080", r) diff --git a/server/pkg/handlers.go b/server/pkg/handlers.go index 0bebd44..22504be 100644 --- a/server/pkg/handlers.go +++ b/server/pkg/handlers.go @@ -1,10 +1,22 @@ package llamactl import ( + "encoding/json" + "fmt" "net/http" "os/exec" ) +type Handler struct { + InstanceManager InstanceManager +} + +func NewHandler(im InstanceManager) *Handler { + return &Handler{ + InstanceManager: im, + } +} + // HelpHandler godoc // @Summary Get help for llama server // @Description Returns the help text for the llama server command @@ -13,15 +25,17 @@ import ( // @Success 200 {string} string "Help text" // @Failure 500 {string} string "Internal Server Error" // @Router /server/help [get] -func HelpHandler(w http.ResponseWriter, r *http.Request) { - helpCmd := exec.Command("llama-server", "--help") - output, err := helpCmd.CombinedOutput() - if err != nil { - http.Error(w, "Failed to get help: "+err.Error(), http.StatusInternalServerError) - return +func (h *Handler) HelpHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + helpCmd := exec.Command("llama-server", "--help") + output, err := helpCmd.CombinedOutput() + if err != nil { + http.Error(w, "Failed to get help: "+err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/plain") + w.Write(output) } - w.Header().Set("Content-Type", "text/plain") - w.Write(output) } // VersionHandler godoc @@ -32,15 +46,17 @@ func HelpHandler(w http.ResponseWriter, r *http.Request) { // @Success 200 {string} string "Version information" // @Failure 500 {string} string "Internal Server Error" // @Router /server/version [get] -func VersionHandler(w http.ResponseWriter, r *http.Request) { - versionCmd := exec.Command("llama-server", "--version") - output, err := versionCmd.CombinedOutput() - if err != nil { - http.Error(w, "Failed to get version: "+err.Error(), http.StatusInternalServerError) - return +func (h *Handler) VersionHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + versionCmd := exec.Command("llama-server", "--version") + output, err := versionCmd.CombinedOutput() + if err != nil { + http.Error(w, "Failed to get version: "+err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/plain") + w.Write(output) } - w.Header().Set("Content-Type", "text/plain") - w.Write(output) } // ListDevicesHandler godoc @@ -51,15 +67,29 @@ func VersionHandler(w http.ResponseWriter, r *http.Request) { // @Success 200 {string} string "List of devices" // @Failure 500 {string} string "Internal Server Error" // @Router /server/devices [get] -func ListDevicesHandler(w http.ResponseWriter, r *http.Request) { - listCmd := exec.Command("llama-server", "--list-devices") - output, err := listCmd.CombinedOutput() - if err != nil { - http.Error(w, "Failed to list devices: "+err.Error(), http.StatusInternalServerError) - return +func (h *Handler) ListDevicesHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + listCmd := exec.Command("llama-server", "--list-devices") + output, err := listCmd.CombinedOutput() + if err != nil { + http.Error(w, "Failed to list devices: "+err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/plain") + w.Write(output) + } +} + +func (h *Handler) StartHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var requestBody InstanceOptions + if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil { + fmt.Println("Error decoding request body:", err) + http.Error(w, "Invalid request body", http.StatusBadRequest) + return + } + } - w.Header().Set("Content-Type", "text/plain") - w.Write(output) } // func launchHandler(w http.ResponseWriter, r *http.Request) { diff --git a/server/pkg/instance.go b/server/pkg/instance.go index 2058152..fb8fa7f 100644 --- a/server/pkg/instance.go +++ b/server/pkg/instance.go @@ -4,3 +4,16 @@ type Instance struct { Status string Options *LlamaServerOptions } + +type InstanceManager interface { +} + +type instanceManager struct { + instances map[string]*Instance +} + +func NewInstanceManager() InstanceManager { + return &instanceManager{ + instances: make(map[string]*Instance), + } +} diff --git a/server/pkg/routes.go b/server/pkg/routes.go index 6af11d7..1de76d5 100644 --- a/server/pkg/routes.go +++ b/server/pkg/routes.go @@ -8,19 +8,19 @@ import ( _ "llamactl/docs" ) -func SetupRouter() *chi.Mux { +func SetupRouter(handler *Handler) *chi.Mux { r := chi.NewRouter() r.Use(middleware.Logger) r.Get("/swagger/*", httpSwagger.Handler( - httpSwagger.URL("/swagger/doc.json"), // The URL pointing to API definition + httpSwagger.URL("/swagger/doc.json"), )) // Define routes r.Route("/api/v1", func(r chi.Router) { - r.Get("/server/help", HelpHandler) - r.Get("/server/version", VersionHandler) - r.Get("/server/devices", ListDevicesHandler) + r.Get("/server/help", handler.HelpHandler()) + r.Get("/server/version", handler.VersionHandler()) + r.Get("/server/devices", handler.ListDevicesHandler()) // Launch and stop handlers // r.Post("/server/launch/{model}", launchHandler)