Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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