Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "WebAdmin/config"
  5. "WebAdmin/models"
  6. "database/sql"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "strconv"
  12. "strings"
  13.  
  14. _ "github.com/go-sql-driver/mysql"
  15. )
  16.  
  17. const (
  18. UserTypeFacebook string = "facebook"
  19. UserTypeGameCenter string = "game_center"
  20. UserTypeGooglePlay string = "google_play"
  21. UserTypeTouringIOS string = "touring_ios"
  22. UserTypeTouringAndroid string = "touring_android"
  23. UserTypeSignInWithApple string = "sign_in_with_apple"
  24. UserTypeLine string = "line"
  25. )
  26.  
  27. type Message struct {
  28. Title string `json:"title"`
  29. Message string `json:"message"`
  30. Gold string `json:"gold"`
  31. Chip string `json:"chip"`
  32. Ticket string `json:"ticket"`
  33. UIDS string `json:"UIDS"`
  34. GMUsername string `json:"GMUsername"`
  35. }
  36.  
  37. func main() {
  38. fmt.Println("----- Start server at port 11433 -----")
  39.  
  40. // http.Handle("/addGmGiftReward", withCors(addGmGiftReward))
  41. // http.Handle("/getInventoryItems", withCors(GetInventoryItems))
  42. //http.Handle("/getInventoryItems", withCors(GetInventoryItems))
  43. http.HandleFunc("/getInventoryItems", GetInventoryItems)
  44.  
  45. http.ListenAndServe("0.0.0.0:11433", nil)
  46. //log.Fatal(http.ListenAndServeTLS("0.0.0.0:11433", "certs/fullchain.pem", "certs/privkey.pem", nil))
  47.  
  48. }
  49.  
  50. func withCors(handler func(w http.ResponseWriter, r *http.Request)) http.Handler {
  51. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  52. addCors(&w)
  53. if r.Method == http.MethodOptions {
  54. return
  55. }
  56. handler(w, r)
  57. })
  58. }
  59.  
  60. func addCors(w *http.ResponseWriter) {
  61. (*w).Header().Set("Access-Control-Allow-Credentials", "true")
  62. (*w).Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
  63. (*w).Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
  64. (*w).Header().Set("Access-Control-Allow-Origin", "*")
  65. }
  66.  
  67. func addGmGiftReward(w http.ResponseWriter, r *http.Request) {
  68. b, err := ioutil.ReadAll(r.Body)
  69. defer r.Body.Close()
  70. if err != nil {
  71. // http.Error(w, err.Error(), 500)
  72. fmt.Print("send err", err)
  73. return
  74. }
  75.  
  76. // Unmarshal
  77. var msg Message
  78. err = json.Unmarshal(b, &msg)
  79. if err != nil {
  80. fmt.Print("Unmarshal err", err, "body ", string(b))
  81. http.Error(w, err.Error(), 500)
  82. return
  83. }
  84.  
  85. dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
  86. config.GameDBUser,
  87. config.GameDBPassword,
  88. config.GameDBHost,
  89. config.GameDBPort,
  90. config.GameDBName)
  91.  
  92. conn, err := sql.Open("mysql", dataSource)
  93. if err != nil {
  94. fmt.Print("conn open err", err)
  95. http.Error(w, err.Error(), 500)
  96. return
  97. }
  98. defer conn.Close()
  99. msg.Message = strings.ReplaceAll(msg.Message, ",", "")
  100. uids := strings.Fields(msg.UIDS)
  101.  
  102. Param := fmt.Sprintf("{ gold_free=%s , chip_free=%s , ticket_free=%s, source=gm_gift, title=%s, message=%s}", msg.Gold, msg.Chip, msg.Ticket, msg.Title, msg.Message)
  103. for i := 0; i < len(uids); i++ {
  104. platform, err := models.GetUserPlatform(conn, uids[i])
  105. if err == nil {
  106. switch platform {
  107. case UserTypeFacebook:
  108. models.SaveUserBatch(conn, uids[i], platform, "fb", Param)
  109. case UserTypeLine:
  110. models.SaveUserBatch(conn, uids[i], platform, "ln", Param)
  111. case UserTypeGooglePlay:
  112. models.SaveUserBatch(conn, uids[i], platform, "gp", Param)
  113. case UserTypeGameCenter:
  114. models.SaveUserBatch(conn, uids[i], platform, "gc", Param)
  115. case UserTypeTouringIOS:
  116. models.SaveUserBatch(conn, uids[i], platform, "gi", Param)
  117. case UserTypeSignInWithApple:
  118. models.SaveUserBatch(conn, uids[i], platform, "ap", Param)
  119. }
  120. }
  121. }
  122. numUser := len(uids)
  123. gold, _ := strconv.Atoi(msg.Gold)
  124. chip, _ := strconv.Atoi(msg.Chip)
  125. ticket, _ := strconv.Atoi(msg.Ticket)
  126. giftLog := fmt.Sprintf("gold=%v ,chip=%v ,ticket=%v ", gold*numUser, chip*numUser, ticket*numUser)
  127. go saveWebLog(msg.GMUsername, Param, msg.UIDS, "GM_Gift", numUser, giftLog)
  128. fmt.Fprint(w, "success")
  129. }
  130.  
  131. func GetInventoryItems(w http.ResponseWriter, r *http.Request) {
  132.  
  133. // dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
  134. // config.GameDBUser,
  135. // config.GameDBPassword,
  136. // config.GameDBHost,
  137. // config.GameDBPort,
  138. // config.GameDBName)
  139.  
  140. // conn, err := sql.Open("mysql", dataSource)
  141. // if err != nil {
  142. // fmt.Print("conn open err", err)
  143. // http.Error(w, err.Error(), 500)
  144. // return
  145. // }
  146. // defer conn.Close()
  147. // respItems, err := models.GetProfileItemList(conn)
  148. // fmt.Print("err GetInventoryItems ", err)
  149. // jsonResp, err := json.Marshal(respItems)
  150. // if err != nil {
  151. // fmt.Print("json.Marshal ", err)
  152. // }
  153. // // go saveWebLog(msg.GMUsername, Param, msg.UIDS, "GM_Gift", numUser, giftLog)
  154. fmt.Fprint(w, "Done")
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement