123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package utils
- import (
- "errors"
- "go-admin/common/actions"
- "net/http"
- "github.com/go-admin-team/go-admin-core/sdk"
- "github.com/go-admin-team/go-admin-core/sdk/config"
- "github.com/go-admin-team/go-admin-core/sdk/service"
- v1 "github.com/prometheus/client_golang/api/prometheus/v1"
- "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
- "github.com/gin-gonic/gin"
- "github.com/gin-gonic/gin/binding"
- "gorm.io/gorm"
- "github.com/go-admin-team/go-admin-core/logger"
- "github.com/go-admin-team/go-admin-core/sdk/api"
- )
- type OtApi struct {
- api.Api
- OlapConn driver.Conn
- ChOrm *gorm.DB
- PgOrm *gorm.DB
- Prome v1.API
- }
- type OtService struct {
- service.Service
- OlapConn driver.Conn
- ChOrm *gorm.DB
- PgOrm *gorm.DB
- DataPermission actions.DataPermission
- Prome v1.API
- }
- func (e *OtApi) Init(c *gin.Context, req any, svc *OtService) error {
- if err := e.MakeContext(c).MakeDB().Errors; err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return err
- }
- if err := e.Bind(req).Errors; err != nil {
- e.Error(http.StatusBadRequest, err, err.Error())
- return err
- }
- if err := e.MakeService(svc).Errors; err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return err
- }
- svc.DataPermission = *actions.GetPermissionFromContext(c)
- return nil
- }
- func (e *OtApi) Handle() error {
- return nil
- }
- func (e *OtApi) MakeService(c *OtService) *OtApi {
- c.Log = e.Logger
- c.Orm = e.Orm
- if c.Orm != nil {
- c.Orm.WithContext(e.Context.Request.Context())
- }
- c.OlapConn = e.OlapConn
- c.ChOrm = e.ChOrm
- if c.ChOrm != nil {
- c.ChOrm.WithContext(e.Context.Request.Context())
- }
- c.PgOrm = e.PgOrm
- c.Prome = e.Prome
- return e
- }
- func (e *OtApi) MakeDB() *OtApi {
- e.MakeOrm()
- OlapConn, err := getOlapDriver(e.Context)
- if err != nil {
- e.Logger.Error(http.StatusInternalServerError, err, "olap数据库连接获取失败")
- e.AddError(err)
- }
- e.OlapConn = OlapConn
- // if e.ChOrm, err = getChOrm(e.Context); err != nil {
- // e.Logger.Error(http.StatusInternalServerError, err, "olap数据库ORM获取失败")
- // e.AddError(err)
- // }
- if e.Prome, err = getPrometheus(e.Context); err != nil {
- e.Logger.Error(http.StatusInternalServerError, err, "prometheus api获取失败")
- e.AddError(err)
- }
- dbMap := sdk.Runtime.GetDb()
- for key, db := range dbMap {
- if key == "pg" {
- e.PgOrm = db
- } else if key == "ck" {
- e.ChOrm = db
- }
- }
- return e
- }
- func (e *OtApi) AddError(err error) {
- e.Api.AddError(err)
- }
- // MakeContext 设置http上下文
- func (e *OtApi) MakeContext(c *gin.Context) *OtApi {
- e.Api.MakeContext(c)
- return e
- }
- // GetLogger 获取上下文提供的日志
- func (e OtApi) GetLogger() *logger.Helper {
- return e.Api.GetLogger()
- }
- // Bind 参数校验
- func (e *OtApi) Bind(d interface{}, bindings ...binding.Binding) *OtApi {
- e.Api.Bind(d, bindings...)
- return e
- }
- // GetOrm 获取Orm DB
- func (e OtApi) GetOrm() (*gorm.DB, error) {
- return e.Api.GetOrm()
- }
- // MakeOrm 设置Orm DB
- func (e *OtApi) MakeOrm() *OtApi {
- e.Api.MakeOrm()
- return e
- }
- // Error 通常错误数据处理
- func (e OtApi) Error(code int, err error, msg string) {
- if config.ApplicationConfig.Mode == "dev" {
- e.Api.Error(code, err, msg)
- } else {
- e.Api.Error(code, nil, "网络异常,请稍后重试")
- }
- }
- // OK 通常成功数据处理
- func (e OtApi) OK(data interface{}, msg string) {
- e.Api.OK(data, msg)
- }
- // PageOK 分页数据处理
- func (e OtApi) PageOK(result interface{}, count int, pageIndex int, pageSize int, msg string) {
- e.Api.PageOK(result, count, pageIndex, pageSize, msg)
- }
- // Custom 兼容函数
- func (e OtApi) Custom(data gin.H) {
- e.Api.Custom(data)
- }
- func (e OtApi) Translate(form, to interface{}) {
- e.Api.Translate(form, to)
- }
- // getAcceptLanguage 获取当前语言
- func (e *OtApi) getAcceptLanguage() string {
- return ""
- }
- func getOlapDriver(c *gin.Context) (driver.Conn, error) {
- ap, exist := c.Get("olapdb")
- if !exist {
- return nil, errors.New("olapdb connect not exist")
- }
- switch t := ap.(type) {
- case driver.Conn:
- //新增操作
- return t, nil
- default:
- return nil, errors.New("olap connect not exist")
- }
- }
- func getChOrm(c *gin.Context) (*gorm.DB, error) {
- orm, exist := c.Get("chorm")
- if !exist {
- return nil, errors.New("clickhouse orm 获取失败")
- }
- switch t := orm.(type) {
- case *gorm.DB:
- return t, nil
- default:
- return nil, errors.New("clickhouse orm 获取成功,但类型错误")
- }
- }
- func getPrometheus(c *gin.Context) (v1.API, error) {
- prome, exist := c.Get("prome")
- if !exist {
- return nil, errors.New("prometheus api 获取失败")
- }
- switch t := prome.(type) {
- case v1.API:
- return t, nil
- default:
- return nil, errors.New("prometheus api 获取成功,但类型错误")
- }
- }
|