server.go 777 B

123456789101112131415161718192021222324252627282930313233343536
  1. package core
  2. import (
  3. "fmt"
  4. "mtp20access/global"
  5. "mtp20access/initialize"
  6. "net/http"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "go.uber.org/zap"
  10. )
  11. type server interface {
  12. ListenAndServe() error
  13. }
  14. // RunApiServer 启动Http API 服务
  15. func RunApiServer() {
  16. // 启动服务
  17. Router := initialize.Routers()
  18. address := fmt.Sprintf(":%d", global.M2A_CONFIG.System.Addr)
  19. s := initServer(address, Router)
  20. global.M2A_LOG.Info("server run success on ", zap.String("address", address))
  21. global.M2A_LOG.Error(s.ListenAndServe().Error())
  22. }
  23. func initServer(address string, router *gin.Engine) server {
  24. return &http.Server{
  25. Addr: address,
  26. Handler: router,
  27. ReadTimeout: 20 * time.Second,
  28. WriteTimeout: 20 * time.Second,
  29. MaxHeaderBytes: 1 << 20,
  30. }
  31. }