wechat.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "wechat-api/crontask"
  28. "wechat-api/internal/config"
  29. "wechat-api/internal/handler"
  30. "wechat-api/internal/svc"
  31. "github.com/robfig/cron/v3"
  32. "github.com/zeromicro/go-zero/core/conf"
  33. "github.com/zeromicro/go-zero/rest"
  34. )
  35. var configFile = flag.String("f", "etc/wechat.yaml", "the config file")
  36. func main() {
  37. flag.Parse()
  38. var c config.Config
  39. conf.MustLoad(*configFile, &c, conf.UseEnv())
  40. server := rest.MustNewServer(c.RestConf, rest.WithCors(c.CROSConf.Address))
  41. defer server.Stop()
  42. ctx := svc.NewServiceContext(c)
  43. handler.RegisterHandlers(server, ctx)
  44. cronCtx := cron.New(cron.WithChain(
  45. cron.SkipIfStillRunning(cron.DefaultLogger),
  46. cron.Recover(cron.DefaultLogger),
  47. ))
  48. crontask.ScheduleRun(cronCtx, ctx)
  49. go cronCtx.Start()
  50. defer cronCtx.Stop()
  51. fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
  52. server.Start()
  53. }