package service import ( "git.sxidc.com/go-framework/baize/framework/binding" "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-framework/baize/framework/core/api/request" "git.sxidc.com/go-framework/baize/framework/core/api/response" "git.sxidc.com/go-framework/baize/framework/core/application" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/infrastructure" "git.sxidc.com/go-tools/utils/strutils" "github.com/pkg/errors" ) var versionService = &VersionService{} type VersionService struct{} func (svc *VersionService) Init(appInstance *application.App) error { svc.prefixRoot(appInstance) return nil } func (svc *VersionService) Destroy() error { return nil } func (svc *VersionService) prefixRoot(appInstance *application.App) { prefixRootBinder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, ""), appInstance.Infrastructure()) binding.GetBind(prefixRootBinder, &binding.SimpleBindItem[map[string]any]{ Path: "/version", SendResponseFunc: response.SendMapResponse, ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) { localCacheVersion, err := i.LocalCache().Get("version") if err != nil { return nil, err } redisCacheVersion, err := i.RedisCache().Get("version") if err != nil { return nil, err } if localCacheVersion != redisCacheVersion { return nil, errors.New("版本不一致") } if strutils.IsStringEmpty(localCacheVersion) { localCacheVersion = "cache-empty-v1.0.0" } return map[string]any{ "version": localCacheVersion, }, nil }, }) }