package web

import (
	"github.com/gofiber/fiber/v2/middleware/logger"
	"github.com/gofiber/fiber/v2/middleware/recover"
	"log/slog"
	"proxy-server/gateway/env"
	"strconv"

	"github.com/gofiber/fiber/v2"
)

type Server struct {
	web *fiber.App
}

func New() *Server {
	return &Server{}
}

func (s *Server) Run() error {
	slog.Info("启动接口服务", "服务端口", env.AppWebPort)
	s.web = fiber.New(fiber.Config{
		DisableStartupMessage: true,
	})

	// 配置中间件和路由
	s.web.Use(recover.New())
	s.web.Use(logger.New())
	Router(s.web)

	// 启动服务
	address := ":" + strconv.Itoa(int(env.AppWebPort))
	err := s.web.Listen(address)
	if err != nil {
		return err
	}

	return nil
}

func (s *Server) Stop() error {
	err := s.web.Shutdown()
	if err != nil {
		return err
	}

	return nil
}
