web.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  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/web/controller"
  12. "github.com/mhaya/game/game_cluster/nodes/web/sdk"
  13. )
  14. func main() {
  15. // 配置mhaya引擎,加载profile配置文件
  16. app := mhaya.Configure("./game/config/profile-gc.json", "m-web-1", false, mhaya.Cluster)
  17. // 注册调度组件
  18. app.Register(mhayaCron.New())
  19. // 注册检查中心服是否启动组件
  20. app.Register(checkCenter.New())
  21. // 注册数据配表组件
  22. app.Register(data.New())
  23. // 加载http server组件
  24. app.Register(httpServerComponent(app.Address()))
  25. // 注册db组件
  26. app.Register(mhayaMongo.NewComponent())
  27. app.AddActors(
  28. &mdb.ActorDB{},
  29. )
  30. // 加载sdk逻辑
  31. sdk.Init(app)
  32. // 启动mhaya引擎
  33. app.Startup()
  34. }
  35. func httpServerComponent(addr string) *mhayaGin.Component {
  36. gin.SetMode(gin.DebugMode)
  37. // new http server
  38. httpServer := mhayaGin.NewHttp("http_server", addr)
  39. httpServer.Use(mhayaGin.Cors())
  40. // http server使用gin组件搭建,这里增加一个RecoveryWithZap中间件
  41. httpServer.Use(mhayaGin.RecoveryWithZap(true))
  42. //注册 controller
  43. httpServer.Register(new(controller.Controller))
  44. return httpServer
  45. }