fix: remove unconventional delete multiple instances endpoint

Ayman Bagabas created

Change summary

internal/client/proto.go  | 17 -----------------
internal/server/proto.go  | 32 +++++---------------------------
internal/server/server.go |  2 +-
3 files changed, 6 insertions(+), 45 deletions(-)

Detailed changes

internal/client/proto.go 🔗

@@ -547,23 +547,6 @@ func (c *Client) DeleteInstance(ctx context.Context, id string) error {
 	return nil
 }
 
-func (c *Client) DeleteInstances(ctx context.Context, ids []string) error {
-	r, err := http.NewRequestWithContext(ctx, "DELETE", "http://localhost/v1/instances", jsonBody(ids))
-	if err != nil {
-		return fmt.Errorf("failed to create request: %w", err)
-	}
-	r.Header.Set("Content-Type", "application/json")
-	rsp, err := c.h.Do(r)
-	if err != nil {
-		return fmt.Errorf("failed to delete instances: %w", err)
-	}
-	defer rsp.Body.Close()
-	if rsp.StatusCode != http.StatusOK {
-		return fmt.Errorf("failed to delete instances: status code %d", rsp.StatusCode)
-	}
-	return nil
-}
-
 func jsonBody(v any) *bytes.Buffer {
 	b := new(bytes.Buffer)
 	m, _ := json.Marshal(v)

internal/server/proto.go 🔗

@@ -504,34 +504,12 @@ func (c *controllerV1) handleGetInstanceConfig(w http.ResponseWriter, r *http.Re
 }
 
 func (c *controllerV1) handleDeleteInstances(w http.ResponseWriter, r *http.Request) {
-	var ids []string
-	id := r.URL.Query().Get("id")
-	if id != "" {
-		ids = append(ids, id)
-	}
-
-	// Get IDs from body
-	var args []proto.Instance
-	if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
-		c.logError(r, "failed to decode request", "error", err)
-		jsonError(w, http.StatusBadRequest, "failed to decode request")
-		return
-	}
-	ids = append(ids, func() []string {
-		out := make([]string, len(args))
-		for i, arg := range args {
-			out[i] = arg.ID
-		}
-		return out
-	}()...)
-
-	for _, id := range ids {
-		ins, ok := c.instances.Get(id)
-		if ok {
-			ins.App.Shutdown()
-		}
-		c.instances.Del(id)
+	id := r.PathValue("id")
+	ins, ok := c.instances.Get(id)
+	if ok {
+		ins.App.Shutdown()
 	}
+	c.instances.Del(id)
 }
 
 func (c *controllerV1) handlePostInstances(w http.ResponseWriter, r *http.Request) {

internal/server/server.go 🔗

@@ -138,7 +138,7 @@ func NewServer(cfg *config.Config, network, address string) *Server {
 	mux.HandleFunc("POST /v1/control", c.handlePostControl)
 	mux.HandleFunc("GET /v1/instances", c.handleGetInstances)
 	mux.HandleFunc("POST /v1/instances", c.handlePostInstances)
-	mux.HandleFunc("DELETE /v1/instances", c.handleDeleteInstances)
+	mux.HandleFunc("DELETE /v1/instances/{id}", c.handleDeleteInstances)
 	mux.HandleFunc("GET /v1/instances/{id}/config", c.handleGetInstanceConfig)
 	mux.HandleFunc("GET /v1/instances/{id}/events", c.handleGetInstanceEvents)
 	mux.HandleFunc("GET /v1/instances/{id}/sessions", c.handleGetInstanceSessions)