Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

133 lines
4.8KB

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