package server

import (
	"flag"
	"gadmin/config"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/sirupsen/logrus"
)

func Start() {
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		panic(err)
	}
	time.Local = loc // 设置全局默认时区
	cfgFile := ".env"
	if config.SysType == "windows" {
		// envFilename = "local.env"
		// go run main.go -c local67.env
		flag.StringVar(&cfgFile, "c", "local.env", "配置环境")
		flag.Parse()
	}

	// 初始化日志，建立与数据库 redis nats etcd等的连接
	config.Init(cfgFile)

	// 网页服务
	go func() {
		logrus.Infof("服务端口 [%v]", os.Getenv("ADMIN_SERVER_PORT"))
		if err := NewEngine().Run(os.Getenv("ADMIN_SERVER_PORT")); err != nil {
			logrus.Panicf("服务启动失败：%v", err)
		}
	}()

	after()
}

func after() {
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	quantity := <-quit
	logrus.Infoln("收到中断信号:", quantity.String())

	logrus.Infoln("已退出")
}
