package main

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

	_ = 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)
	}
}
