package main

import (
	"fmt"
	"gitee.ltd/lxh/logger/log"
	"github.com/gin-contrib/pprof"
	"github.com/spf13/cast"
	"math/rand"
	"net/http"
	"os"
	"time"
	"wireguard-ui/config"
	"wireguard-ui/http/router"
	"wireguard-ui/initialize"
	"wireguard-ui/script"
)

func init() {
	initialize.Init()
	if err := script.New().Do(); err != nil {
		log.Errorf("执行脚本失败: %v", err.Error())
	}
}

func main() {
	rand.New(rand.NewSource(time.Now().Local().UnixNano()))
	router.Rooters()
	handler := router.InitRouter()

	if cast.ToBool(os.Getenv("ENABLED_PPROF")) {
		pprof.Register(handler, "/monitoring")
	}

	httpServe := http.Server{
		Addr:    fmt.Sprintf(":%d", config.Config.Http.Port),
		Handler: handler,
	}

	if err := httpServe.ListenAndServe(); err != nil {
		log.Panicf("启动http服务端失败: %v", err.Error())
	}
}