package core

import (
	"fmt"
	"mtp20access/global"
	"mtp20access/initialize"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type server interface {
	ListenAndServe() error
}

// RunApiServer 启动Http API 服务
func RunApiServer() {
	// 启动服务
	Router := initialize.Routers()
	address := fmt.Sprintf(":%d", global.M2A_CONFIG.System.Addr)
	s := initServer(address, Router)
	global.M2A_LOG.Info("server run success on ", zap.String("address", address))
	global.M2A_LOG.Error(s.ListenAndServe().Error())
}

func initServer(address string, router *gin.Engine) server {
	return &http.Server{
		Addr:           address,
		Handler:        router,
		ReadTimeout:    20 * time.Second,
		WriteTimeout:   20 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
}
