package main

import (
	"flag"
	"work-device/api"
	"work-device/bus"
	"work-device/client"
	"work-device/conf"
	"work-device/log"

	"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.NewMongoDB)
	// _ = app.Provide(db.NewRedisClient)
	_ = app.Provide(api.NewHttpService)

	return app
}

func main() {

	flag.Parse()
	app := BuildApp()

	err := app.Invoke(func(config *conf.AppConf, busCenter *comm.NatsBus, svc *api.Service) error {
		go busCenter.Run(nil)
		go client.Connect()
		svc.Run()
		return nil
	})

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