web.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package adminapi
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/mhaya"
  5. mhayaCron "github.com/mhaya/components/cron"
  6. mhayaGin "github.com/mhaya/components/gin"
  7. mhayaMongo "github.com/mhaya/components/mongo"
  8. checkCenter "github.com/mhaya/game/game_cluster/internal/component/check_center"
  9. "github.com/mhaya/game/game_cluster/internal/data"
  10. "github.com/mhaya/game/game_cluster/internal/mdb"
  11. "github.com/mhaya/game/game_cluster/nodes/adminapi/router"
  12. )
  13. func Run(profileFilePath, nodeId string) {
  14. // 配置mhaya引擎,加载profile配置文件
  15. app := mhaya.Configure(profileFilePath, nodeId, false, mhaya.Cluster)
  16. // 注册调度组件
  17. app.Register(mhayaCron.New())
  18. // 注册检查中心服是否启动组件
  19. app.Register(checkCenter.New())
  20. // 注册数据配表组件
  21. app.Register(data.New())
  22. // 加载http server组件
  23. app.Register(httpServerComponent(app.Address()))
  24. // 注册db组件
  25. app.Register(mhayaMongo.NewComponent())
  26. app.AddActors(
  27. &mdb.ActorDB{Name: nodeId},
  28. )
  29. app.Startup()
  30. }
  31. func httpServerComponent(addr string) *mhayaGin.Component {
  32. gin.SetMode(gin.DebugMode)
  33. // new http server
  34. httpServer := mhayaGin.NewHttp("http_server", addr)
  35. httpServer.Use(mhayaGin.Cors())
  36. // http server使用gin组件搭建,这里增加一个RecoveryWithZap中间件
  37. httpServer.Use(mhayaGin.RecoveryWithZap(true))
  38. // 注册 controller
  39. httpServer.Register(new(router.Controller))
  40. return httpServer
  41. }