From 84d10b5ccc997328a7a88a9cb3ec5ccc118e9a69 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 16 Jul 2025 22:09:11 +0200 Subject: [PATCH] Initial server setup --- server/cmd/llamactl.go | 67 ++++++++++++++++++++++++++++++++++++++++++ server/go.mod | 5 ++++ server/go.sum | 2 ++ 3 files changed, 74 insertions(+) create mode 100644 server/cmd/llamactl.go create mode 100644 server/go.mod create mode 100644 server/go.sum diff --git a/server/cmd/llamactl.go b/server/cmd/llamactl.go new file mode 100644 index 0000000..9ef1e08 --- /dev/null +++ b/server/cmd/llamactl.go @@ -0,0 +1,67 @@ +package main + +import ( + "net/http" + "os" + "os/exec" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" +) + +var instances map[string]*exec.Cmd = make(map[string]*exec.Cmd) + +func execLLama(model string) *exec.Cmd { + llamaCmd := exec.Command("llama", "server", "--model", model, "--port", "8080") + return llamaCmd +} + +func launchHandler(w http.ResponseWriter, r *http.Request) { + model := chi.URLParam(r, "model") + if model == "" { + http.Error(w, "Model parameter is required", http.StatusBadRequest) + return + } + + cmd := execLLama(model) + if err := cmd.Start(); err != nil { + http.Error(w, "Failed to start llama server: "+err.Error(), http.StatusInternalServerError) + return + } + + instances[model] = cmd + w.Write([]byte("Llama server started for model: " + model)) +} + +func stopHandler(w http.ResponseWriter, r *http.Request) { + model := chi.URLParam(r, "model") + if model == "" { + http.Error(w, "Model parameter is required", http.StatusBadRequest) + return + } + + cmd, exists := instances[model] + if !exists { + http.Error(w, "No running instance for model: "+model, http.StatusNotFound) + return + } + + if err := cmd.Process.Signal(os.Interrupt); err != nil { + http.Error(w, "Failed to stop llama server: "+err.Error(), http.StatusInternalServerError) + return + } + + delete(instances, model) + w.Write([]byte("Llama server stopped for model: " + model)) +} + +func main() { + r := chi.NewRouter() + r.Use(middleware.Logger) + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("welcome")) + }) + r.Post("/launch/{model}", launchHandler) + r.Post("/stop/{model}", stopHandler) + http.ListenAndServe(":3000", r) +} diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..430b7b8 --- /dev/null +++ b/server/go.mod @@ -0,0 +1,5 @@ +module github.com/lordmathis/llamactl + +go 1.24.5 + +require github.com/go-chi/chi/v5 v5.2.2 diff --git a/server/go.sum b/server/go.sum new file mode 100644 index 0000000..ed9ec89 --- /dev/null +++ b/server/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618= +github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=