Files
mir-godot/server/framework/interface/interface.go

68 lines
1.3 KiB
Go
Raw Normal View History

2024-03-03 22:59:18 +08:00
/**
2024-03-06 10:47:22 +08:00
#*****************************************************************************
# @file interface.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目任何形式收费均为盗版
#*****************************************************************************
*/
2024-03-03 22:59:18 +08:00
2024-03-06 10:47:22 +08:00
package Interface
2024-03-03 22:59:18 +08:00
import (
"Game/framework/config"
2024-03-06 10:47:22 +08:00
"Game/framework/interface/ping"
2024-03-03 22:59:18 +08:00
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gookit/color"
"log"
"net/http"
"os"
"os/signal"
"time"
)
2024-03-06 10:47:22 +08:00
// 接口路由
2024-03-03 22:59:18 +08:00
func router() *gin.Engine {
2024-03-06 10:47:22 +08:00
2024-03-03 22:59:18 +08:00
router := gin.New()
gin.SetMode(Config.Get.Service.Mode)
2024-03-06 10:47:22 +08:00
// 健康检查接口
router.GET("/ping", PingInterface.Ping)
2024-03-03 22:59:18 +08:00
return router
}
2024-03-06 10:47:22 +08:00
// Init 接口初始化
2024-03-03 22:59:18 +08:00
func Init() {
2024-03-06 10:47:22 +08:00
2024-03-03 22:59:18 +08:00
routers := router()
var HttpServer = &http.Server{
Addr: fmt.Sprintf(":%d", Config.Get.Service.HttpPort),
Handler: routers,
ReadTimeout: Config.Get.Service.ReadTimeout,
WriteTimeout: Config.Get.Service.WriteTimeout,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := HttpServer.ListenAndServe(); err != nil {
}
}()
log.Println("[game]", color.Green.Text("server..."))
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := HttpServer.Shutdown(ctx); err != nil {
}
}