router.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. package router
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "bet24.com/servers/payment/dot"
  12. "bet24.com/servers/payment/huawei"
  13. "bet24.com/servers/payment/payermax"
  14. "bet24.com/servers/payment/adjust"
  15. "bet24.com/servers/payment/apple"
  16. "bet24.com/servers/payment/config"
  17. "bet24.com/servers/payment/controller"
  18. "bet24.com/servers/payment/facebook"
  19. "bet24.com/servers/payment/google"
  20. "bet24.com/servers/payment/hall"
  21. "bet24.com/servers/payment/middleware"
  22. "bet24.com/servers/payment/update"
  23. "github.com/gin-gonic/gin"
  24. "github.com/mattn/go-colorable"
  25. )
  26. func Run() {
  27. // 强制日志颜色化
  28. gin.ForceConsoleColor()
  29. // 设置颜色输出,识别 console 色值
  30. gin.DefaultWriter = colorable.NewColorableStdout()
  31. // 设置日志模式
  32. gin.SetMode(gin.DebugMode)
  33. // 创建一个默认的路由
  34. r := gin.Default()
  35. r.Static("/static", "./html/static")
  36. r.LoadHTMLGlob("html/templates/*")
  37. r.Use(middleware.CheckValid(), middleware.Cors())
  38. // 创建一个默认的路由(HTTPS)
  39. rTls := gin.Default()
  40. rTls.Static("/static", "./html/static")
  41. rTls.LoadHTMLGlob("html/templates/*")
  42. rTls.Use(middleware.TlsHandler(), middleware.CheckValid(), middleware.Cors()) // 处理SSL的中间件
  43. // ------------------------------苹果支付------------------------------
  44. appleGroup := rTls.Group("/apple")
  45. {
  46. // 苹果验证
  47. appleGroup.POST("/verify", apple.Verify)
  48. }
  49. adjustGroup := rTls.Group("/adjust")
  50. {
  51. adjustGroup.POST("/notify", adjust.Notify)
  52. adjustGroup.GET("/notify", adjust.Notify)
  53. }
  54. // ------------------------------谷歌支付------------------------------
  55. googleGroup := rTls.Group("/google")
  56. {
  57. // 谷歌支付验证
  58. googleGroup.POST("/verify", google.Verify)
  59. googleGroup.GET("/rewardAD", google.RewardAD)
  60. googleGroup.POST("/rewardAD", google.RewardAD)
  61. }
  62. // ------------------------------facebook------------------------------
  63. facebookGroup := rTls.Group("/facebook")
  64. {
  65. // facebook
  66. facebookGroup.POST("/del/:id", facebook.Del)
  67. facebookGroup.POST("/info", facebook.Info)
  68. facebookGroup.GET("/info", facebook.Info)
  69. }
  70. // ------------------------------cropay支付------------------------------
  71. //cropayGroupTls := rTls.Group("/cropay")
  72. //{
  73. // // 下单(POST)
  74. // cropayGroupTls.POST("/order", cropay.Order)
  75. // // 下单(GET)
  76. // cropayGroupTls.GET("/order", cropay.Order)
  77. //
  78. // // 回调(POST)
  79. // cropayGroupTls.POST("/notify", cropay.Notify)
  80. // // 回调(GET)
  81. // cropayGroupTls.GET("/notify", cropay.Notify)
  82. //
  83. // // 成功(POST)
  84. // cropayGroupTls.POST("/succ_back", cropay.Succ)
  85. // // 成功(GET)
  86. // cropayGroupTls.GET("/succ_back", cropay.Succ)
  87. //
  88. // // 失败(POST)
  89. // cropayGroupTls.POST("/fail_back", cropay.Fail)
  90. // // 失败(GET)
  91. // cropayGroupTls.GET("/fail_back", cropay.Fail)
  92. //}
  93. // 聚合支付、提现
  94. //zhongshuiGroupTls := rTls.Group("/zshui")
  95. //{
  96. // // 充值下单(POST)
  97. // zhongshuiGroupTls.POST("/payOrder", zhongshui.PayOrder)
  98. // // 充值下单(GET)
  99. // zhongshuiGroupTls.GET("/payOrder", zhongshui.PayOrder)
  100. //
  101. // // 支付完跳转处理(POST)
  102. // zhongshuiGroupTls.POST("/payCallback", zhongshui.PayCallback)
  103. // // 支付完跳转处理(GET)
  104. // zhongshuiGroupTls.GET("/payCallback", zhongshui.PayCallback)
  105. //
  106. // // 充值回调(POST)
  107. // zhongshuiGroupTls.POST("/payNotify", zhongshui.PayNotify)
  108. // // 充值回调(GET)
  109. // zhongshuiGroupTls.GET("/payNotify", zhongshui.PayNotify)
  110. //
  111. // // 提现请求(POST)
  112. // zhongshuiGroupTls.POST("/withdrawOrder", zhongshui.WithdrawOrder)
  113. // // 提现请求(GET)
  114. // zhongshuiGroupTls.GET("/withdrawOrder", zhongshui.WithdrawOrder)
  115. //
  116. // // 提现回调(POST)
  117. // zhongshuiGroupTls.POST("/withdrawNotify", zhongshui.WithdrawNotify)
  118. // // 提现回调(GET)
  119. // zhongshuiGroupTls.GET("/withdrawNotify", zhongshui.WithdrawNotify)
  120. //}
  121. // Kaya支付、提现
  122. //kayaGroupTls := rTls.Group("/kaya")
  123. //{
  124. // // 充值下单(POST)
  125. // kayaGroupTls.POST("/payOrder", kaya.PayOrder)
  126. // // 充值下单(GET)
  127. // kayaGroupTls.GET("/payOrder", kaya.PayOrder)
  128. //
  129. // // 充值回调(POST)
  130. // kayaGroupTls.POST("/payNotify", kaya.PayNotify)
  131. // // 充值回调(GET)
  132. // kayaGroupTls.GET("/payNotify", kaya.PayNotify)
  133. //
  134. // // 提现请求(POST)
  135. // kayaGroupTls.POST("/withdrawOrder", kaya.WithdrawOrder)
  136. // // 提现请求(GET)
  137. // kayaGroupTls.GET("/withdrawOrder", kaya.WithdrawOrder)
  138. //
  139. // // 提现回调(POST)
  140. // kayaGroupTls.POST("/withdrawNotify", kaya.WithdrawNotify)
  141. // // 提现回调(GET)
  142. // kayaGroupTls.GET("/withdrawNotify", kaya.WithdrawNotify)
  143. //}
  144. // toppay 支付、提现
  145. //toppayGroupTls := rTls.Group("/toppay")
  146. //{
  147. // // 充值下单(POST)
  148. // toppayGroupTls.POST("/payOrder", toppay.PayOrder)
  149. // // 充值下单(GET)
  150. // toppayGroupTls.GET("/payOrder", toppay.PayOrder)
  151. //
  152. // // 充值回调(POST)
  153. // toppayGroupTls.POST("/payNotify", toppay.PayNotify)
  154. // // 充值回调(GET)
  155. // toppayGroupTls.GET("/payNotify", toppay.PayNotify)
  156. //
  157. // // 提现请求(POST)
  158. // toppayGroupTls.POST("/withdrawOrder", toppay.WithdrawOrder)
  159. // // 提现请求(GET)
  160. // toppayGroupTls.GET("/withdrawOrder", toppay.WithdrawOrder)
  161. //
  162. // // 提现回调(POST)
  163. // toppayGroupTls.POST("/withdrawNotify", toppay.WithdrawNotify)
  164. // // 提现回调(GET)
  165. // toppayGroupTls.GET("/withdrawNotify", toppay.WithdrawNotify)
  166. //}
  167. // toppay 支付、提现
  168. //toppayGroup := r.Group("/toppay")
  169. //{
  170. // // 充值下单(POST)
  171. // toppayGroup.POST("/payOrder", toppay.PayOrder)
  172. // // 充值下单(GET)
  173. // toppayGroup.GET("/payOrder", toppay.PayOrder)
  174. //
  175. // // 充值回调(POST)
  176. // toppayGroup.POST("/payNotify", toppay.PayNotify)
  177. // // 充值回调(GET)
  178. // toppayGroup.GET("/payNotify", toppay.PayNotify)
  179. //
  180. // // 提现请求(POST)
  181. // toppayGroup.POST("/withdrawOrder", toppay.WithdrawOrder)
  182. // // 提现请求(GET)
  183. // toppayGroup.GET("/withdrawOrder", toppay.WithdrawOrder)
  184. //
  185. // // 提现回调(POST)
  186. // toppayGroup.POST("/withdrawNotify", toppay.WithdrawNotify)
  187. // // 提现回调(GET)
  188. // toppayGroup.GET("/withdrawNotify", toppay.WithdrawNotify)
  189. //}
  190. // flashpay 支付、提现
  191. //flashpayGroup := r.Group("/flashpay")
  192. //{
  193. // // 充值下单(POST)
  194. // flashpayGroup.POST("/payOrder", flash.PayOrder)
  195. // // 充值下单(GET)
  196. // flashpayGroup.GET("/payOrder", flash.PayOrder)
  197. //
  198. // // 充值回调(POST)
  199. // flashpayGroup.POST("/payNotify", flash.PayNotify)
  200. // // 充值回调(GET)
  201. // flashpayGroup.GET("/payNotify", flash.PayNotify)
  202. //
  203. // // 提现请求(POST)
  204. // flashpayGroup.POST("/withdrawOrder", flash.WithdrawOrder)
  205. // // 提现请求(GET)
  206. // flashpayGroup.GET("/withdrawOrder", flash.WithdrawOrder)
  207. //
  208. // // 提现回调(POST)
  209. // flashpayGroup.POST("/withdrawNotify", flash.WithdrawNotify)
  210. // // 提现回调(GET)
  211. // flashpayGroup.GET("/withdrawNotify", flash.WithdrawNotify)
  212. //}
  213. // Lets支付、提现
  214. //letsGroupTls := rTls.Group("/lets")
  215. //{
  216. // // 充值下单(POST)
  217. // letsGroupTls.POST("/payOrder", lets.PayOrder)
  218. // // 充值下单(GET)
  219. // letsGroupTls.GET("/payOrder", lets.PayOrder)
  220. //
  221. // // 充值回调(POST)
  222. // letsGroupTls.POST("/payNotify", lets.PayNotify)
  223. // // 充值回调(GET)
  224. // letsGroupTls.GET("/payNotify", lets.PayNotify)
  225. //
  226. // // 支付完跳转处理(POST)
  227. // letsGroupTls.POST("/return", lets.PayCallback)
  228. // // 支付完跳转处理(GET)
  229. // letsGroupTls.GET("/return", lets.PayCallback)
  230. //
  231. // // 提现请求(POST)
  232. // letsGroupTls.POST("/withdrawOrder", lets.WithdrawOrder)
  233. // // 提现请求(GET)
  234. // letsGroupTls.GET("/withdrawOrder", lets.WithdrawOrder)
  235. //
  236. // // 提现回调(POST)
  237. // letsGroupTls.POST("/withdrawNotify", lets.WithdrawNotify)
  238. // // 提现回调(GET)
  239. // letsGroupTls.GET("/withdrawNotify", lets.WithdrawNotify)
  240. //}
  241. // Liu支付、提现
  242. //liuGroupTls := rTls.Group("/liu")
  243. //{
  244. // // 充值下单(POST)
  245. // liuGroupTls.POST("/payOrder", liu.PayOrder)
  246. // // 充值下单(GET)
  247. // liuGroupTls.GET("/payOrder", liu.PayOrder)
  248. //
  249. // // 充值回调(POST)
  250. // liuGroupTls.POST("/payNotify", liu.PayNotify)
  251. // // 充值回调(GET)
  252. // liuGroupTls.GET("/payNotify", liu.PayNotify)
  253. //
  254. // // 支付完跳转处理(POST)
  255. // liuGroupTls.POST("/return", liu.PayCallback)
  256. // // 支付完跳转处理(GET)
  257. // liuGroupTls.GET("/return", liu.PayCallback)
  258. //
  259. // // 提现请求(POST)
  260. // liuGroupTls.POST("/withdrawOrder", liu.WithdrawOrder)
  261. // // 提现请求(GET)
  262. // liuGroupTls.GET("/withdrawOrder", liu.WithdrawOrder)
  263. //
  264. // // 提现审核
  265. // liuGroupTls.POST("/withdrawAudit", liu.WithdrawAudit)
  266. //}
  267. // dingpei支付、提现
  268. //dingpeiGroupTls := rTls.Group("/dingpei")
  269. //{
  270. // // 充值下单(POST)
  271. // dingpeiGroupTls.POST("/payOrder", dingpei.PayOrder)
  272. // // 充值下单(GET)
  273. // dingpeiGroupTls.GET("/payOrder", dingpei.PayOrder)
  274. //
  275. // // 充值回调(POST)
  276. // dingpeiGroupTls.POST("/payNotify", dingpei.PayNotify)
  277. // // 充值回调(GET)
  278. // dingpeiGroupTls.GET("/payNotify", dingpei.PayNotify)
  279. //
  280. // // 提现请求(POST)
  281. // dingpeiGroupTls.POST("/withdrawOrder", dingpei.WithdrawOrder)
  282. // // 提现请求(GET)
  283. // dingpeiGroupTls.GET("/withdrawOrder", dingpei.WithdrawOrder)
  284. //
  285. // // 提现回调(POST)
  286. // dingpeiGroupTls.POST("/withdrawNotify", dingpei.WithdrawNotify)
  287. // // 提现回调(GET)
  288. // dingpeiGroupTls.GET("/withdrawNotify", dingpei.WithdrawNotify)
  289. //
  290. // // 支付完跳转处理(POST)
  291. // dingpeiGroupTls.POST("/return", dingpei.PayCallback)
  292. // // 支付完跳转处理(GET)
  293. // dingpeiGroupTls.GET("/return", dingpei.PayCallback)
  294. //}
  295. // payermax支付、提现
  296. payermaxGroupTls := rTls.Group("/payermax")
  297. {
  298. // 充值下单(POST)
  299. payermaxGroupTls.POST("/payOrder", payermax.PayOrder)
  300. // 充值下单(GET)
  301. payermaxGroupTls.GET("/payOrder", payermax.PayOrder)
  302. // 充值回调(POST)
  303. payermaxGroupTls.POST("/payNotify", payermax.PayNotify)
  304. // 充值回调(GET)
  305. payermaxGroupTls.GET("/payNotify", payermax.PayNotify)
  306. // 提现请求(POST)
  307. payermaxGroupTls.POST("/withdrawOrder", payermax.WithdrawOrder)
  308. // 提现请求(GET)
  309. payermaxGroupTls.GET("/withdrawOrder", payermax.WithdrawOrder)
  310. // 提现回调(POST)
  311. payermaxGroupTls.POST("/withdrawNotify", payermax.WithdrawNotify)
  312. // 提现回调(GET)
  313. payermaxGroupTls.GET("/withdrawNotify", payermax.WithdrawNotify)
  314. // 支付完跳转处理(POST)
  315. payermaxGroupTls.POST("/return", payermax.PayCallback)
  316. // 支付完跳转处理(GET)
  317. payermaxGroupTls.GET("/return", payermax.PayCallback)
  318. }
  319. // // livepay 支付、提现
  320. // livepayGroupTls := r.Group("/livepay")
  321. // {
  322. // // 充值下单(POST)
  323. // livepayGroupTls.POST("/payOrder", livepay.PayOrder)
  324. // // 充值下单(GET)
  325. // livepayGroupTls.GET("/payOrder", livepay.PayOrder)
  326. // // 充值回调(POST)
  327. // livepayGroupTls.POST("/payNotify", livepay.PayNotify)
  328. // // 充值回调(GET)
  329. // livepayGroupTls.GET("/payNotify", livepay.PayNotify)
  330. // // 支付完跳转处理(POST)
  331. // livepayGroupTls.POST("/return", livepay.PayCallback)
  332. // // 支付完跳转处理(GET)
  333. // livepayGroupTls.GET("/return", livepay.PayCallback)
  334. // }
  335. // crush支付、提现
  336. //crushGroupTls := rTls.Group("/crush")
  337. //{
  338. // // 充值下单(POST)
  339. // crushGroupTls.POST("/payOrder", crush.PayOrder)
  340. // // 充值下单(GET)
  341. // crushGroupTls.GET("/payOrder", crush.PayOrder)
  342. //
  343. // // 充值回调(POST)
  344. // crushGroupTls.POST("/payNotify", crush.PayNotify)
  345. // // 充值回调(GET)
  346. // crushGroupTls.GET("/payNotify", crush.PayNotify)
  347. //
  348. // // 提现请求(POST)
  349. // crushGroupTls.POST("/withdrawOrder", crush.WithdrawOrder)
  350. // // 提现请求(GET)
  351. // crushGroupTls.GET("/withdrawOrder", crush.WithdrawOrder)
  352. //
  353. // // 提现回调(POST)
  354. // crushGroupTls.POST("/withdrawNotify", crush.WithdrawNotify)
  355. // // 提现回调(GET)
  356. // crushGroupTls.GET("/withdrawNotify", crush.WithdrawNotify)
  357. //
  358. // // 支付完跳转处理(POST)
  359. // crushGroupTls.POST("/return", crush.PayCallback)
  360. // // 支付完跳转处理(GET)
  361. // crushGroupTls.GET("/return", crush.PayCallback)
  362. //}
  363. // oPay支付、提现
  364. //oPayGroupTls := rTls.Group("/opay")
  365. //{
  366. // // 充值下单(POST)
  367. // oPayGroupTls.POST("/payOrder", opay.PayOrder)
  368. // // 充值下单(GET)
  369. // oPayGroupTls.GET("/payOrder", opay.PayOrder)
  370. //
  371. // // 充值回调(POST)
  372. // oPayGroupTls.POST("/payNotify", opay.PayNotify)
  373. // // 充值回调(GET)
  374. // oPayGroupTls.GET("/payNotify", opay.PayNotify)
  375. //
  376. // // 支付完跳转处理(POST)
  377. // oPayGroupTls.POST("/return", opay.PayCallback)
  378. // // 支付完跳转处理(GET)
  379. // oPayGroupTls.GET("/return", opay.PayCallback)
  380. //}
  381. // paymob支付、提现
  382. //paymobGroupTls := rTls.Group("/paymob")
  383. //{
  384. // // 充值下单(POST)
  385. // paymobGroupTls.POST("/payOrder", paymob.PayOrder)
  386. // // 充值下单(GET)
  387. // paymobGroupTls.GET("/payOrder", paymob.PayOrder)
  388. //
  389. // // 充值回调(POST)
  390. // paymobGroupTls.POST("/payNotify", paymob.PayNotify)
  391. // // 充值回调(GET)
  392. // paymobGroupTls.GET("/payNotify", paymob.PayNotify)
  393. //
  394. // // 支付完跳转处理(POST)
  395. // paymobGroupTls.POST("/return", paymob.PayCallback)
  396. // // 支付完跳转处理(GET)
  397. // paymobGroupTls.GET("/return", paymob.PayCallback)
  398. //}
  399. // fawry支付、提现
  400. //fawryGroupTls := rTls.Group("/fawry")
  401. //{
  402. // // 充值下单(POST)
  403. // fawryGroupTls.POST("/payOrder", fawry.PayOrder)
  404. // // 充值下单(GET)
  405. // fawryGroupTls.GET("/payOrder", fawry.PayOrder)
  406. //
  407. // // 充值回调(POST)
  408. // fawryGroupTls.POST("/payNotify", fawry.PayNotify)
  409. // // 充值回调(GET)
  410. // fawryGroupTls.GET("/payNotify", fawry.PayNotify)
  411. //}
  412. // la支付、提现
  413. //laGroupTls := rTls.Group("/la")
  414. //{
  415. // // 充值下单(POST)
  416. // laGroupTls.POST("/payOrder", lapay.PayOrder)
  417. // // 充值下单(GET)
  418. // laGroupTls.GET("/payOrder", lapay.PayOrder)
  419. //
  420. // // 充值回调(POST)
  421. // laGroupTls.POST("/payNotify", lapay.PayNotify)
  422. // // 充值回调(GET)
  423. // laGroupTls.GET("/payNotify", lapay.PayNotify)
  424. //
  425. // // 支付完跳转处理(POST)
  426. // laGroupTls.POST("/return", lapay.PayCallback)
  427. // // 支付完跳转处理(GET)
  428. // laGroupTls.GET("/return", lapay.PayCallback)
  429. //}
  430. // coda支付、提现
  431. //codaGroupTls := rTls.Group("/coda")
  432. //{
  433. // // 充值下单(POST)
  434. // codaGroupTls.POST("/payOrder", coda.PayOrder)
  435. // // 充值下单(GET)
  436. // codaGroupTls.GET("/payOrder", coda.PayOrder)
  437. //
  438. // // 充值回调(POST)
  439. // codaGroupTls.POST("/payNotify", coda.PayNotify)
  440. // // 充值回调(GET)
  441. // codaGroupTls.GET("/payNotify", coda.PayNotify)
  442. //
  443. // // 支付完跳转处理(POST)
  444. // codaGroupTls.POST("/return", coda.PayCallback)
  445. // // 支付完跳转处理(GET)
  446. // codaGroupTls.GET("/return", coda.PayCallback)
  447. //
  448. //}
  449. // 华为支付
  450. huaweiGroupTls := rTls.Group("/huawei")
  451. {
  452. // 充值下单(POST)
  453. huaweiGroupTls.POST("/order", huawei.Order)
  454. // 充值下单(GET)
  455. huaweiGroupTls.GET("/order", huawei.Order)
  456. // 充值回调(POST)
  457. huaweiGroupTls.POST("/notify", huawei.Notify)
  458. // 充值回调(GET)
  459. huaweiGroupTls.GET("/notify", huawei.Notify)
  460. }
  461. // 检查更新
  462. checkUpdate := r.Group("/update")
  463. {
  464. // 查询余额
  465. checkUpdate.GET("/checkupdate", update.CheckUpdate)
  466. }
  467. // 大厅功能
  468. hallTls := rTls.Group("/hall")
  469. {
  470. // 师徒登录(绑定)
  471. hallTls.POST("/register", hall.Register)
  472. // 代理绑定
  473. hallTls.POST("/bind", hall.Bind)
  474. // 汇率列表
  475. hallTls.POST("/getExchangeRate", hall.GetExchangeRateList)
  476. // 网页商城列表
  477. hallTls.POST("/getShopList", hall.GetShopList)
  478. // 用户信息
  479. hallTls.POST("/userInfo", hall.GetUserInfo)
  480. hallTls.POST("/isInReview", hall.IsInReview)
  481. hallTls.GET("/isInReview", hall.IsInReview)
  482. hallTls.POST("/uploadFile", hall.UploadFile)
  483. hallTls.POST("/feedback", hall.Feedback)
  484. hallTls.GET("/feedback", hall.Feedback)
  485. }
  486. // 打点
  487. dotTls := rTls.Group("/dot")
  488. {
  489. // 中转前端的打点到服务器
  490. dotTls.POST("/addDot", dot.AddDot)
  491. dotTls.GET("/addDot", dot.AddDot)
  492. }
  493. // ------------------------------404错误------------------------------
  494. // 404错误
  495. r.NoRoute(controller.NotFoundRoute)
  496. r.NoMethod(controller.NotFoundMethod)
  497. // 启动HTTPS服务(gin默认启动服务)
  498. go rTls.RunTLS(fmt.Sprintf(":%d", config.Server.TlsPort), config.Server.TlsCert, config.Server.TlsKey)
  499. // 启动HTTP服务(gin默认启动服务)
  500. r.Run(fmt.Sprintf(":%d", config.Server.WebPort))
  501. // ------------------------------优雅关机------------------------------
  502. // 实现优雅的关机
  503. srv := &http.Server{
  504. Addr: fmt.Sprintf("%d", config.Server.WebPort),
  505. Handler: r,
  506. }
  507. go func() {
  508. fmt.Printf("Listening and serving HTTP on %v\n", srv.Addr)
  509. // 启动HTTP服务
  510. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  511. log.Fatalf("listen: %s\n", err)
  512. }
  513. }()
  514. // 等待中断信号来优雅地关闭服务器,为关闭服务器操作设置一个超时时长
  515. quit := make(chan os.Signal, 1) // 创建一个接收信号的通道
  516. // kill 默认会发送 syscall.SIGTERM 信号
  517. // kill -2 发送 syscall.SIGINT 信号,我们常用的 Ctrl+C 就是触发系统SIGINT信号
  518. // kill -9 发送 syscall.SIGKILL 信号,但是不能被捕获,所以不需要添加它
  519. // signal.Notify 把收到的 syscall.SIGINT 或 syscall.SIGTERM 信号转发给 quit
  520. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) // 此处不会阻塞
  521. <-quit // 阻塞在此,当接收到上述两种信号时才会往下执行
  522. fmt.Println("Shutdown Server!")
  523. // 创建一个30秒超时的context
  524. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  525. defer cancel()
  526. // 优雅关闭服务(将未处理完的请求处理完再关闭服务),超时就退出
  527. if err := srv.Shutdown(ctx); err != nil {
  528. log.Fatal("Server Shutdown:", err)
  529. }
  530. fmt.Println("payment server closed ...")
  531. }