init
This commit is contained in:
43
router/base.go-
Normal file
43
router/base.go-
Normal file
@@ -0,0 +1,43 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"energy-management-system/utils/exception"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ExceptionResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg interface{} `json:"msg"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
type Exception struct {
|
||||
HttpCode int `json:"-"`
|
||||
Code int `json:"code"`
|
||||
Msg interface{} `json:"msg"`
|
||||
Err error `json:"err"`
|
||||
}
|
||||
|
||||
func Panic(c *gin.Context, e *Exception) {
|
||||
c.JSON(e.HttpCode, ExceptionResponse{e.Code, e.Msg, GetReqPath(c)})
|
||||
}
|
||||
func Unknow(c *gin.Context) {
|
||||
c.JSON(http.StatusForbidden, ExceptionResponse{exception.UNKNOW_ERROR, "未知错误", GetReqPath(c)})
|
||||
}
|
||||
func Server(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, ExceptionResponse{exception.SERVER_ERROR, "服务器错误", GetReqPath(c)})
|
||||
}
|
||||
|
||||
func NotFoundR(c *gin.Context) {
|
||||
c.JSON(http.StatusForbidden, ExceptionResponse{exception.NOT_FOUND_ROUTE, "Not Found Route", GetReqPath(c)})
|
||||
}
|
||||
|
||||
func NotFoundM(c *gin.Context) {
|
||||
c.JSON(http.StatusForbidden, ExceptionResponse{exception.NOT_FOUND_METH, "Not Found Method", GetReqPath(c)})
|
||||
}
|
||||
|
||||
// GetReqPath 获取请求路径
|
||||
func GetReqPath(c *gin.Context) string {
|
||||
return c.Request.Method + " " + c.Request.URL.String()
|
||||
}
|
||||
2
router/recovery.go-
Normal file
2
router/recovery.go-
Normal file
@@ -0,0 +1,2 @@
|
||||
package router
|
||||
|
||||
40
router/router.go
Normal file
40
router/router.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
v1 "energy-management-system/api/v1"
|
||||
"energy-management-system/middleware"
|
||||
"energy-management-system/router/routes"
|
||||
"energy-management-system/utils/exception"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func InitRouter() *gin.Engine {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
r := gin.Default()
|
||||
r.NoRoute(exception.NotFoundR)
|
||||
r.NoMethod(exception.NotFoundM)
|
||||
r.Use(middleware.Cors(), middleware.Recovery())
|
||||
api := r.Group("api")
|
||||
{
|
||||
controllersV1 := new(v1.Controller)
|
||||
apiV1 := api.Group("v1")
|
||||
{
|
||||
apiV1.GET("test/test", controllersV1.TestApi.Test)
|
||||
apiV1.GET("test/testData", controllersV1.TestApi.TestData)
|
||||
}
|
||||
//// 需要权限校验
|
||||
//apiV1.Use(middleware.JWTAuth())
|
||||
//{
|
||||
// apiV1.GET("user/test_d", controllersV1.TestD)
|
||||
//}
|
||||
|
||||
// 需要权限校验
|
||||
//apiV1.Use(middleware.CasbinRBAC())
|
||||
|
||||
{
|
||||
routes.GroupRoutes.PeakValley.InitPeakValley(apiV1)
|
||||
}
|
||||
|
||||
}
|
||||
return r
|
||||
}
|
||||
7
router/routes/enter.go
Normal file
7
router/routes/enter.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package routes
|
||||
|
||||
type groupRoutes struct {
|
||||
PeakValley
|
||||
}
|
||||
|
||||
var GroupRoutes = new(groupRoutes)
|
||||
38
router/routes/peak_valley.go
Normal file
38
router/routes/peak_valley.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
v1 "energy-management-system/api/v1"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PeakValley struct{}
|
||||
|
||||
func (r *PeakValley) InitPeakValley(Router *gin.RouterGroup) {
|
||||
peakValleyApi := Router.Group("peakValley")
|
||||
{
|
||||
|
||||
peakValleyApi.GET("types", v1.Controllers.PeakValley.GetPeakValleyTypes)
|
||||
peakValleyApi.GET("ruleList", v1.Controllers.PeakValley.PeakValleyRuleList)
|
||||
peakValleyApi.POST("createRule", v1.Controllers.PeakValley.CreatePeakValleyRule)
|
||||
peakValleyApi.PUT("updateRule", v1.Controllers.PeakValley.UpdatePeakValleyRule)
|
||||
peakValleyApi.GET("ruleDetail/:id", v1.Controllers.PeakValley.PeakValleyRuleDetail)
|
||||
peakValleyApi.GET("ruleEditDetail/:id", v1.Controllers.PeakValley.PeakValleyRuleEditDetail)
|
||||
|
||||
//userapi.DELETE(":id", api.Controllers.UserApi.DeleteUser)
|
||||
|
||||
//// 个人信息管理
|
||||
//userapi.GET("detail", api.Controllers.UserApi.GetUserDetail)
|
||||
//userapi.PUT("update", api.Controllers.UserApi.UpdateUsers)
|
||||
//userapi.GET("logout", api.Controllers.UserApi.Logout)
|
||||
//userapi.GET("refresh", api.Controllers.UserApi.RefreshToken)
|
||||
//
|
||||
//// 用户管理
|
||||
//userapi.GET("", api.Controllers.UserApi.GetUserListByPage)
|
||||
//userapi.POST("", api.Controllers.UserApi.CreateUser)
|
||||
//userapi.PUT("", api.Controllers.UserApi.UpdateUser)
|
||||
//userapi.DELETE(":id", api.Controllers.UserApi.DeleteUser)
|
||||
//userapi.GET(":id", api.Controllers.UserApi.GetUser)
|
||||
//userapi.POST("transform", api.Controllers.UserApi.TransformUser)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user