sdk.go 4.6 KB

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