Skip to content
Snippets Groups Projects
Select Git revision
  • 6565f6e4528b8b6ee0f9bffee8a61498f0ef9036
  • master default protected
  • feature/PRXS-3383-CollectionsSort
  • refactor/PRXS-3053-Files
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • feature/3149-LocaleCodeAsID-Feature
  • PRXS-3421-RecursiveReferences
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • feature/PRXS-3218-HideTemplateActions
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

ObjectHandler.go

Blame
  • client.go 4.87 KiB
    package content
    
    import (
    	"time"
    
    	"git.perx.ru/perxis/perxis-go/pkg/cache"
    	clientsSvc "git.perx.ru/perxis/perxis-go/pkg/clients/middleware"
    	clientsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/clients/transport/grpc"
    	collaboratorsSvc "git.perx.ru/perxis/perxis-go/pkg/collaborators/middleware"
    	collaboratorsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/collaborators/transport/grpc"
    	collectionsSvc "git.perx.ru/perxis/perxis-go/pkg/collections/middleware"
    	collectionsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/collections/transport/grpc"
    	environmentsSvc "git.perx.ru/perxis/perxis-go/pkg/environments/middleware"
    	environmentsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/environments/transport/grpc"
    	invitationsSvc "git.perx.ru/perxis/perxis-go/pkg/invitations/middleware"
    	invitationsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/invitations/transport/grpc"
    	itemsSvc "git.perx.ru/perxis/perxis-go/pkg/items/middleware"
    	itemsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/items/transport/grpc"
    	localesSvc "git.perx.ru/perxis/perxis-go/pkg/locales/middleware"
    	localsTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/locales/transport/grpc"
    	referencesSvc "git.perx.ru/perxis/perxis-go/pkg/references/middleware"
    	referencesTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/references/transport/grpc"
    	spacesSvc "git.perx.ru/perxis/perxis-go/pkg/spaces/middleware"
    	spacesTransportGrpc "git.perx.ru/perxis/perxis-go/pkg/spaces/transport/grpc"
    	rolesSvc "git.perx.ru/perxis/perxis-go/roles/middleware"
    	rolesTransportGrpc "git.perx.ru/perxis/perxis-go/roles/transport/grpc"
    	"go.uber.org/zap"
    	"google.golang.org/grpc"
    )
    
    const (
    	DefaultCacheSize = 1000
    	DefaultCacheTTL  = time.Second * 10
    )
    
    func NewClient(conn *grpc.ClientConn, opts ...Option) *Content {
    
    	client := &Content{}
    	config := &Config{}
    
    	for _, o := range opts {
    		o(config)
    	}
    
    	if config.Logger == nil {
    		config.Logger = zap.NewNop()
    	}
    
    	client.Spaces = spacesTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Environments = environmentsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Collections = collectionsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Items = itemsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Invitations = invitationsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Collaborators = collaboratorsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Clients = clientsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Locales = localsTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.Roles = rolesTransportGrpc.NewClient(conn, config.ClientOptions...)
    	client.References = referencesTransportGrpc.NewGRPCClient(conn, "", config.ClientOptions...)
    
    	if !config.NoDecode {
    		client.Items = itemsSvc.ClientEncodeMiddleware(client.Collections)(client.Items)
    		client.References = referencesSvc.ClientEncodeMiddleware(client.Collections)(client.References)
    	}
    
    	if !config.NoCache {
    		client = WithCaching(client, DefaultCacheSize, DefaultCacheTTL)
    	}
    
    	if !config.NoLog {
    		client = WithLogging(client, config.Logger, config.AccessLog)
    	}
    
    	return client
    }
    
    func WithCaching(client *Content, size int, ttl time.Duration) *Content {
    	c := *client
    
    	c.Clients = clientsSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Clients)
    	c.Environments = environmentsSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Environments)
    	c.Locales = localesSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Locales)
    	c.Roles = rolesSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Roles)
    	c.Spaces = spacesSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Spaces)
    	c.Items = itemsSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl), cache.NewMemoryCache(size, ttl), c.Environments)(client.Items)
    	c.Collections = collectionsSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl), c.Environments)(client.Collections)
    	c.Collaborators = collaboratorsSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Collaborators)
    	c.Invitations = invitationsSvc.CachingMiddleware(cache.NewMemoryCache(size, ttl))(client.Invitations)
    
    	return &c
    }
    
    func WithLogging(cs *Content, logger *zap.Logger, accessLog bool) *Content {
    	s := *cs
    
    	s.Collaborators = collaboratorsSvc.WithLog(s.Collaborators, logger, accessLog)
    	s.Collections = collectionsSvc.WithLog(s.Collections, logger, accessLog)
    	s.Environments = environmentsSvc.WithLog(s.Environments, logger, accessLog)
    	s.Invitations = invitationsSvc.WithLog(s.Invitations, logger, accessLog)
    	s.Items = itemsSvc.WithLog(s.Items, logger, accessLog)
    	s.Locales = localesSvc.WithLog(s.Locales, logger, accessLog)
    	s.Roles = rolesSvc.WithLog(s.Roles, logger, accessLog)
    	s.Spaces = spacesSvc.WithLog(s.Spaces, logger, accessLog)
    	s.Clients = clientsSvc.WithLog(s.Clients, logger, accessLog)
    	s.References = referencesSvc.WithLog(s.References, logger, accessLog)
    
    	return &s
    }