/
onion_resource.go
228 lines (190 loc) · 5.35 KB
/
onion_resource.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// generated by ginger from go generate -- DO NOT EDIT
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/mingderwang/pepper/jsonToGo"
"log"
"bytes"
"net/http"
"os"
"os/exec"
"strconv"
"time"
)
type OnionResource struct {
db gorm.DB
}
// @Title CreateOnion
// @Description get string by ID
// @Accept json
// @Param some_id path int true "Some ID"
// @Success 201 {object} string
// @Failure 400 {object} APIError "problem decoding body"
// @Router /onion/ [post]
func (tr *OnionResource) CreateOnion(c *gin.Context) {
var onion Onion
if c.Bind(&onion) != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding body"})
return
}
//onion.Status = OnionStatus
onion.Ginger_Created = int32(time.Now().Unix())
tr.db.Save(&onion)
c.JSON(http.StatusCreated, onion)
}
func (tr *OnionResource) GetAllOnions(c *gin.Context) {
var onions []Onion
tr.db.Order("ginger__created desc").Find(&onions)
c.JSON(http.StatusOK, onions)
}
func (tr *OnionResource) GetOnion(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding id sent"})
return
}
var onion Onion
if tr.db.First(&onion, id).RecordNotFound() {
c.JSON(http.StatusNotFound, gin.H{"message": "not found"})
} else {
c.JSON(http.StatusOK, onion)
}
}
func (tr *OnionResource) BuildOnion(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding id sent"})
return
}
var onion Onion
if tr.db.First(&onion, id).RecordNotFound() {
c.JSON(http.StatusNotFound, gin.H{"message": "not found"})
} else {
genJsonToGo(onion)
dockerize(onion)
c.JSON(http.StatusOK, onion)
}
}
func genJsonToGo(obj Onion) {
output, _ := jsonToGo.Gen(obj.JsonSchema, obj.TypeName)
writeFile("./workspace/"+obj.DomainName+"."+obj.TypeName+"/onion", "onion.go", output)
}
func dockerize(obj Onion) {
command1 := "pwd"
out, err := exec.Command(command1).Output()
if err != nil {
log.Fatal(err)
}
//fmt.Printf("1. pwd output is %s\n", out)
i := len(out) - 1
outstring := string(out[:i])
path := outstring + "/workspace/" + obj.DomainName + "." + obj.TypeName + "/onion"
command2 := "./rolling_update.sh"
cmd2 := exec.Command(command2, path)
var out2 bytes.Buffer
var stderr2 bytes.Buffer
cmd2.Stdout = &out2
cmd2.Stderr = &stderr2
err2 := cmd2.Run()
if err2 != nil {
fmt.Println(fmt.Sprint(err2) + ": " + stderr2.String())
return
}
fmt.Printf("2. run docker.sh output is %s\n", out2)
}
func writeFile(path string, fileName string, stream string) {
//check path
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, os.ModePerm)
}
f, err := os.OpenFile(path+"/"+fileName, os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
log.Print(err)
n3, err := f.WriteString(stream)
fmt.Printf("wrote %d bytes\n", n3)
f.Sync()
}
func (tr *OnionResource) UpdateOnion(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding id sent"})
return
}
var onion Onion
if c.Bind(&onion) != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding body"})
return
}
onion.Ginger_Id = int32(id)
var existing Onion
if tr.db.First(&existing, id).RecordNotFound() {
c.JSON(http.StatusNotFound, gin.H{"message": "not found"})
} else {
tr.db.Save(&onion)
c.JSON(http.StatusOK, onion)
}
}
func (tr *OnionResource) PatchOnion(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding id sent"})
return
}
// this is a hack because Gin falsely claims my unmarshalled obj is invalid.
// recovering from the panic and using my object that already has the json body bound to it.
var json []Patch
r := c.Bind(&json)
if r != nil {
fmt.Println(r)
} else {
if json[0].Op != "replace" && json[0].Path != "/status" {
c.JSON(http.StatusBadRequest, gin.H{"message": "PATCH support is limited and can only replace the /status path"})
return
}
var onion Onion
if tr.db.First(&onion, id).RecordNotFound() {
c.JSON(http.StatusNotFound, gin.H{"message": "not found"})
} else {
//onion.Status = json[0].Value
tr.db.Save(&onion)
c.JSON(http.StatusOK, onion)
}
}
}
func (tr *OnionResource) DeleteOnion(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "problem decoding id sent"})
return
}
var onion Onion
if tr.db.First(&onion, id).RecordNotFound() {
c.JSON(http.StatusNotFound, gin.H{"message": "not found"})
} else {
tr.db.Delete(&onion)
c.Data(http.StatusNoContent, "application/json", make([]byte, 0))
}
}
func (tr *OnionResource) getId(c *gin.Context) (int32, error) {
idStr := c.Params.ByName("id")
id, err := strconv.Atoi(idStr)
if err != nil {
log.Print(err)
return 0, err
}
return int32(id), nil
}
/**
* on patching: http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/
*
* patch specification https://tools.ietf.org/html/rfc5789
* json definition http://tools.ietf.org/html/rfc6902
*/
type Patch struct {
Op string `json:"op" binding:"required"`
From string `json:"from"`
Path string `json:"path"`
Value string `json:"value"`
}