package main

import (
	"copter-train/api"
	"copter-train/bus"
	"copter-train/conf"
	"copter-train/db"
	"copter-train/log"
	"flag"

	"go.uber.org/dig"
	"infish.cn/comm"
)

var confFile = flag.String("conf", "./app.yaml", "conf file")

func BuildApp() *dig.Container {

	app := dig.New()
	_ = app.Provide(func() (*conf.AppConf, error) {
		return conf.NewAppConf(*confFile)
	})

	_ = app.Provide(bus.NewNatsBus)
	_ = app.Provide(db.NewConfigMongoDB)
	_ = app.Provide(db.NewConfigRedisClient)
	_ = app.Provide(api.NewHttpService)

	return app
}

func main() {
	flag.Parse()
	app := BuildApp()
	// comm.AppMongoMiration()

	err := app.Invoke(func(svc *api.Service, bus *comm.NatsBus) error {
		go bus.Run(nil)
		svc.Run()
		return nil
	})

	if err != nil {
		log.Errorf("the pink service start fail %+v", err)
		panic(err)
	}
}
