mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 00:54:23 +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/
|
// @license.url https://opensource.org/license/mit/
|
||||||
// @basePath /api/v1
|
// @basePath /api/v1
|
||||||
func main() {
|
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
|
// Start the server with the router
|
||||||
fmt.Println("Starting llamactl on port 8080...")
|
fmt.Println("Starting llamactl on port 8080...")
|
||||||
http.ListenAndServe(":8080", r)
|
http.ListenAndServe(":8080", r)
|
||||||
|
|||||||
@@ -1,10 +1,22 @@
|
|||||||
package llamactl
|
package llamactl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
InstanceManager InstanceManager
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(im InstanceManager) *Handler {
|
||||||
|
return &Handler{
|
||||||
|
InstanceManager: im,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// HelpHandler godoc
|
// HelpHandler 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
|
||||||
@@ -13,15 +25,17 @@ import (
|
|||||||
// @Success 200 {string} string "Help text"
|
// @Success 200 {string} string "Help text"
|
||||||
// @Failure 500 {string} string "Internal Server Error"
|
// @Failure 500 {string} string "Internal Server Error"
|
||||||
// @Router /server/help [get]
|
// @Router /server/help [get]
|
||||||
func HelpHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) HelpHandler() http.HandlerFunc {
|
||||||
helpCmd := exec.Command("llama-server", "--help")
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
output, err := helpCmd.CombinedOutput()
|
helpCmd := exec.Command("llama-server", "--help")
|
||||||
if err != nil {
|
output, err := helpCmd.CombinedOutput()
|
||||||
http.Error(w, "Failed to get help: "+err.Error(), http.StatusInternalServerError)
|
if err != nil {
|
||||||
return
|
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
|
// VersionHandler godoc
|
||||||
@@ -32,15 +46,17 @@ func HelpHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Success 200 {string} string "Version information"
|
// @Success 200 {string} string "Version information"
|
||||||
// @Failure 500 {string} string "Internal Server Error"
|
// @Failure 500 {string} string "Internal Server Error"
|
||||||
// @Router /server/version [get]
|
// @Router /server/version [get]
|
||||||
func VersionHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) VersionHandler() http.HandlerFunc {
|
||||||
versionCmd := exec.Command("llama-server", "--version")
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
output, err := versionCmd.CombinedOutput()
|
versionCmd := exec.Command("llama-server", "--version")
|
||||||
if err != nil {
|
output, err := versionCmd.CombinedOutput()
|
||||||
http.Error(w, "Failed to get version: "+err.Error(), http.StatusInternalServerError)
|
if err != nil {
|
||||||
return
|
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
|
// ListDevicesHandler godoc
|
||||||
@@ -51,15 +67,29 @@ func VersionHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Success 200 {string} string "List of devices"
|
// @Success 200 {string} string "List of devices"
|
||||||
// @Failure 500 {string} string "Internal Server Error"
|
// @Failure 500 {string} string "Internal Server Error"
|
||||||
// @Router /server/devices [get]
|
// @Router /server/devices [get]
|
||||||
func ListDevicesHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) ListDevicesHandler() http.HandlerFunc {
|
||||||
listCmd := exec.Command("llama-server", "--list-devices")
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
output, err := listCmd.CombinedOutput()
|
listCmd := exec.Command("llama-server", "--list-devices")
|
||||||
if err != nil {
|
output, err := listCmd.CombinedOutput()
|
||||||
http.Error(w, "Failed to list devices: "+err.Error(), http.StatusInternalServerError)
|
if err != nil {
|
||||||
return
|
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) {
|
// func launchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -4,3 +4,16 @@ type Instance struct {
|
|||||||
Status string
|
Status string
|
||||||
Options *LlamaServerOptions
|
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"
|
_ "llamactl/docs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupRouter() *chi.Mux {
|
func SetupRouter(handler *Handler) *chi.Mux {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
|
|
||||||
r.Get("/swagger/*", httpSwagger.Handler(
|
r.Get("/swagger/*", httpSwagger.Handler(
|
||||||
httpSwagger.URL("/swagger/doc.json"), // The URL pointing to API definition
|
httpSwagger.URL("/swagger/doc.json"),
|
||||||
))
|
))
|
||||||
|
|
||||||
// Define routes
|
// Define routes
|
||||||
r.Route("/api/v1", func(r chi.Router) {
|
r.Route("/api/v1", func(r chi.Router) {
|
||||||
r.Get("/server/help", HelpHandler)
|
r.Get("/server/help", handler.HelpHandler())
|
||||||
r.Get("/server/version", VersionHandler)
|
r.Get("/server/version", handler.VersionHandler())
|
||||||
r.Get("/server/devices", ListDevicesHandler)
|
r.Get("/server/devices", handler.ListDevicesHandler())
|
||||||
|
|
||||||
// Launch and stop handlers
|
// Launch and stop handlers
|
||||||
// r.Post("/server/launch/{model}", launchHandler)
|
// r.Post("/server/launch/{model}", launchHandler)
|
||||||
|
|||||||
Reference in New Issue
Block a user