service_context.go 4.2 KB
package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/rest"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/middleware"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/repository"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database"
	"gorm.io/gorm"
)

type ServiceContext struct {
	Config config.Config
	DB     *gorm.DB
	Redis  *redis.Redis

	ArticleBackupRepository  domain.ArticleBackupRepository
	ArticleCommentRepository domain.ArticleCommentRepository
	ArticleDraftRepository   domain.ArticleDraftRepository
	ArticleRepository        domain.ArticleRepository
	ArticleSectionRepository domain.ArticleSectionRepository
	ArticleTagRepository     domain.ArticleTagRepository
	ArticleAndTagRepository  domain.ArticleAndTagRepository

	CompanyRepository         domain.CompanyRepository
	DepartmentRepository      domain.DepartmentRepository
	MessageBusinessRepository domain.MessageBusinessRepository
	MessageSystemRepository   domain.MessageSystemRepository
	RoleRepository            domain.RoleRepository
	UserFollowRepository      domain.UserFollowRepository
	UserLoveFlagRepository    domain.UserLoveFlagRepository
	UserReadArticleRepository domain.UserReadArticleRepository
	UserRepository            domain.UserRepository

	ApiAuthService authlib.ApiAuthService

	LoginStatusCheck rest.Middleware
}

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,
		ApiAuthService:   apiAuth,
		LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,

		ArticleBackupRepository:  repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
		ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
		ArticleDraftRepository:   repository.NewArticleDraftRepository(cache.NewCachedRepository(mlCache)),
		ArticleRepository:        repository.NewArticleRepository(cache.NewCachedRepository(mlCache)),
		ArticleSectionRepository: repository.NewArticleSectionRepository(cache.NewCachedRepository(mlCache)),
		ArticleAndTagRepository:  repository.NewArticleAndTagRepository(cache.NewCachedRepository(mlCache)),

		CompanyRepository:         repository.NewCompanyRepository(cache.NewCachedRepository(mlCache)),
		DepartmentRepository:      repository.NewDepartmentRepository(cache.NewCachedRepository(mlCache)),
		MessageBusinessRepository: repository.NewMessageBusinessRepository(cache.NewCachedRepository(mlCache)),
		MessageSystemRepository:   repository.NewMessageSystemRepository(cache.NewCachedRepository(mlCache)),
		RoleRepository:            repository.NewRoleRepository(cache.NewCachedRepository(mlCache)),
		UserFollowRepository:      repository.NewUserFollowRepository(cache.NewCachedRepository(mlCache)),
		UserLoveFlagRepository:    repository.NewUserLoveFlagRepository(cache.NewCachedRepository(mlCache)),
		UserRepository:            repository.NewUserRepository(cache.NewCachedRepository(mlCache)),
		UserReadArticleRepository: repository.NewUserReadArticleRepository(cache.NewCachedRepository(mlCache)),
		ArticleTagRepository:      repository.NewArticleTagRepository(cache.NewCachedRepository(mlCache)),
	}
}

func (svc *ServiceContext) DefaultDBConn() transaction.Conn {
	return transaction.NewTransactionContext(svc.DB)
}