You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "net/http/httputil"
  7. "net/url"
  8. )
  9. func main() {
  10. http.HandleFunc("/", webrootHandler)
  11. http.HandleFunc("/api", apiV1Main)
  12. //http.ListenAndServe("127.0.0.1:65500", nil)
  13. http.ListenAndServe(":65500", nil)
  14. }
  15. //apiV1Main version 1 main entry for all wechat callbacks
  16. //
  17. func apiV1Main(w http.ResponseWriter, r *http.Request) {
  18. logRequestDebug(httputil.DumpRequest(r, true))
  19. rq := r.URL.RawQuery
  20. m, _ := url.ParseQuery(rq)
  21. fmt.Fprintf(w, m["echostr"][0])
  22. }
  23. //webrootHandler sending contents to client when request "/"
  24. // essentially to prove the webserver is still alive
  25. // echo query string to the client
  26. func webrootHandler(w http.ResponseWriter, r *http.Request) {
  27. fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
  28. rq := r.URL.RawQuery
  29. m, _ := url.ParseQuery(rq)
  30. for index, element := range m {
  31. fmt.Fprintf(w, "\r\n%s => %s", index, element)
  32. }
  33. logRequestDebug(httputil.DumpRequest(r, true))
  34. }
  35. func logRequestDebug(data []byte, err error) {
  36. if err == nil {
  37. fmt.Printf("%s\n\n", data)
  38. } else {
  39. log.Fatalf("%s\n\n", err)
  40. }
  41. }