/
handlers.go
96 lines (79 loc) · 2.25 KB
/
handlers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// handlers.go
package main
import (
"context"
"encoding/json"
//"fmt"
"io"
"net/http"
"os/exec"
"time"
"go.mongodb.org/mongo-driver/bson"
)
type SmallcaseCurl struct {
SmallCaseId string `bson:"smallcase_id,omitempty" json:"smallcase_id,omitempty"`
EncryptedCurl string `bson:"encrypted_curl,omitempty"`
}
type SmallcaseAPIResponse struct {
Data struct {
Constituents []interface{} `json:"constituents"`
} `json:"data"`
}
func getConstituents(w http.ResponseWriter, r *http.Request) {
var curlDetails SmallcaseCurl
filter := bson.M{"smallcase_id": "CMMO_0001"}
err := collection.FindOne(context.Background(), filter).Decode(&curlDetails)
if err != nil {
http.Error(w, "Smallcase not found", http.StatusNotFound)
return
}
curl, err := DecryptText(curlDetails.EncryptedCurl)
if err != nil {
http.Error(w, "Error decrypting text "+err.Error(), http.StatusInternalServerError)
return
}
//fmt.Println(curl)
w.Header().Set("Content-Type", "application/json")
cmd := exec.Command("bash", "-c", curl)
output, err := cmd.Output()
if err != nil {
http.Error(w, "Error fetching constituents "+err.Error(), http.StatusInternalServerError)
return
}
var response SmallcaseAPIResponse
err = json.Unmarshal(output, &response)
if err != nil {
http.Error(w, "Error decoding response JSON "+err.Error(), http.StatusInternalServerError)
return
}
//fmt.Println(response)
constituents := response.Data.Constituents
json.NewEncoder(w).Encode(constituents)
}
func setConstituents(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body "+err.Error(), http.StatusInternalServerError)
return
}
encryptedText, err := EncryptText(string(body))
if err != nil {
http.Error(w, "Error encrypting text "+err.Error(), http.StatusInternalServerError)
return
}
// Update database
filter := bson.M{"smallcase_id": "CMMO_0001"}
update := bson.M{
"$set": bson.M{
"encrypted_curl": encryptedText,
"updated_at": time.Now(),
},
}
_, err = collection.UpdateOne(context.Background(), filter, update)
if err != nil {
http.Error(w, "Error updating database", http.StatusInternalServerError)
return
}
// shortcircuit to return API response
getConstituents(w, r)
}