service_context.go
2.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
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/chat/api/internal/config"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/repository"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/open"
"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/middleware"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gorm.io/gorm"
)
type ServiceContext struct {
Config config.Config
LogRequest rest.Middleware
Redis *redis.Redis
DB *gorm.DB
ChatSessionRepository domain.ChatSessionRepository
ChatSessionRecordRepository domain.ChatSessionRecordRepository
ChatDocumentRepository domain.ChatDocumentRepository
ChatDatasetRepository domain.ChatDatasetRepository
ChatDatasetDocumentMappingRepository domain.ChatDatasetDocumentMappingRepository
SystemOpen open.SystemOpen
}
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"})
return &ServiceContext{
Config: c,
DB: db,
Redis: redis,
LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
ChatSessionRepository: repository.NewChatSessionRepository(cache.NewCachedRepository(mlCache)),
ChatSessionRecordRepository: repository.NewChatSessionRecordRepository(cache.NewCachedRepository(mlCache)),
ChatDocumentRepository: repository.NewChatDocumentRepository(cache.NewCachedRepository(mlCache)),
ChatDatasetRepository: repository.NewChatDatasetRepository(cache.NewCachedRepository(mlCache)),
ChatDatasetDocumentMappingRepository: repository.NewChatDatasetDocumentMappingRepository(cache.NewCachedRepository(mlCache)),
SystemOpen: open.NewSystemOpen(c.Redis),
}
}
func (svc *ServiceContext) DefaultDBConn() transaction.Conn {
return transaction.NewTransactionContext(svc.DB)
}