wechat.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // wechat
  2. //
  3. // Description: wechat service
  4. //
  5. // Schemes: http, https
  6. // Host: localhost:0
  7. // BasePath: /
  8. // Version: 0.0.1
  9. // SecurityDefinitions:
  10. // Token:
  11. // type: apiKey
  12. // name: Authorization
  13. // in: header
  14. // Security:
  15. // - Token: []
  16. // Consumes:
  17. // - application/json
  18. //
  19. // Produces:
  20. // - application/json
  21. //
  22. // swagger:meta
  23. package main
  24. import (
  25. "flag"
  26. "fmt"
  27. "github.com/zeromicro/go-zero/core/logx"
  28. "time"
  29. "wechat-api/crontask"
  30. "wechat-api/internal/config"
  31. "wechat-api/internal/handler"
  32. "wechat-api/internal/pkg/customer_of_im/channel"
  33. "wechat-api/internal/svc"
  34. "github.com/robfig/cron/v3"
  35. "github.com/zeromicro/go-zero/core/conf"
  36. "github.com/zeromicro/go-zero/rest"
  37. )
  38. var configFile = flag.String("f", "etc/wechat.yaml", "the config file")
  39. func main() {
  40. flag.Parse()
  41. var c config.Config
  42. conf.MustLoad(*configFile, &c, conf.UseEnv())
  43. server := rest.MustNewServer(c.RestConf, rest.WithCors(c.CROSConf.Address))
  44. defer server.Stop()
  45. ctx := svc.NewServiceContext(c)
  46. handler.RegisterHandlers(server, ctx)
  47. //个微处理程序 没有彻底完成之前不能开,和cow重复了
  48. if len(ctx.WechatWs) == 0 {
  49. fmt.Println("wechat ws is nil")
  50. } else if ctx.WechatWs["default"] != nil {
  51. var ic channel.IChannel
  52. for _, ws := range ctx.WechatWs {
  53. switch ws.CTypes {
  54. case "wechat":
  55. ic = channel.NewWechatChannel(ws, ctx)
  56. }
  57. ws.RegisterMessageHandler(ic.OnMessage)
  58. }
  59. logx.Info("注册个微处理通道~")
  60. }
  61. for {
  62. time.Sleep(time.Second * 10)
  63. }
  64. cronCtx := cron.New(cron.WithChain(
  65. cron.SkipIfStillRunning(cron.DefaultLogger),
  66. cron.Recover(cron.DefaultLogger),
  67. ))
  68. crontask.ScheduleRun(cronCtx, ctx)
  69. go cronCtx.Start()
  70. defer cronCtx.Stop()
  71. fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
  72. server.Start()
  73. }