init.go 698 B

1234567891011121314151617181920212223242526272829
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/gin-contrib/gzip"
  5. "github.com/gin-gonic/gin"
  6. "icloudapp.cn/tools/middleware"
  7. "net/http"
  8. )
  9. func InitRouter() *gin.Engine {
  10. r := gin.Default()
  11. // 上传大小限制 100M
  12. r.MaxMultipartMemory = 100 << 20
  13. // 启用gzip压缩
  14. r.Use(gzip.Gzip(gzip.BestCompression))
  15. r.Use(middleware.LoggerMiddleware())
  16. // 错误恢复
  17. r.Use(gin.CustomRecovery(func(ctx *gin.Context, recovered interface{}) {
  18. if err, ok := recovered.(string); ok {
  19. ctx.String(http.StatusInternalServerError, fmt.Sprintf("error occuring: %s", err))
  20. }
  21. ctx.Abort()
  22. //c.AbortWithStatus(http.StatusInternalServerError)
  23. }))
  24. SetupApiRouters(r)
  25. SetupPHPRouter(r)
  26. return r
  27. }