service_context.go
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package svc
import (
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/config"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/db/repository"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/database"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/gateway"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/middleware"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gorm.io/gorm"
"time"
)
type ServiceContext struct {
Config config.Config
LoginStatusCheck rest.Middleware
LogRequest rest.Middleware
Redis *redis.Redis
DB *gorm.DB
// 外部接口
SmsService smslib.SMSService
// 数据仓储
UserRepository domain.SysUserRepository
CompanyRepository domain.SysCompanyRepository
DepartmentRepository domain.SysDepartmentRepository
EmployeeRepository domain.SysEmployeeRepository
RoleRepository domain.SysRoleRepository
GroupRepository domain.SysGroupRepository
AppRepository domain.SysAppRepository
CompanyAppRepository domain.SysCompanyAppRepository
UserDepartmentRepository domain.SysUserDepartmentRepository
}
func NewServiceContext(c config.Config) *ServiceContext {
db := database.OpenGormPGDB(c.DB.DataSource, c.Log.Mode)
mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass)
redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})
//apiAuth := authlib.ApiAuthService{
// Service: gateway.NewService(c.ApiAuth.Name, c.ApiAuth.Host, c.ApiAuth.Timeout),
//}
return &ServiceContext{
Config: c,
DB: db,
Redis: redis,
SmsService: smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)},
//LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware().Handle,
LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
UserRepository: repository.NewSysUserRepository(cache.NewCachedRepository(mlCache)),
CompanyRepository: repository.NewSysCompanyRepository(cache.NewCachedRepository(mlCache)),
DepartmentRepository: repository.NewSysDepartmentRepository(cache.NewCachedRepository(mlCache)),
EmployeeRepository: repository.NewSysEmployeeRepository(cache.NewCachedRepository(mlCache)),
RoleRepository: repository.NewSysRoleRepository(cache.NewCachedRepository(mlCache)),
GroupRepository: repository.NewSysGroupRepository(cache.NewCachedRepository(mlCache)),
AppRepository: repository.NewSysAppRepository(cache.NewCachedRepository(mlCache)),
UserDepartmentRepository: repository.NewSysUserDepartmentRepository(cache.NewCachedRepository(mlCache)),
CompanyAppRepository: repository.NewSysCompanyAppRepository(cache.NewCachedRepository(mlCache)),
}
}
func (svc *ServiceContext) DefaultDBConn() transaction.Conn {
return transaction.NewTransactionContext(svc.DB)
}