d5eb2402f61a8ffa559f8f914daef7e383dde079.svn-base 6.4 KB

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