Refactor server handlers and routing to use a handler struct

This commit is contained in:
2025-07-17 21:46:26 +02:00
parent 1e962438bf
commit 6f571dd91c
4 changed files with 82 additions and 30 deletions

View File

@@ -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)

View File

@@ -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,7 +25,8 @@ 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) {
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 {
@@ -22,6 +35,7 @@ func HelpHandler(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "text/plain")
w.Write(output)
}
}
// VersionHandler godoc
@@ -32,7 +46,8 @@ 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) {
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 {
@@ -41,6 +56,7 @@ func VersionHandler(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "text/plain")
w.Write(output)
}
}
// ListDevicesHandler godoc
@@ -51,7 +67,8 @@ 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) {
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 {
@@ -60,6 +77,19 @@ func ListDevicesHandler(w http.ResponseWriter, r *http.Request) {
}
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
}
}
}
// func launchHandler(w http.ResponseWriter, r *http.Request) {

View File

@@ -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),
}
}

View File

@@ -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)