sdk.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package utils
  2. import (
  3. "errors"
  4. "go-admin/common/actions"
  5. "net/http"
  6. "github.com/go-admin-team/go-admin-core/sdk/config"
  7. "github.com/go-admin-team/go-admin-core/sdk/service"
  8. v1 "github.com/prometheus/client_golang/api/prometheus/v1"
  9. "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
  10. "github.com/gin-gonic/gin"
  11. "github.com/gin-gonic/gin/binding"
  12. "gorm.io/gorm"
  13. "github.com/go-admin-team/go-admin-core/logger"
  14. "github.com/go-admin-team/go-admin-core/sdk/api"
  15. )
  16. type OtApi struct {
  17. api.Api
  18. OlapConn driver.Conn
  19. ChOrm *gorm.DB
  20. Prome v1.API
  21. }
  22. type OtService struct {
  23. service.Service
  24. OlapConn driver.Conn
  25. ChOrm *gorm.DB
  26. DataPermission actions.DataPermission
  27. Prome v1.API
  28. }
  29. func (e *OtApi) Init(c *gin.Context, req any, svc *OtService) error {
  30. if err := e.MakeContext(c).MakeDB().Errors; err != nil {
  31. e.Error(http.StatusInternalServerError, err, err.Error())
  32. return err
  33. }
  34. if err := e.Bind(req).Errors; err != nil {
  35. e.Error(http.StatusBadRequest, err, err.Error())
  36. return err
  37. }
  38. if err := e.MakeService(svc).Errors; err != nil {
  39. e.Error(http.StatusInternalServerError, err, err.Error())
  40. return err
  41. }
  42. svc.DataPermission = *actions.GetPermissionFromContext(c)
  43. return nil
  44. }
  45. func (e *OtApi) Handle() error {
  46. return nil
  47. }
  48. func (e *OtApi) MakeService(c *OtService) *OtApi {
  49. c.Log = e.Logger
  50. c.Orm = e.Orm
  51. if c.Orm != nil {
  52. c.Orm.WithContext(e.Context.Request.Context())
  53. }
  54. c.OlapConn = e.OlapConn
  55. c.ChOrm = e.ChOrm
  56. if c.ChOrm != nil {
  57. c.ChOrm.WithContext(e.Context.Request.Context())
  58. }
  59. c.Prome = e.Prome
  60. return e
  61. }
  62. func (e *OtApi) MakeDB() *OtApi {
  63. e.MakeOrm()
  64. OlapConn, err := getOlapDriver(e.Context)
  65. if err != nil {
  66. e.Logger.Error(http.StatusInternalServerError, err, "olap数据库连接获取失败")
  67. e.AddError(err)
  68. }
  69. e.OlapConn = OlapConn
  70. if e.ChOrm, err = getChOrm(e.Context); err != nil {
  71. e.Logger.Error(http.StatusInternalServerError, err, "olap数据库ORM获取失败")
  72. e.AddError(err)
  73. }
  74. if e.Prome, err = getPrometheus(e.Context); err != nil {
  75. e.Logger.Error(http.StatusInternalServerError, err, "prometheus api获取失败")
  76. e.AddError(err)
  77. }
  78. return e
  79. }
  80. func (e *OtApi) AddError(err error) {
  81. e.Api.AddError(err)
  82. }
  83. // MakeContext 设置http上下文
  84. func (e *OtApi) MakeContext(c *gin.Context) *OtApi {
  85. e.Api.MakeContext(c)
  86. return e
  87. }
  88. // GetLogger 获取上下文提供的日志
  89. func (e OtApi) GetLogger() *logger.Helper {
  90. return e.Api.GetLogger()
  91. }
  92. // Bind 参数校验
  93. func (e *OtApi) Bind(d interface{}, bindings ...binding.Binding) *OtApi {
  94. e.Api.Bind(d, bindings...)
  95. return e
  96. }
  97. // GetOrm 获取Orm DB
  98. func (e OtApi) GetOrm() (*gorm.DB, error) {
  99. return e.Api.GetOrm()
  100. }
  101. // MakeOrm 设置Orm DB
  102. func (e *OtApi) MakeOrm() *OtApi {
  103. e.Api.MakeOrm()
  104. return e
  105. }
  106. // Error 通常错误数据处理
  107. func (e OtApi) Error(code int, err error, msg string) {
  108. if config.ApplicationConfig.Mode == "dev" {
  109. e.Api.Error(code, err, msg)
  110. } else {
  111. e.Api.Error(code, nil, "网络异常,请稍后重试")
  112. }
  113. }
  114. // OK 通常成功数据处理
  115. func (e OtApi) OK(data interface{}, msg string) {
  116. e.Api.OK(data, msg)
  117. }
  118. // PageOK 分页数据处理
  119. func (e OtApi) PageOK(result interface{}, count int, pageIndex int, pageSize int, msg string) {
  120. e.Api.PageOK(result, count, pageIndex, pageSize, msg)
  121. }
  122. // Custom 兼容函数
  123. func (e OtApi) Custom(data gin.H) {
  124. e.Api.Custom(data)
  125. }
  126. func (e OtApi) Translate(form, to interface{}) {
  127. e.Api.Translate(form, to)
  128. }
  129. // getAcceptLanguage 获取当前语言
  130. func (e *OtApi) getAcceptLanguage() string {
  131. return ""
  132. }
  133. func getOlapDriver(c *gin.Context) (driver.Conn, error) {
  134. ap, exist := c.Get("olapdb")
  135. if !exist {
  136. return nil, errors.New("olapdb connect not exist")
  137. }
  138. switch t := ap.(type) {
  139. case driver.Conn:
  140. //新增操作
  141. return t, nil
  142. default:
  143. return nil, errors.New("olap connect not exist")
  144. }
  145. }
  146. func getChOrm(c *gin.Context) (*gorm.DB, error) {
  147. orm, exist := c.Get("chorm")
  148. if !exist {
  149. return nil, errors.New("clickhouse orm 获取失败")
  150. }
  151. switch t := orm.(type) {
  152. case *gorm.DB:
  153. return t, nil
  154. default:
  155. return nil, errors.New("clickhouse orm 获取成功,但类型错误")
  156. }
  157. }
  158. func getPrometheus(c *gin.Context) (v1.API, error) {
  159. prome, exist := c.Get("prome")
  160. if !exist {
  161. return nil, errors.New("prometheus api 获取失败")
  162. }
  163. switch t := prome.(type) {
  164. case v1.API:
  165. return t, nil
  166. default:
  167. return nil, errors.New("prometheus api 获取成功,但类型错误")
  168. }
  169. }