package main import ( "fmt" "log" "net/http" "net/http/httputil" "net/url" ) func main() { http.HandleFunc("/", webrootHandler) http.HandleFunc("/api", apiV1Main) //http.ListenAndServe("127.0.0.1:65500", nil) CreateDefaultMenu() http.ListenAndServe(":65500", nil) } //apiV1Main version 1 main entry for all wechat callbacks // func apiV1Main(w http.ResponseWriter, r *http.Request) { logRequestDebug(httputil.DumpRequest(r, true)) rq := r.URL.RawQuery m, _ := url.ParseQuery(rq) fmt.Fprintf(w, m["echostr"][0]) } //webrootHandler sending contents to client when request "/" // essentially to prove the webserver is still alive // echo query string to the client func webrootHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) rq := r.URL.RawQuery m, _ := url.ParseQuery(rq) for index, element := range m { fmt.Fprintf(w, "\r\n%s => %s", index, element) } logRequestDebug(httputil.DumpRequest(r, true)) } func logRequestDebug(data []byte, err error) { if err == nil { fmt.Printf("%s\n\n", data) } else { log.Fatalf("%s\n\n", err) } }