2026-02-28 15:29:16 +08:00
|
|
|
package svc
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-30 02:53:55 +00:00
|
|
|
"github.com/casbin/casbin/v2"
|
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
2026-02-28 15:29:16 +08:00
|
|
|
"github.com/zeromicro/go-zero/rest"
|
|
|
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
|
|
|
"muyu-apiserver/gateway/internal/config"
|
|
|
|
|
"muyu-apiserver/gateway/internal/middleware"
|
2026-03-30 02:53:55 +00:00
|
|
|
"muyu-apiserver/gateway/internal/repo"
|
|
|
|
|
pkgcasbin "muyu-apiserver/pkg/casbin"
|
|
|
|
|
"muyu-apiserver/pkg/tenantctx"
|
2026-02-28 15:29:16 +08:00
|
|
|
"muyu-apiserver/rpc/inventory/inventoryservice"
|
2026-07-04 02:29:54 +00:00
|
|
|
"muyu-apiserver/rpc/production/productionservice"
|
2026-06-15 09:20:56 +08:00
|
|
|
"muyu-apiserver/rpc/purchase/purchaseservice"
|
2026-02-28 15:29:16 +08:00
|
|
|
"muyu-apiserver/rpc/system/systemservice"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ServiceContext struct {
|
|
|
|
|
Config config.Config
|
|
|
|
|
AuthorityMiddleware rest.Middleware
|
2026-03-30 02:53:55 +00:00
|
|
|
CasbinEnforcer *casbin.Enforcer
|
|
|
|
|
CrmRepo *repo.CrmRepo
|
2026-02-28 15:29:16 +08:00
|
|
|
SystemRpc systemservice.SystemService
|
|
|
|
|
InventoryRpc inventoryservice.InventoryService
|
2026-06-15 09:20:56 +08:00
|
|
|
PurchaseRpc purchaseservice.PurchaseService
|
2026-07-04 02:29:54 +00:00
|
|
|
ProductionRpc productionservice.ProductionService
|
2026-02-28 15:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
2026-03-30 02:53:55 +00:00
|
|
|
mysqlConn := sqlx.NewMysql(c.DataSource)
|
|
|
|
|
|
|
|
|
|
var pgConn sqlx.SqlConn
|
|
|
|
|
if c.PostgresDataSource != "" {
|
|
|
|
|
pgConn = sqlx.NewSqlConn("postgres", c.PostgresDataSource)
|
|
|
|
|
} else {
|
|
|
|
|
pgConn = mysqlConn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var enforcer *casbin.Enforcer
|
|
|
|
|
if c.DataSource != "" {
|
|
|
|
|
func() {
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
logx.Errorf("init casbin enforcer panic: %v (skipped, running without enforcer)", r)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
e, err := pkgcasbin.NewEnforcer(c.DataSource)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Errorf("init casbin enforcer failed: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
enforcer = e
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 15:29:16 +08:00
|
|
|
return &ServiceContext{
|
|
|
|
|
Config: c,
|
2026-03-30 02:53:55 +00:00
|
|
|
AuthorityMiddleware: middleware.NewAuthorityMiddleware(enforcer).Handle,
|
|
|
|
|
CasbinEnforcer: enforcer,
|
|
|
|
|
CrmRepo: repo.NewCrmRepo(pgConn),
|
|
|
|
|
SystemRpc: systemservice.NewSystemService(zrpc.MustNewClient(c.SystemRpc, zrpc.WithUnaryClientInterceptor(tenantctx.UnaryClientInterceptor()))),
|
|
|
|
|
InventoryRpc: inventoryservice.NewInventoryService(zrpc.MustNewClient(c.InventoryRpc, zrpc.WithUnaryClientInterceptor(tenantctx.UnaryClientInterceptor()))),
|
2026-06-15 09:20:56 +08:00
|
|
|
PurchaseRpc: purchaseservice.NewPurchaseService(zrpc.MustNewClient(c.PurchaseRpc, zrpc.WithUnaryClientInterceptor(tenantctx.UnaryClientInterceptor()))),
|
2026-07-04 02:29:54 +00:00
|
|
|
ProductionRpc: productionservice.NewProductionService(zrpc.MustNewClient(c.ProductionRpc, zrpc.WithUnaryClientInterceptor(tenantctx.UnaryClientInterceptor()))),
|
2026-02-28 15:29:16 +08:00
|
|
|
}
|
|
|
|
|
}
|