controller.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. ******************************************************************************
  3. * @file controller.go
  4. * @author MakerYang
  5. ******************************************************************************
  6. */
  7. package Controller
  8. import (
  9. "Game/framework/config"
  10. "Game/framework/controller/ping"
  11. "context"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "github.com/gookit/color"
  15. "log"
  16. "net/http"
  17. "os"
  18. "os/signal"
  19. "time"
  20. )
  21. func router() *gin.Engine {
  22. router := gin.New()
  23. gin.SetMode(Config.Get.Service.Mode)
  24. router.GET("/ping", PingController.Ping)
  25. return router
  26. }
  27. func Init() {
  28. routers := router()
  29. var HttpServer = &http.Server{
  30. Addr: fmt.Sprintf(":%d", Config.Get.Service.HttpPort),
  31. Handler: routers,
  32. ReadTimeout: Config.Get.Service.ReadTimeout,
  33. WriteTimeout: Config.Get.Service.WriteTimeout,
  34. MaxHeaderBytes: 1 << 20,
  35. }
  36. go func() {
  37. if err := HttpServer.ListenAndServe(); err != nil {
  38. }
  39. }()
  40. log.Println("[game]", color.Green.Text("server..."))
  41. quit := make(chan os.Signal)
  42. signal.Notify(quit, os.Interrupt)
  43. <-quit
  44. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  45. defer cancel()
  46. if err := HttpServer.Shutdown(ctx); err != nil {
  47. }
  48. }