router.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package routers
  2. import (
  3. "fmt"
  4. "github.com/gin-contrib/gzip"
  5. "github.com/gin-gonic/gin"
  6. _ "github.com/kptyun/KPTCOMM/docs"
  7. "github.com/kptyun/KPTCOMM/middleware/jwt"
  8. ginSwagger "github.com/swaggo/gin-swagger"
  9. // "github.com/kptyun/KPTCOMM/middleware/permission"
  10. "github.com/kptyun/KPTCOMM/pkg/setting"
  11. "github.com/kptyun/KPTCOMM/routers/api"
  12. "github.com/swaggo/gin-swagger/swaggerFiles"
  13. "net/http"
  14. "strings"
  15. )
  16. func InitRouter() *gin.Engine {
  17. r := gin.New()
  18. // ginpprof.Wrap(r)
  19. r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  20. // 你的自定义格式
  21. return fmt.Sprintf(" %+v \n",
  22. param.Keys,
  23. )
  24. }))
  25. r.Use(gin.Logger()) // 日志
  26. r.Use(gzip.Gzip(gzip.DefaultCompression)) "github.com/gin-contrib/gzip"
  27. r.Use(Cors()) // 跨域请求rolemenus
  28. r.Use(gin.Recovery())
  29. gin.SetMode(setting.ServerSetting.RunMode)
  30. r.Static("/static", setting.CurrentPath+"/dist/static") // 添加资源路径
  31. r.StaticFile("/editor.worker.js", setting.CurrentPath+"/dist/editor.worker.js") //前端接口
  32. r.StaticFile("/json.worker.js", setting.CurrentPath+"/dist/json.worker.js") //前端接口
  33. r.StaticFile("/schema.json", setting.CurrentPath+"/dist/schema.json") //前端接口
  34. r.Static("/test", setting.CurrentPath+"/dist/temp") // 添加资源路径
  35. r.StaticFile("/", setting.CurrentPath+"/dist/index.html") //前端接口
  36. r.StaticFile("/favicon.ico", setting.CurrentPath+"/dist/favicon.ico") //前端接口
  37. r.Static("/file", setting.CurrentPath+"/uploads/file")
  38. r.Static("/uploads", setting.CurrentPath+"/uploads")
  39. r.POST("/auth", api.Auth) // 获取登录token
  40. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释
  41. r.POST("/regpasture", api.RegPasture)
  42. eqm := r.Group("/manage/api")
  43. eqm.POST("/login", api.Auth)
  44. apiV1 := r.Group("/authdata")
  45. apiV1.Use(jwt.JWT()) // token 验证
  46. // apiV1.Use(permission.CasbinMiddleware()) // 权限 验证
  47. {
  48. apiV1.POST("/rolemenus", api.GetRecuDataByName)
  49. apiV1.GET("/userinfo", api.GetUserinfo)
  50. apiV1.POST("/logout", api.UserLogout)
  51. apiV1.POST("/GetRecuDataByName", api.GetRecuDataByName) ////
  52. apiV1.POST("/GetDataByName", api.GetDataByName)
  53. apiV1.POST("/GetDataByNames", api.GetDataByNames)
  54. apiV1.POST("/PostDataByName", api.PostDataByName)
  55. apiV1.POST("/PostDataByNames", api.PostDataByNames)
  56. apiV1.POST("/UpdateDataRelation", api.UpdateDataRelation) //log.error
  57. apiV1.POST("/GetDataByNameForm", api.GetDataByNameForm)
  58. apiV1.POST("/PostDataByNameForm", api.PostDataByNameForm)
  59. apiV1.POST("/ExecDataByConfig", api.ExecDataByConfig)
  60. apiV1.POST("/ImportExcel", api.ImportExcel)
  61. apiV1.POST("/upload/:id/:name", api.UploadFile)
  62. apiV1.POST("/uploads", api.UploadFiles)
  63. apiV1.POST("/uploaderimage", api.UploaderImage)
  64. //apiV1.POST("/removeimage", api.RemovePicByName)
  65. //apiV1.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  66. //apiV1.POST("/getoriginimage/:filename", api.GetOriginImage)
  67. //apiV1.GET("/getfileraw/:filename", api.GetFileRaw)
  68. //apiV1.GET("/downloadfile/:filename", api.DownloadFile)
  69. //apiV1.POST("/removefile/:id", api.RemoveFileByName)
  70. apiV1.POST("/testdata", api.TestData)
  71. }
  72. if setting.ServerSetting.NoAuth > 0 {
  73. apiV2 := r.Group("/data") // restful 接口 tablename 是 表名字
  74. {
  75. apiV2.POST("/getdata", api.GetData)
  76. apiV2.POST("/GetDataByName", api.GetDataByName)
  77. apiV2.POST("/GetDataByNames", api.GetDataByNames)
  78. apiV2.POST("/PostDataByName", api.PostDataByName)
  79. apiV2.POST("/PostDataByNames", api.PostDataByNames)
  80. apiV2.POST("/ExecDataByConfig", api.ExecDataByConfig)
  81. apiV2.POST("/GetDataByNameForm", api.GetDataByNameForm)
  82. apiV2.POST("/PostDataByNameForm", api.PostDataByNameForm)
  83. apiV2.POST("/ImportExcel", api.ImportExcel)
  84. apiV2.POST("/upload", api.UploadFile)
  85. apiV2.POST("/uploads", api.UploadFiles)
  86. apiV2.POST("/uploaderimage", api.UploaderImage)
  87. //apiV2.POST("/removeimage", api.RemovePicByName)
  88. //apiV2.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  89. //apiV2.GET("/getoriginimage/:filename", api.GetOriginImage)
  90. //apiV2.GET("/getfileraw/:filename", api.GetFileRaw)
  91. //apiV2.GET("/downloadfile/:filename", api.DownloadFile)
  92. //apiV2.POST("/removefile", api.RemoveFileByName)
  93. apiV2.POST("/testdata", api.TestData)
  94. }
  95. }
  96. return r
  97. }
  98. // 跨域
  99. func Cors() gin.HandlerFunc {
  100. return func(c *gin.Context) {
  101. method := c.Request.Method //请求方法
  102. origin := c.Request.Header.Get("Origin") //请求头部
  103. var headerKeys []string // 声明请求头keys
  104. for k, _ := range c.Request.Header {
  105. headerKeys = append(headerKeys, k)
  106. }
  107. headerStr := strings.Join(headerKeys, ", ")
  108. if headerStr != "" {
  109. headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
  110. } else {
  111. headerStr = "access-control-allow-origin, access-control-allow-headers"
  112. }
  113. if origin != "" {
  114. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  115. c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
  116. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
  117. // header的类型
  118. 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")
  119. // 允许跨域设置 可以返回其他子段
  120. 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") // 跨域关键设置 让浏览器可以解析
  121. c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
  122. c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
  123. c.Set("content-type", "application/json") // 设置返回格式是json
  124. }
  125. //放行所有OPTIONS方法
  126. if method == "OPTIONS" {
  127. c.JSON(http.StatusOK, "Options Request!")
  128. }
  129. // 处理请求
  130. c.Next() // 处理请求
  131. }
  132. }