-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-controller.go
50 lines (41 loc) · 1.29 KB
/
http-controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package controller
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
type SecretsService interface {
UpsertSecrets(ctx context.Context, env string, cfg map[string]interface{}) error
}
type SecretsEndpoints struct {
service SecretsService
}
func New(s SecretsService) *SecretsEndpoints {
return &SecretsEndpoints{s}
}
func (e SecretsEndpoints) UpsertSecretsHandler(w http.ResponseWriter, r *http.Request) {
query, ctx := r.URL.Query(), context.Background()
env := query.Get("env")
secrets := make(map[string]interface{})
err := json.NewDecoder(r.Body).Decode(&secrets)
if err != nil {
log.Printf("Error decoding request body: %v", err)
http.Error(w, "Error decoding request body", http.StatusBadRequest)
return
}
err = e.service.UpsertSecrets(ctx, env, secrets)
if err != nil {
log.Printf("Error upserting secrets for env %s with error: %v", env, err)
http.Error(w, fmt.Sprintf("Error upserting secrets for env %s", env), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
response := map[string]string{"message": "Secrets upserted successfully"}
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Printf("Error encoding response: %v", err)
http.Error(w, "Error encoding response", http.StatusInternalServerError)
}
}