update config working #1

Merged
jjsfunhouse merged 1 commits from jjsfunhouse/coop-cloud-backend:main into main 2026-05-08 16:05:47 +00:00
5 changed files with 117 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

16
cli/remove.go Normal file
View File

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