feat(foundation):构建基本的路由创建结构。

This commit is contained in:
徐涛
2022-08-09 15:22:25 +08:00
parent 35dc66f935
commit d614bcc8e1
3 changed files with 66 additions and 6 deletions

35
router/router.go Normal file
View File

@@ -0,0 +1,35 @@
package router
import (
"electricity_bill_calc/response"
"log"
"runtime/debug"
"github.com/gin-gonic/gin"
)
func Router() *gin.Engine {
router := gin.Default()
router.Use(Recover)
return router
}
//404
func HandleNotFound(c *gin.Context) {
response.NewResult(c).Error(404, "资源未找到")
}
//500
func Recover(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
//打印错误堆栈信息
log.Printf("panic: %v\n", r)
debug.PrintStack()
response.NewResult(c).Error(500, "服务器内部错误")
}
}()
//继续后续接口调用
c.Next()
}