app-cloudep-tweeting-service/internal/svc/service_context.go

37 lines
946 B
Go
Raw Permalink Normal View History

2024-08-28 09:09:01 +00:00
package svc
2024-08-29 01:08:15 +00:00
import (
"app-cloudep-tweeting-service/internal/config"
model "app-cloudep-tweeting-service/internal/model/mongo"
"fmt"
2024-08-29 14:50:09 +00:00
vi "code.30cm.net/digimon/library-go/validator"
2024-08-29 01:08:15 +00:00
)
2024-08-28 09:09:01 +00:00
type ServiceContext struct {
2024-08-29 01:08:15 +00:00
Config config.Config
Validate vi.Validate
2024-08-29 13:26:01 +00:00
PostModel model.PostModel
PostLikeModel model.Post_likesModel
2024-08-28 09:09:01 +00:00
}
func NewServiceContext(c config.Config) *ServiceContext {
2024-08-29 01:08:15 +00:00
baseMongo := MustMongoConnectUrl(c)
2024-08-29 13:26:01 +00:00
postCollection := model.Post{}
postLikeCollection := model.PostLikes{}
2024-08-29 01:08:15 +00:00
2024-08-28 09:09:01 +00:00
return &ServiceContext{
2024-08-29 13:26:01 +00:00
Config: c,
Validate: vi.MustValidator(),
PostModel: model.NewPostModel(baseMongo, c.Mongo.Database, postCollection.CollectionName(), c.Cache),
PostLikeModel: model.NewPost_likesModel(baseMongo, c.Mongo.Database, postLikeCollection.CollectionName()),
2024-08-28 09:09:01 +00:00
}
}
2024-08-29 01:08:15 +00:00
func MustMongoConnectUrl(c config.Config) string {
return fmt.Sprintf(
"%s://%s:%s", c.Mongo.Schema,
c.Mongo.Host, c.Mongo.Port)
}