From fbf5b90cf86b476a6056a4e64d408633b69e6e5a Mon Sep 17 00:00:00 2001 From: hey Date: Fri, 8 May 2026 11:50:46 -0400 Subject: [PATCH] update config working --- cli/api.go | 8 ++++++ cli/config.go | 33 ++++++++++++++++++++---- cli/logs.go | 1 - cli/new.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++--- cli/remove.go | 16 ++++++++++++ 5 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 cli/remove.go diff --git a/cli/api.go b/cli/api.go index 89ff78d..3b0b0cb 100644 --- a/cli/api.go +++ b/cli/api.go @@ -81,6 +81,14 @@ func newAbraHandler() *abraHandler { http.Error(w, "Method not implemented", http.StatusMethodNotAllowed) } }) + h.mux.HandleFunc("/api/apps/{appId}/remove", func(w http.ResponseWriter, r *http.Request) { + switch r.Method{ + case http.MethodPost: + h.handleRemoveApp(w, r, r.PathValue("appId")) + default: + http.Error(w, "Method not implemented", http.StatusMethodNotAllowed) + } + }) h.mux.HandleFunc("/api/servers", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: diff --git a/cli/config.go b/cli/config.go index 173336d..1ae35cd 100644 --- a/cli/config.go +++ b/cli/config.go @@ -1,11 +1,16 @@ package cli import ( "log" - "io" + "io/ioutil" "os" "net/http" + "encoding/json" appPkg "coopcloud.tech/abra/pkg/app" ) +type FileResponse struct { + Content string `json:"content"` +} + func (h *abraHandler) handleGetConfig(w http.ResponseWriter, r *http.Request, appName string) { files, err := appPkg.LoadAppFiles("") if err != nil { @@ -20,7 +25,22 @@ func (h *abraHandler) handleGetConfig(w http.ResponseWriter, r *http.Request, ap } log.Printf("path: %s | server: %s", appFile.Path, appFile.Server) log.Printf("Ending...") + // TODO: sanitize + file, err := ioutil.ReadFile(appFile.Path) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + resp := FileResponse{Content: string(file)} + jsonBytes, err := json.Marshal(resp) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) + w.Write(jsonBytes) } func (h *abraHandler) handlePostConfig(w http.ResponseWriter, r *http.Request, appName string) { @@ -35,15 +55,18 @@ func (h *abraHandler) handlePostConfig(w http.ResponseWriter, r *http.Request, a http.Error(w, err.Error(), http.StatusBadRequest) return } - body, err := io.ReadAll(r.Body) + d := json.NewDecoder(r.Body) + d.DisallowUnknownFields() // catch unwanted fields + + var config FileResponse + err = d.Decode(&config) if err != nil { + log.Printf("err: %s", err); http.Error(w, "Failed to read request", http.StatusBadRequest) return } - defer r.Body.Close() - - err = os.WriteFile(appFile.Path, body, 0644) + err = os.WriteFile(appFile.Path, []byte(config.Content), 0644) if err != nil { http.Error(w, "Failed to write file", http.StatusInternalServerError) return diff --git a/cli/logs.go b/cli/logs.go index 8fb245a..89a05a0 100644 --- a/cli/logs.go +++ b/cli/logs.go @@ -80,7 +80,6 @@ func (h *abraHandler) handleGetLogs(w http.ResponseWriter, r *http.Request, appN return } fmt.Fprintf(w, "data: %s\n\n", line) - log.Printf(line) flusher.Flush() } } diff --git a/cli/new.go b/cli/new.go index 8a34457..7042be6 100644 --- a/cli/new.go +++ b/cli/new.go @@ -5,7 +5,13 @@ import ( "net/http" "encoding/json" "fmt" + + appPkg "coopcloud.tech/abra/pkg/app" + "coopcloud.tech/abra/pkg/secret" + "coopcloud.tech/abra/pkg/recipe" + "coopcloud.tech/abra/pkg/client" ) + func (h *abraHandler) handleNewApp(w http.ResponseWriter, r *http.Request, appName string) { args := []string{"app", "new", appName, "-n"} d := json.NewDecoder(r.Body) @@ -21,7 +27,6 @@ func (h *abraHandler) handleNewApp(w http.ResponseWriter, r *http.Request, appNa err := d.Decode(&body) if err != nil { - log.Printf("???\n") // bad JSON or unrecognized json field http.Error(w, err.Error(), http.StatusBadRequest) return @@ -39,9 +44,6 @@ func (h *abraHandler) handleNewApp(w http.ResponseWriter, r *http.Request, appNa if body.Chaos != nil && *body.Chaos == true { args = append(args, "-C") } - if body.Secrets != nil && *body.Secrets == true { - args = append(args, "--secrets") - } log.Printf("%v", args) cmd := exec.Command("abra", args...) output, err := cmd.Output() @@ -50,5 +52,64 @@ func (h *abraHandler) handleNewApp(w http.ResponseWriter, r *http.Request, appNa InternalServerErrorHandler(w, r) return } + var fs []AppSecret + if body.Secrets != nil && *body.Secrets == true { + appSecrets, err := createSecrets(appName, *body.Domain, *body.Server) + if err != nil { + log.Printf("Error creating secrets: %s", err) + InternalServerErrorHandler(w, r) + return + } + for k, v := range appSecrets { + fs = append(fs, AppSecret{ + Name: k, + Value: v, + Version: "v1", + }) + } + } + jsonBytes, err := json.Marshal(fs) + if err != nil { + log.Printf("JSON conversion failed: %s\n", err) + InternalServerErrorHandler(w, r) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) + w.Write(jsonBytes) } + +func createSecrets(appName string, domain string, server string) (map[string]string, error) { + cl, err := client.New(server) + if err != nil { + return nil, err + } + recipe := recipe.Get(appName) + + + sampleEnv, err := recipe.SampleEnv() + if err != nil { + return nil, err + } + + composeFiles, err := recipe.GetComposeFiles(sampleEnv) + if err != nil { + return nil, err + } + + //sanitisedAppName := appPkg.SanitiseAppName(domain) + secretsConfig, err := secret.ReadSecretsConfig( + recipe.SampleEnvPath, + composeFiles, + appPkg.StackName(domain), + ) + if err != nil { + return nil, err + } + + secrets, err := secret.GenerateSecrets(cl, secretsConfig, server) + if err != nil { + return nil, err + } + return secrets, nil +} \ No newline at end of file diff --git a/cli/remove.go b/cli/remove.go new file mode 100644 index 0000000..5146e55 --- /dev/null +++ b/cli/remove.go @@ -0,0 +1,16 @@ +package cli +import ( + "log" + "os/exec" + "net/http" +) +func (h *abraHandler) handleRemoveApp(w http.ResponseWriter, r *http.Request, appName string) { + cmd := exec.Command("abra", "app", "remove", appName, "-n") + output, err := cmd.Output() + if err != nil { + log.Printf("Error: ", string(output)) + InternalServerErrorHandler(w, r) + return + } + w.WriteHeader(http.StatusOK) +} -- 2.49.0