-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (39 loc) · 1.24 KB
/
main.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
// main.go
package main
import (
"net/http"
"encoding/json"
"io/ioutil"
)
type generalRet struct {
Status string `json:"status"`
Value interface{} `json:"value"`
}
func main() {
http.HandleFunc("/shippingRate/", folderHandler)
http.HandleFunc("/getShippingRate", getShippingRate)
http.ListenAndServe(":2014", nil)
}
func folderHandler(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, "."+request.URL.Path)
}
func readPostData(w http.ResponseWriter, req *http.Request,location string)(postData []byte,err error){
if postData, err = ioutil.ReadAll(req.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
logError("reaPostData " + location + " err :",err)
}
return
}
func writeGeneralResponse(location string, value interface{},origData []byte, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
gr := generalRet{Status:"ok",Value:value}
if ret,err := json.Marshal(gr);err!=nil{
logError("write resp marshal",err)
w.WriteHeader(http.StatusInternalServerError)
} else if ret,err = runHook(location,"post",origData,ret);err!=nil {
logError("run post hook for " + location, err)
w.WriteHeader(http.StatusInternalServerError)
}else{
w.Write(ret)
}
}