interface.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. AccountInterface "Game/framework/interface/account"
  12. "Game/framework/interface/ping"
  13. "context"
  14. "fmt"
  15. "github.com/gin-gonic/gin"
  16. "github.com/gookit/color"
  17. "log"
  18. "net/http"
  19. "os"
  20. "os/signal"
  21. "time"
  22. )
  23. // 接口路由
  24. func router() *gin.Engine {
  25. router := gin.New()
  26. gin.SetMode(Config.Get.Service.Mode)
  27. // 健康检查接口
  28. router.GET("/ping", PingInterface.Ping)
  29. // 账户相关接口
  30. account := router.Group("account")
  31. {
  32. account.POST("/register", AccountInterface.Register)
  33. }
  34. return router
  35. }
  36. // Init 接口初始化
  37. func Init() {
  38. routers := router()
  39. var HttpServer = &http.Server{
  40. Addr: fmt.Sprintf(":%d", Config.Get.Service.HttpPort),
  41. Handler: routers,
  42. ReadTimeout: Config.Get.Service.ReadTimeout,
  43. WriteTimeout: Config.Get.Service.WriteTimeout,
  44. MaxHeaderBytes: 1 << 20,
  45. }
  46. go func() {
  47. if err := HttpServer.ListenAndServe(); err != nil {
  48. }
  49. }()
  50. log.Println("[game]", color.Green.Text("server..."))
  51. quit := make(chan os.Signal)
  52. signal.Notify(quit, os.Interrupt)
  53. <-quit
  54. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  55. defer cancel()
  56. if err := HttpServer.Shutdown(ctx); err != nil {
  57. }
  58. }