version.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package service
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/core/api"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  7. "git.sxidc.com/go-framework/baize/framework/core/application"
  8. "git.sxidc.com/go-framework/baize/framework/core/domain"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  10. "git.sxidc.com/go-tools/utils/strutils"
  11. "github.com/pkg/errors"
  12. )
  13. var versionService = &VersionService{}
  14. type VersionService struct{}
  15. func (svc *VersionService) Init(appInstance *application.App) error {
  16. svc.prefixRoot(appInstance)
  17. return nil
  18. }
  19. func (svc *VersionService) Destroy() error {
  20. return nil
  21. }
  22. func (svc *VersionService) prefixRoot(appInstance *application.App) {
  23. prefixRootBinder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, ""), appInstance.Infrastructure())
  24. binding.GetBind(prefixRootBinder, &binding.SimpleBindItem[map[string]any]{
  25. Path: "/version",
  26. SendResponseFunc: response.SendMapResponse,
  27. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  28. localCacheVersion, err := i.LocalCache().Get("version")
  29. if err != nil {
  30. return nil, err
  31. }
  32. redisCacheVersion, err := i.RedisCache().Get("version")
  33. if err != nil {
  34. return nil, err
  35. }
  36. if localCacheVersion != redisCacheVersion {
  37. return nil, errors.New("版本不一致")
  38. }
  39. if strutils.IsStringEmpty(localCacheVersion) {
  40. localCacheVersion = "cache-empty-v1.0.0"
  41. }
  42. return map[string]any{
  43. "version": localCacheVersion,
  44. }, nil
  45. },
  46. })
  47. }