server.go 810 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. func RunServer() {
  15. // 初始化redis服务
  16. initialize.Redis()
  17. // 从redis加载jwt数据
  18. // 启动服务
  19. Router := initialize.Routers()
  20. address := fmt.Sprintf(":%d", global.M2A_CONFIG.System.Addr)
  21. s := initServer(address, Router)
  22. global.M2A_LOG.Info("server run success on ", zap.String("address", address))
  23. global.M2A_LOG.Error(s.ListenAndServe().Error())
  24. }
  25. func initServer(address string, router *gin.Engine) server {
  26. return &http.Server{
  27. Addr: address,
  28. Handler: router,
  29. ReadTimeout: 20 * time.Second,
  30. WriteTimeout: 20 * time.Second,
  31. MaxHeaderBytes: 1 << 20,
  32. }
  33. }