-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservicemgmt.go
291 lines (258 loc) · 9.09 KB
/
servicemgmt.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2015. Zuercher Hochschule fuer Angewandte Wissenschaften
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Author: Piyush Harsh,
* URL: piyush-harsh.info
*/
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"net/http"
"strconv"
"strings"
)
func ServiceRegisterHandler(out http.ResponseWriter, in *http.Request) {
out.Header().Set("Content-Type", "application/json")
decoder := json.NewDecoder(in.Body)
var s service_struct
err := decoder.Decode(&s)
if len(in.Header["X-Auth-Token"]) == 0 {
MyFileWarning.Println("User List Module - Can't Proceed: Token Missing!")
out.WriteHeader(http.StatusBadRequest) //400 status code
var jsonbody = staticMsgs[5]
fmt.Fprintln(out, jsonbody)
} else {
token := in.Header["X-Auth-Token"][0]
//check if token is valid and belongs to an admin user
isAdmin := CheckTokenAdmin(token)
if isAdmin {
if err != nil {
out.WriteHeader(http.StatusBadRequest) //status 400 Bad Request
var jsonbody = staticMsgs[1]
fmt.Fprintln(out, jsonbody)
MyFileInfo.Println("Received malformed request on URI:/admin/service/ POST")
panic(err)
} else if len(s.Shortname) == 0 {
MyFileInfo.Println("Received malformed request on URI:/admin/service/ POST")
out.WriteHeader(http.StatusBadRequest)
var jsonbody = staticMsgs[1] //status 400 Bad Request
fmt.Fprintln(out, jsonbody)
} else {
MyFileInfo.Println("Received JSON: Struct value received for service [shortname, description]:", s.Shortname, ",", s.Description)
serviceCount := GetCount(dbArg, "service", "shortname", s.Shortname)
if serviceCount > 0 {
MyFileInfo.Println("Duplicate service create request on URI:/admin/service/ POST")
out.WriteHeader(http.StatusPreconditionFailed)
var jsonbody = staticMsgs[12] //service already exists
fmt.Fprintln(out, jsonbody)
} else {
//now store the new service in the table and return back the proper response
MyFileInfo.Println("Attempting to store new service:", s.Shortname, "into the table.")
uuid := genuuid()
uuid = strings.TrimSpace(uuid)
MyFileInfo.Println("Generated a new uuid for the service:", uuid)
MyFileInfo.Println("Attempting to store new service:", s.Shortname, "into the table.")
status := InsertService(dbArg, "service", uuid, s.Shortname, s.Description)
MyFileInfo.Println("Status of the attempt to store new service:", s.Shortname, "into the table was:", status)
out.WriteHeader(http.StatusOK) //200 status code
var jsonbody = staticMsgs[13] //service registration msg, replace with actual content for xxx and yyy
sId := LocateService(dbArg, "service", uuid)
MyFileInfo.Println("The new id for service:", s.Shortname, "is:", sId)
//constructing the correct JSON response
jsonbody = strings.Replace(jsonbody, "xxx", strconv.Itoa(sId), 1)
jsonbody = strings.Replace(jsonbody, "yyy", uuid, 1)
fmt.Fprintln(out, jsonbody)
}
}
} else {
var jsonbody = staticMsgs[18]
out.WriteHeader(http.StatusUnauthorized) //401 status code
fmt.Fprintln(out, jsonbody)
}
}
MyFileInfo.Println("Received request on URI:/admin/service/ POST")
}
func ServiceListHandler(out http.ResponseWriter, in *http.Request) {
out.Header().Set("Content-Type", "application/json")
if len(in.Header["X-Auth-Token"]) == 0 {
MyFileWarning.Println("User List Module - Can't Proceed: Token Missing!")
out.WriteHeader(http.StatusBadRequest) //400 status code
var jsonbody = staticMsgs[5]
fmt.Fprintln(out, jsonbody)
} else {
token := in.Header["X-Auth-Token"][0]
//check if token is valid and belongs to an admin user
isAdmin := CheckTokenAdmin(token)
if isAdmin {
uuidlist, snamelist, sidlist := GetServiceList(dbArg, "service")
var jsonbody = staticMsgs[11]
var buffer1 bytes.Buffer
var buffer2 bytes.Buffer
var buffer3 bytes.Buffer
for i := 0; i < len(uuidlist); i++ {
if i == 0 {
buffer1.WriteString("\"")
buffer1.WriteString(uuidlist[i])
buffer1.WriteString("\"")
buffer2.WriteString("\"")
buffer2.WriteString(snamelist[i])
buffer2.WriteString("\"")
buffer3.WriteString("\"")
buffer3.WriteString(sidlist[i])
buffer3.WriteString("\"")
} else {
buffer1.WriteString(",\"")
buffer1.WriteString(uuidlist[i])
buffer1.WriteString("\"")
buffer2.WriteString(",\"")
buffer2.WriteString(snamelist[i])
buffer2.WriteString("\"")
buffer3.WriteString(",\"")
buffer3.WriteString(sidlist[i])
buffer3.WriteString("\"")
}
}
jsonbody = strings.Replace(jsonbody, "uuid-xxx", buffer1.String(), 1)
jsonbody = strings.Replace(jsonbody, "name-yyy", buffer2.String(), 1)
jsonbody = strings.Replace(jsonbody, "id-zzz", buffer3.String(), 1)
out.WriteHeader(http.StatusOK) //200 status code
fmt.Fprintln(out, jsonbody)
} else {
var jsonbody = staticMsgs[18]
out.WriteHeader(http.StatusUnauthorized) //401 status code
fmt.Fprintln(out, jsonbody)
}
}
MyFileInfo.Println("Received request on URI:/admin/service/ GET")
}
func GetServiceList(filePath string, tableName string) ([]string, []string, []string) {
db, err := sql.Open("sqlite3", filePath)
if err != nil {
checkErr(err, 1, db)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
queryStmt := "SELECT sid, key, shortname FROM tablename;"
queryStmt = strings.Replace(queryStmt, "tablename", tableName, 1)
MyFileInfo.Println("SQLite3 Query:", queryStmt)
rows, err := db.Query(queryStmt)
if err != nil {
MyFileWarning.Println("Caught error in service-list method.")
checkErr(err, 1, db)
}
defer rows.Close()
var sidlist []string
var uuidlist []string
var namelist []string
for rows.Next() {
var suuid string
var sname string
var sid string
err = rows.Scan(&sid, &suuid, &sname)
checkErr(err, 1, db)
uuidlist = append(uuidlist, suuid)
namelist = append(namelist, sname)
sidlist = append(sidlist, sid)
}
return uuidlist, namelist, sidlist
}
func InsertService(filePath string, tableName string, serviceUuid string, serviceName string, serviceDesc string) bool {
db, err := sql.Open("sqlite3", filePath)
if err != nil {
checkErr(err, 1, db)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
insertStmt := "INSERT INTO tablename VALUES (NULL, 'skey', 'sname', 'sdesc');"
insertStmt = strings.Replace(insertStmt, "tablename", tableName, 1)
insertStmt = strings.Replace(insertStmt, "skey", serviceUuid, 1)
insertStmt = strings.Replace(insertStmt, "sname", serviceName, 1)
insertStmt = strings.Replace(insertStmt, "sdesc", serviceDesc, 1)
MyFileInfo.Println("SQLite3 Query:", insertStmt)
res, err := db.Exec(insertStmt)
if err != nil {
MyFileWarning.Println("Caught error in insert-service method,", res)
checkErr(err, 1, db)
}
return true
}
func LocateService(filePath string, tableName string, uuid string) int {
db, err := sql.Open("sqlite3", filePath)
if err != nil {
checkErr(err, 1, db)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
queryStmt := "SELECT sid FROM tablename WHERE key='searchterm';"
queryStmt = strings.Replace(queryStmt, "tablename", tableName, 1)
queryStmt = strings.Replace(queryStmt, "searchterm", uuid, 1)
MyFileInfo.Println("SQLite3 Query:", queryStmt)
rows, err := db.Query(queryStmt)
if err != nil {
MyFileWarning.Println("Caught error in service-locate method.")
checkErr(err, 1, db)
}
defer rows.Close()
if rows.Next() {
var serviceId int
err = rows.Scan(&serviceId)
checkErr(err, 1, db)
return serviceId
}
return -1
}
func LocateServiceCode(filePath string, tableName string, uuId string) string {
db, err := sql.Open("sqlite3", filePath)
if err != nil {
checkErr(err, 1, db)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
queryStmt := "SELECT shortname FROM tablename WHERE key='searchterm';"
queryStmt = strings.Replace(queryStmt, "tablename", tableName, 1)
queryStmt = strings.Replace(queryStmt, "searchterm", uuId, 1)
MyFileInfo.Println("SQLite3 Query:", queryStmt)
rows, err := db.Query(queryStmt)
if err != nil {
MyFileWarning.Println("Caught error in locate-service-code method.")
checkErr(err, 1, db)
}
defer rows.Close()
if rows.Next() {
var shortcode string
err = rows.Scan(&shortcode)
checkErr(err, 1, db)
return shortcode
}
return ""
}