package adminapi

import (
	"github.com/gin-gonic/gin"
	"github.com/mhaya"
	mhayaCron "github.com/mhaya/components/cron"
	mhayaGin "github.com/mhaya/components/gin"
	mhayaMongo "github.com/mhaya/components/mongo"
	checkCenter "github.com/mhaya/game/game_cluster/internal/component/check_center"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb"
	"github.com/mhaya/game/game_cluster/nodes/adminapi/router"
)

func Run(profileFilePath, nodeId string) {
	// 配置mhaya引擎,加载profile配置文件
	app := mhaya.Configure(profileFilePath, nodeId, false, mhaya.Cluster)
	// 注册调度组件
	app.Register(mhayaCron.New())
	// 注册检查中心服是否启动组件
	app.Register(checkCenter.New())
	// 注册数据配表组件
	app.Register(data.New())
	// 加载http server组件
	app.Register(httpServerComponent(app.Address()))
	// 注册db组件
	app.Register(mhayaMongo.NewComponent())
	app.AddActors(
		&mdb.ActorDB{Name: nodeId},
	)
	app.Startup()
}

func httpServerComponent(addr string) *mhayaGin.Component {
	gin.SetMode(gin.DebugMode)

	// new http server
	httpServer := mhayaGin.NewHttp("http_server", addr)
	httpServer.Use(mhayaGin.Cors())

	// http server使用gin组件搭建，这里增加一个RecoveryWithZap中间件
	httpServer.Use(mhayaGin.RecoveryWithZap(true))

	// 注册 controller
	httpServer.Register(new(router.Controller))

	return httpServer
}
