bootstrap.go 741 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package utils
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "base-gin/utils/base"
  5. "base-gin/utils/middleware"
  6. "base-gin/utils/pprof"
  7. )
  8. type BootstrapConf struct {
  9. LogLevel string
  10. }
  11. func Bootstrap(router *gin.Engine, opts BootstrapConf) {
  12. //环境判断 env GIN_MODE=release/debug
  13. if base.GetJHYEnv() == base.ENV_PRODUCT {
  14. gin.SetMode(gin.ReleaseMode)
  15. }
  16. //日志配置 json格式,默认标准输出
  17. base.InitLog(opts.LogLevel)
  18. // trace init
  19. base.InitTracer()
  20. // 中间件
  21. router.Use(middleware.Tracer())
  22. router.Use(middleware.Logger())
  23. router.Use(gin.Recovery())
  24. //health
  25. router.HEAD("/health", func(c *gin.Context) {
  26. c.String(200, "succ")
  27. })
  28. //性能分析工具
  29. if base.IsPprofEnv() {
  30. pprof.Register(router)
  31. }
  32. }