package main

import (
	MC "app.yhyue.com/moapp/jybase/common"
	"app.yhyue.com/moapp/jybase/endless"

	// logrusx "app.yhyue.com/moapp/jylogx/logx"
	"flag"
	"fmt"
	IC "jyBXSubscribe/rpc/init"
	"jyBXSubscribe/rpc/internal/server"
	"jyBXSubscribe/rpc/internal/svc"
	"jyBXSubscribe/rpc/type/bxsubscribe"
	"log"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcfg"
	"github.com/zeromicro/go-zero/core/conf"

	// "github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var configF = flag.String("cf", "etc/bxsubscribe.yaml", "the config file")
var dbFile = flag.String("df", "etc/db.yaml", "the db file")
var logFile = flag.String("lf", "etc/logs.yaml", "the log file")

func main() {
	//初始化基本配置
	g.Cfg().GetAdapter().(*gcfg.AdapterFile).SetFileName("etc/config.yaml")
	//logx.SetWriter(logx.NewWriter(jylog.GetWriter()))
	conf.MustLoad(*configF, &IC.C)
	//初始化数据库配置
	conf.MustLoad(*dbFile, &IC.DB)
	//初始化日志信息
	conf.MustLoad(*logFile, &IC.Logc)
	IC.InitC()

	go func() {
		err := endless.ListenAndServe(":"+MC.InterfaceToStr(IC.C.Webrpcport), nil, func() {})
		if err != nil {
			log.Println("ListenAndServe: ", err)
		}
	}()

	ctx := svc.NewServiceContext(IC.C)
	svr := server.NewBxsubscribeServer(ctx)

	s := zrpc.MustNewServer(IC.C.RpcServerConf, func(grpcServer *grpc.Server) {
		bxsubscribe.RegisterBxsubscribeServer(grpcServer, svr)

		if IC.C.Mode == service.DevMode || IC.C.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})

	//日志记录
	// logx.SetWriter(logrusx.NewLogrusWriter())
	//s.AddUnaryInterceptors(util.CustomInterceptors)
	defer s.Stop()
	fmt.Printf("Starting rpc server at %s...\n", IC.C.ListenOn)
	s.Start()
}
