sdk.go 4.2 KB

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