/
http_utils.go
40 lines (35 loc) · 921 Bytes
/
http_utils.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
package utils
import (
"encoding/json"
"log"
"net/http"
"strconv"
)
func ServeJSON(w http.ResponseWriter, j interface{}) {
if j == nil {
http.Error(w, "empty response data", 400)
return
}
// w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(j)
// for request: json.NewDecoder(res.Body).Decode(&body)
}
func Abort(w http.ResponseWriter, message string, code int) {
http.Error(w, message, code)
}
func GetQueryInt(r *http.Request, key string, defaultValue int) int {
value, err := strconv.Atoi(r.URL.Query().Get(key))
if err != nil {
log.Println("Error: get params cols error:", err)
return defaultValue
}
return value
}
func GetQueryInt32(r *http.Request, key string, defaultValue uint32) uint32 {
value, err := strconv.Atoi(r.URL.Query().Get(key))
if err != nil {
log.Println("Error: get params cols error:", err)
return defaultValue
}
return uint32(value)
}