package main

import (
	"fmt"
	"go-account-register/config"
	"go-account-register/router"
	"go-account-register/services"
	"go-account-register/utils"
	"log"
	"os"
	"os/signal"
	"strconv"
	"syscall"

	"github.com/gin-gonic/gin"
)

func main() {
	appConfig, err := config.LoadConfig()
	if err != nil {
		fmt.Println(err)
		fmt.Println("读取配置失败")
	}
	app := gin.Default()
	// 注册路由
	router.Init(app)
	utils.Connect()
	go handleShutdownSignals()

	app.Run(":" + strconv.Itoa(appConfig.Port))
}

// 处理关闭信号的函数
func handleShutdownSignals() {
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

	// 阻塞等待信号
	sig := <-sigChan
	log.Printf("接收到信号: %v, 关闭所有浏览器...", sig)
	services.Browser.CancelAllBrowser()
	accountService := services.InitTwitterAccountService()
	accountService.OfflineAllAccount()
	// 强制退出程序
	os.Exit(0)
}
