service_context.go
3.6 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/tiptok/gocomm/pkg/cache/gzcache"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/config"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/middleware"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/repository"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/database"
"gorm.io/gorm"
)
type ServiceContext struct {
Config config.Config
DB *gorm.DB
Redis *redis.Redis
RedisCache gzcache.GZCache
ChartRepository domain.ChartRepository
ChartSettingRepository domain.ChartSettingRepository
AppPageRepository domain.AppPageRepository
ObjectNoticeRepository domain.ObjectNoticeRepository
ObjectTableRepository domain.ObjectTableRepository
ObjectFieldRepository domain.ObjectFieldRepository
ObjectTableDataRepository domain.ObjectTableDataRepository
ApiAuthService authlib.ApiAuthService
LoginStatusCheck rest.Middleware
LogRequest rest.Middleware
ByteMetadataService bytelib.ByteMetadataService
}
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)
redisCache := gzcache.NewClusterCache([]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,
RedisCache: redisCache,
Redis: redis,
ApiAuthService: apiAuth,
LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
ChartRepository: repository.NewChartRepository(cache.NewCachedRepository(mlCache)),
ChartSettingRepository: repository.NewChartSettingRepository(cache.NewCachedRepository(mlCache)),
AppPageRepository: repository.NewAppPageRepository(cache.NewCachedRepository(mlCache)),
ObjectNoticeRepository: repository.NewObjectNoticeRepository(cache.NewCachedRepository(mlCache)),
ObjectTableRepository: repository.NewObjectTableRepository(cache.NewCachedRepository(mlCache)),
ObjectFieldRepository: repository.NewObjectFieldRepository(cache.NewCachedRepository(mlCache)),
ObjectTableDataRepository: repository.NewObjectTableDataRepository(),
ByteMetadataService: bytelib.ByteMetadataService{
Service: gateway.NewService(c.ByteMetadata.Name, c.ByteMetadata.Host, c.ByteMetadata.Timeout),
},
}
}
func (svc *ServiceContext) DefaultDBConn() transaction.Conn {
return transaction.NewTransactionContext(svc.DB)
}