-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouteFunctions.go
111 lines (91 loc) · 2.63 KB
/
routeFunctions.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type QueryRequest struct {
QueryString string `json:"query_string"`
Base64Columns []string `json:"base64_columns"`
}
func (a *App) apiMakeDbRequest(w http.ResponseWriter, r *http.Request) {
executeOnly := r.URL.Query().Get("executeOnly")
executeOnly_state := 0
var err error
if len(executeOnly) > 0 {
executeOnly_state, err = strconv.Atoi(executeOnly)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
}
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
}
var queryrequest QueryRequest
json.Unmarshal(reqBody, &queryrequest)
fmt.Println(queryrequest)
var response []query_response
switch executeOnly_state {
case 0:
response, err = do_db_select_query(a.DB, queryrequest.QueryString, queryrequest.Base64Columns)
default:
err = do_db_query_exec(a.DB, queryrequest.QueryString)
}
fmt.Print(response, err)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
res := make(map[string]interface{})
res["message"] = "db query result"
res["data"] = response
if executeOnly_state == 0 {
res["total"] = len(response)
} else {
res["total"] = 1
res["status"] = 1
}
respondWithJSON(w, http.StatusOK, res)
}
func (a *App) getRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "get called"}`))
}
func (a *App) withParams(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
userID := -1
var err error
if val, ok := pathParams["userID"]; ok {
userID, err = strconv.Atoi(val)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"message": "need a number"}`))
return
}
}
commentID := -1
if val, ok := pathParams["commentID"]; ok {
commentID, err = strconv.Atoi(val)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"message": "need a number"}`))
return
}
}
query := r.URL.Query()
location := query.Get("location")
// example query
// http://127.0.0.1:8080/api/v1/user/23/comment/55?location=elsewhere
w.Write([]byte(fmt.Sprintf(`{"userID": %d, "commentID": %d, "location": "%s" }`, userID, commentID, location)))
}
func (a *App) notFound(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"message": "not found"}`))
}