選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

125 行
4.5KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "net/http/httputil"
  7. "time"
  8. )
  9. //PathsConfig all system available pathes
  10. type PathsConfig struct {
  11. CRMAttachment string `json:"crm_attachmeng"`
  12. Angular2App string `json:"angular2_app"`
  13. }
  14. //GlobalPath all global pathes configurations
  15. var GlobalPath = PathsConfig{
  16. "/tmp/",
  17. "/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist/"}
  18. func main() {
  19. err := readConfig() //wechat API config
  20. if err != nil {
  21. log.Println(err)
  22. log.Fatal("unable to read server_config.json, program quit")
  23. return
  24. }
  25. err = readCRMConfig()
  26. if err != nil {
  27. log.Println(err)
  28. log.Fatal("unable to read crm_config.json, program quit")
  29. }
  30. setupRootFileServer()
  31. startSessionManager(2048)
  32. setupHTTPHandler()
  33. }
  34. func setupHTTPHandler() {
  35. //setup handler
  36. //http.HandleFunc("/", webrootHandler)
  37. http.HandleFunc("/api", apiV1Main)
  38. http.HandleFunc("/upload", uploadHandler)
  39. http.HandleFunc("/crmfiles/", crmAttachmentHandler)
  40. http.HandleFunc("/dumprequest", dumpReuestHandler)
  41. http.HandleFunc("/MP_verify_6JqVkftKr39GMakA.txt", mpDomainAuthSecret)
  42. http.HandleFunc("/profile_newly_register", initialRegistrationHandler)
  43. http.ListenAndServe(":65500", nil)
  44. }
  45. func startSessionManager(concurrent int) {
  46. m := SessionManager{}
  47. AllInMessage = make(chan InWechatMsg, concurrent)
  48. go m.startSessionManager(AllInMessage)
  49. }
  50. func setupRootFileServer() {
  51. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  52. http.ServeFile(w, r, GlobalPath.Angular2App+r.URL.Path[1:])
  53. })
  54. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/ng2-admin/dist"))
  55. //http.Handle("/test", http.StripPrefix("/test", fs))
  56. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist"))
  57. //http.Handle("/", fs)
  58. }
  59. func dumpReuestHandler(w http.ResponseWriter, r *http.Request) {
  60. logRequestDebug(httputil.DumpRequest(r, true))
  61. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  62. w.Header().Set("Access-Control-Allow-Origin", "http://192.168.1.39:4200")
  63. w.Header().Set("Access-Control-Allow-Headers", "Authorziation11,Authorziation12")
  64. w.Header().Set("Access-Control-Allow-Credentials", "true")
  65. w.Header().Set("Access-Control-Expose-Headers", "Set-Cookie,myheader,*")
  66. w.Header().Set("myheader", "myheader-data")
  67. expiration := time.Now().Add(10 * 365 * 24 * time.Hour)
  68. str := time.Now().String()
  69. cookie := http.Cookie{Name: "username", Value: str, Expires: expiration}
  70. http.SetCookie(w, &cookie)
  71. fmt.Fprintf(w, `{"status":"OK"}`)
  72. for _, c := range r.Cookies() {
  73. log.Println(c.Name)
  74. log.Println(c.Value)
  75. }
  76. }
  77. // 用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠。
  78. // 注意事项:
  79. // 1、回调页面域名或路径需使用字母、数字及“-”的组合(例:wx.qq.com或wx.qq.com/mp),不支持IP地址、端口号及短链域名。填写的域名或路径需与实际回调URL中的域名或路径相同。
  80. // 2、填写的域名须通过ICP备案的验证。
  81. // 3、将文件MP_verify_6JqVkftKr39GMakA.txt(点击下载)上传至填写域名或路径指向的web服务器(或虚拟主机)的目录(若填写域名,将文件放置在域名根目录下,例如wx.qq.com/MP_verify_6JqVkftKr39GMakA.txt;若填写路径,将文件放置在路径目录下,例如wx.qq.com/mp/MP_verify_6JqVkftKr39GMakA.txt),并确保可以访问。
  82. func mpDomainAuthSecret(w http.ResponseWriter, r *http.Request) {
  83. fmt.Fprintf(w, "6JqVkftKr39GMakA")
  84. //由于需要什么ICP备案,这个功能不能使用
  85. }
  86. //for user's initial registration, especially for wechat users
  87. //they visit a url that is specifically designed for them to
  88. //auth and input their profile data.
  89. //the url's query string will contains a token and a signature
  90. //so that it's verified, by single get request, to allow people to
  91. //enter their details into the CRM system.
  92. //
  93. //this handler, check's the query sting ,set an auth cookie to the client
  94. //and serve angular app, through an URL "/profile/edit"
  95. //or if the user has already been registered,
  96. //redirect user to a URL "/pages/dashboard"
  97. //
  98. func initialRegistrationHandler(w http.ResponseWriter, r *http.Request) {
  99. expiration := time.Now().Add(10 * 365 * 24 * time.Hour)
  100. str := time.Now().String()
  101. cookie := http.Cookie{Name: "username", Value: str, Expires: expiration}
  102. http.SetCookie(w, &cookie)
  103. cookie = http.Cookie{Name: "signature", Value: "abcee", Expires: expiration}
  104. http.SetCookie(w, &cookie)
  105. http.Redirect(w, r, "http://192.168.1.39:4200/#pages/charts/chartist-js", 302)
  106. }