package main

import (
	"flag"
	"mesh/api"
	"mesh/bus"
	"mesh/conf"
	"mesh/db"
	"mesh/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(db.NewMongoDB)
	_ = app.Provide(db.NewRedisClient)
	_ = app.Provide(bus.NewNatsBus)

	// _ = app.Provide(func(app *conf.AppConf) (*etcd.Cli, error) {
	// 	return etcd.NewEtcdCli(&etcd.CliConfig{
	// 		Endpoints:   app.Etcd.Endpoints,
	// 		UserName:    app.Etcd.UserName,
	// 		Password:    app.Etcd.Password,
	// 		DialTimeout: time.Second * time.Duration(app.Etcd.DialTimeout),
	// 	})
	// })

	_ = app.Provide(api.NewHttpService)

	return app
}

func main() {

	flag.Parse()
	app := BuildApp()

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

		svc.Run()
		return nil
	})

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