This repository has been archived by the owner on Oct 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.go
58 lines (50 loc) · 1.48 KB
/
lib.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type JsonResponse map[string]interface{}
func NotFound(w http.ResponseWriter, r *http.Request) {
var responceText = "Not Found - Invalid Parameters"
responceText += "The format is dns/{function}/{p1}/{p2}/{etc}</b>"
responceText += "example:</b> api-1.aencoin..io:3001/version"
responceText += "will return:</b> <br /> ver:1.(002) "
fmt.Fprint(w, JsonResponse{"resp": 400, "status": "unknow_parameters","message":responceText } )
}
func (jr JsonResponse) String() (str string) { // method to print out String
byte, err := json.Marshal(jr)
if err != nil {
str = "" // return empty
return
}
str = string(byte) //ok,return cast byte to string
return
}
func validateJsonBody(body []byte) string { // function for validating the Json body
// validate the key-value of post body json format
c := make(map[string]interface{})
json.Unmarshal(body, &c)
k := make([]string, len(c))
// copy c's keys into k
j := 0
for s, _ := range c {
k[j] = s
j++
}
var valid_flag int
var reqst = []string{"version"}
for _, reqst_val := range reqst {
for _, k_val := range k {
if reqst_val == k_val {
valid_flag++
break
}
}
}
if valid_flag == 1 {
return "ok"
} else {
return "not_ok"
}
}