package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"bp.jydev.jianyu360.cn/BaseService/gateway/core/node"
	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/config"
	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/handler"
	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/svc"
	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/resource.yaml", "the config file")

func main() {
	flag.Parse()
	var c config.Config
	conf.MustLoad(*configFile, &c)
	// logx 根据配置初始化
	logx.MustSetup(c.Logx)
	//初始化mysql
	InitMysql(c.Mysql.BaseService)
	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()
	handler.RegisterHandlers(server, ctx)
	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	closeNotify, err := node.NewNode(c.Etcd.Hosts...).Register(c.Etcd.Key, fmt.Sprint(c.Port))
	if err != nil {
		panic(err)
	}
	go func() {
		server.Start()
	}()
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	closeNotify()
}
