| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package gin_engine
- import (
- "app/commons/config"
- "app/commons/core"
- "app/commons/gin_engine/middleware"
- "fmt"
- "github.com/gin-gonic/gin"
- )
- // k8s - 健康检查http服务
- func RunK8sHttp() {
- engine := gin.New()
- engine.Use(middleware.Recovery()) // 全局错误恢复中间件
- engine.Use(middleware.Cors()) // 跨域 -- 放行所有
- k8sGroup(engine)
- address := fmt.Sprintf(":%d", config.AppConf().JobAddr)
- core.Log.Infof("Http 开启监听端口: %s", address)
- core.Log.Infof("Gin run : %+v", engine.Run(address))
- }
- // gin
- func NewHttpEngine() *gin.Engine {
- gin.SetMode(gin.ReleaseMode)
- engine := gin.New()
- engine.Use(gin.Logger())
- //engine.Use(middleware.GinLogger()) // 自定义日志处理 -- 自定义日志
- // 全局中间件
- engine.Use(middleware.Recovery()) // 全局错误恢复中间件
- engine.Use(middleware.Cors()) // 跨域 -- 放行所有
- k8sGroup(engine)
- return engine
- }
- func k8sGroup(engine *gin.Engine) {
- health := engine.Group("/health")
- {
- health.GET("/liveness", func(ctx *gin.Context) {
- ctx.JSON(200, gin.H{"status": "alive"})
- })
- health.GET("/readiness", func(ctx *gin.Context) {
- ctx.JSON(200, gin.H{"status": "ready"})
- })
- }
- }
- func RegisterRouter(group *gin.RouterGroup, iCore ContextInterface) {
- iCore.Register(group.Group(iCore.Route()))
- }
- type ContextInterface interface {
- Route() string
- Register(group *gin.RouterGroup)
- }
- type CommonHandler struct {
- }
- func (c CommonHandler) Route() string {
- panic("implement me")
- }
- func (CommonHandler) Register(group *gin.RouterGroup) {
- panic("implement me")
- }
|