interface.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. #*****************************************************************************
  3. # @file interface.go
  4. # @author MakerYang(https://www.makeryang.com)
  5. # @statement 免费课程配套开源项目,任何形式收费均为盗版
  6. #*****************************************************************************
  7. */
  8. package Interface
  9. import (
  10. "Game/framework/config"
  11. "Game/framework/interface/ping"
  12. "context"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/gookit/color"
  16. "log"
  17. "net/http"
  18. "os"
  19. "os/signal"
  20. "time"
  21. )
  22. // 接口路由
  23. func router() *gin.Engine {
  24. router := gin.New()
  25. gin.SetMode(Config.Get.Service.Mode)
  26. // 健康检查接口
  27. router.GET("/ping", PingInterface.Ping)
  28. return router
  29. }
  30. // Init 接口初始化
  31. func Init() {
  32. routers := router()
  33. var HttpServer = &http.Server{
  34. Addr: fmt.Sprintf(":%d", Config.Get.Service.HttpPort),
  35. Handler: routers,
  36. ReadTimeout: Config.Get.Service.ReadTimeout,
  37. WriteTimeout: Config.Get.Service.WriteTimeout,
  38. MaxHeaderBytes: 1 << 20,
  39. }
  40. go func() {
  41. if err := HttpServer.ListenAndServe(); err != nil {
  42. }
  43. }()
  44. log.Println("[game]", color.Green.Text("server..."))
  45. quit := make(chan os.Signal)
  46. signal.Notify(quit, os.Interrupt)
  47. <-quit
  48. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  49. defer cancel()
  50. if err := HttpServer.Shutdown(ctx); err != nil {
  51. }
  52. }