mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-05 16:44:22 +00:00
Refactor server handlers and routing to use a handler struct
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user