package routers import ( "fmt" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" _ "github.com/kptyun/KPTCOMM/docs" "github.com/kptyun/KPTCOMM/middleware/jwt" ginSwagger "github.com/swaggo/gin-swagger" // "github.com/kptyun/KPTCOMM/middleware/permission" "github.com/kptyun/KPTCOMM/pkg/setting" "github.com/kptyun/KPTCOMM/routers/api" "github.com/swaggo/gin-swagger/swaggerFiles" "net/http" "strings" ) func InitRouter() *gin.Engine { r := gin.New() // ginpprof.Wrap(r) r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string { // 你的自定义格式 return fmt.Sprintf(" %+v \n", param.Keys, ) })) r.Use(gin.Logger()) // 日志 r.Use(gzip.Gzip(gzip.DefaultCompression)) "github.com/gin-contrib/gzip" r.Use(Cors()) // 跨域请求rolemenus r.Use(gin.Recovery()) gin.SetMode(setting.ServerSetting.RunMode) r.Static("/static", setting.CurrentPath+"/dist/static") // 添加资源路径 r.StaticFile("/editor.worker.js", setting.CurrentPath+"/dist/editor.worker.js") //前端接口 r.StaticFile("/json.worker.js", setting.CurrentPath+"/dist/json.worker.js") //前端接口 r.StaticFile("/schema.json", setting.CurrentPath+"/dist/schema.json") //前端接口 r.Static("/test", setting.CurrentPath+"/dist/temp") // 添加资源路径 r.StaticFile("/", setting.CurrentPath+"/dist/index.html") //前端接口 r.StaticFile("/favicon.ico", setting.CurrentPath+"/dist/favicon.ico") //前端接口 r.Static("/file", setting.CurrentPath+"/uploads/file") r.Static("/uploads", setting.CurrentPath+"/uploads") r.POST("/auth", api.Auth) // 获取登录token r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释 r.POST("/regpasture", api.RegPasture) eqm := r.Group("/manage/api") eqm.POST("/login", api.Auth) apiV1 := r.Group("/authdata") apiV1.Use(jwt.JWT()) // token 验证 // apiV1.Use(permission.CasbinMiddleware()) // 权限 验证 { apiV1.POST("/rolemenus", api.GetRecuDataByName) apiV1.GET("/userinfo", api.GetUserinfo) apiV1.POST("/logout", api.UserLogout) apiV1.POST("/GetRecuDataByName", api.GetRecuDataByName) //// apiV1.POST("/GetDataByName", api.GetDataByName) apiV1.POST("/GetDataByNames", api.GetDataByNames) apiV1.POST("/PostDataByName", api.PostDataByName) apiV1.POST("/PostDataByNames", api.PostDataByNames) apiV1.POST("/UpdateDataRelation", api.UpdateDataRelation) //log.error apiV1.POST("/GetDataByNameForm", api.GetDataByNameForm) apiV1.POST("/PostDataByNameForm", api.PostDataByNameForm) apiV1.POST("/ExecDataByConfig", api.ExecDataByConfig) apiV1.POST("/ImportExcel", api.ImportExcel) apiV1.POST("/upload/:id/:name", api.UploadFile) apiV1.POST("/uploads", api.UploadFiles) apiV1.POST("/uploaderimage", api.UploaderImage) //apiV1.POST("/removeimage", api.RemovePicByName) //apiV1.GET("/getthumbnailimage/:filename", api.GetThumbnailImage) //apiV1.POST("/getoriginimage/:filename", api.GetOriginImage) //apiV1.GET("/getfileraw/:filename", api.GetFileRaw) //apiV1.GET("/downloadfile/:filename", api.DownloadFile) //apiV1.POST("/removefile/:id", api.RemoveFileByName) apiV1.POST("/testdata", api.TestData) } if setting.ServerSetting.NoAuth > 0 { apiV2 := r.Group("/data") // restful 接口 tablename 是 表名字 { apiV2.POST("/getdata", api.GetData) apiV2.POST("/GetDataByName", api.GetDataByName) apiV2.POST("/GetDataByNames", api.GetDataByNames) apiV2.POST("/PostDataByName", api.PostDataByName) apiV2.POST("/PostDataByNames", api.PostDataByNames) apiV2.POST("/ExecDataByConfig", api.ExecDataByConfig) apiV2.POST("/GetDataByNameForm", api.GetDataByNameForm) apiV2.POST("/PostDataByNameForm", api.PostDataByNameForm) apiV2.POST("/ImportExcel", api.ImportExcel) apiV2.POST("/upload", api.UploadFile) apiV2.POST("/uploads", api.UploadFiles) apiV2.POST("/uploaderimage", api.UploaderImage) //apiV2.POST("/removeimage", api.RemovePicByName) //apiV2.GET("/getthumbnailimage/:filename", api.GetThumbnailImage) //apiV2.GET("/getoriginimage/:filename", api.GetOriginImage) //apiV2.GET("/getfileraw/:filename", api.GetFileRaw) //apiV2.GET("/downloadfile/:filename", api.DownloadFile) //apiV2.POST("/removefile", api.RemoveFileByName) apiV2.POST("/testdata", api.TestData) } } return r } // 跨域 func Cors() gin.HandlerFunc { return func(c *gin.Context) { method := c.Request.Method //请求方法 origin := c.Request.Header.Get("Origin") //请求头部 var headerKeys []string // 声明请求头keys for k, _ := range c.Request.Header { headerKeys = append(headerKeys, k) } headerStr := strings.Join(headerKeys, ", ") if headerStr != "" { headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr) } else { headerStr = "access-control-allow-origin, access-control-allow-headers" } if origin != "" { c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域 c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求 // header的类型 c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, id, Token, name, optname, thumbnail, session, X_Requested_With, Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma") // 允许跨域设置 可以返回其他子段 c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma, FooBar") // 跨域关键设置 让浏览器可以解析 c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒 c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true c.Set("content-type", "application/json") // 设置返回格式是json } //放行所有OPTIONS方法 if method == "OPTIONS" { c.JSON(http.StatusOK, "Options Request!") } // 处理请求 c.Next() // 处理请求 } }