forked from semaphoreui/semaphore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.go
228 lines (178 loc) · 4.8 KB
/
apps.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
package api
import (
"encoding/json"
"errors"
"fmt"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/util"
"github.com/gorilla/context"
"net/http"
"reflect"
"sort"
"strings"
)
func structToFlatMap(obj interface{}) map[string]interface{} {
result := make(map[string]interface{})
val := reflect.ValueOf(obj)
typ := reflect.TypeOf(obj)
if typ.Kind() == reflect.Ptr {
val = val.Elem()
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return result
}
// Iterate over the struct fields
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := typ.Field(i)
jsonTag := fieldType.Tag.Get("json")
// Use the json tag if it is set, otherwise use the field name
fieldName := jsonTag
if fieldName == "" || fieldName == "-" {
fieldName = fieldType.Name
} else {
// Handle the case where the json tag might have options like `json:"name,omitempty"`
fieldName = strings.Split(fieldName, ",")[0]
}
// Check if the field is a struct itself
if field.Kind() == reflect.Struct {
// Convert nested struct to map
nestedMap := structToFlatMap(field.Interface())
// Add nested map to result with a prefixed key
for k, v := range nestedMap {
result[fieldName+"."+k] = v
}
} else if (field.Kind() == reflect.Ptr ||
field.Kind() == reflect.Array ||
field.Kind() == reflect.Slice ||
field.Kind() == reflect.Map) && field.IsNil() {
result[fieldName] = nil
} else {
result[fieldName] = field.Interface()
}
}
return result
}
func validateAppID(str string) error {
return nil
}
func appMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
appID, err := helpers.GetStrParam("app_id", w, r)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
}
if err := validateAppID(appID); err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
context.Set(r, "app_id", appID)
next.ServeHTTP(w, r)
})
}
func getApps(w http.ResponseWriter, r *http.Request) {
type app struct {
util.App
ID string `json:"id"`
}
apps := make([]app, 0)
for k, a := range util.Config.Apps {
apps = append(apps, app{
App: a,
ID: k,
})
}
sort.Slice(apps, func(i, j int) bool {
return apps[i].Priority > apps[j].Priority
})
helpers.WriteJSON(w, http.StatusOK, apps)
}
func getApp(w http.ResponseWriter, r *http.Request) {
appID := context.Get(r, "app_id").(string)
app, ok := util.Config.Apps[appID]
if !ok {
helpers.WriteErrorStatus(w, "app not found", http.StatusNotFound)
return
}
helpers.WriteJSON(w, http.StatusOK, app)
}
func deleteApp(w http.ResponseWriter, r *http.Request) {
appID := context.Get(r, "app_id").(string)
store := helpers.Store(r)
err := store.DeleteOptions("apps." + appID)
if err != nil && !errors.Is(err, db.ErrNotFound) {
helpers.WriteErrorStatus(w, err.Error(), http.StatusInternalServerError)
return
}
delete(util.Config.Apps, appID)
w.WriteHeader(http.StatusNoContent)
}
func setAppOption(store db.Store, appID string, field string, val interface{}) error {
key := "apps." + appID + "." + field
if val == nil {
return store.DeleteOptions(key)
}
v := fmt.Sprintf("%v", val)
if err := store.SetOption(key, v); err != nil {
return err
}
opts := make(map[string]string)
opts[key] = v
options := db.ConvertFlatToNested(opts)
_ = db.AssignMapToStruct(options, util.Config)
return nil
}
func setApp(w http.ResponseWriter, r *http.Request) {
appID := context.Get(r, "app_id").(string)
store := helpers.Store(r)
var app util.App
if !helpers.Bind(w, r, &app) {
return
}
options := structToFlatMap(app)
for k, v := range options {
t := reflect.TypeOf(v)
if v != nil {
switch t.Kind() {
case reflect.String:
if v == "" {
v = nil
}
case reflect.Slice, reflect.Array:
newV, err := json.Marshal(v)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusInternalServerError)
return
}
v = string(newV)
if v == "[]" {
v = nil
}
default:
}
}
if err := setAppOption(store, appID, k, v); err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusNoContent)
}
func setAppActive(w http.ResponseWriter, r *http.Request) {
appID := context.Get(r, "app_id").(string)
store := helpers.Store(r)
var body struct {
Active bool `json:"active"`
}
if !helpers.Bind(w, r, &body) {
helpers.WriteErrorStatus(w, "Invalid request body", http.StatusBadRequest)
return
}
if err := setAppOption(store, appID, "active", body.Active); err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}