A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
refactor(agent): make a register for site URL
LinkinStars209dc4f623da
diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.goindex 620906d3..adb2ba34 100644--- a/internal/service/siteinfo/siteinfo_service.go+++ b/internal/service/siteinfo/siteinfo_service.go@@ -16,6 +16,7 @@ import ("github.com/answerdev/answer/internal/service/siteinfo_common"tagcommon "github.com/answerdev/answer/internal/service/tag_common""github.com/answerdev/answer/pkg/uid"+ "github.com/answerdev/answer/plugin""github.com/jinzhu/copier""github.com/segmentfault/pacman/errors""github.com/segmentfault/pacman/log"@@ -36,15 +37,14 @@ func NewSiteInfoService(tagCommonService *tagcommon.TagCommonService,configService *config.ConfigService,) *SiteInfoService {- //usersSiteInfo, _ := siteInfoCommonService.GetSiteUsers(context.Background())- //if usersSiteInfo != nil {- // constant.DefaultAvatar = usersSiteInfo.DefaultAvatar- // constant.DefaultGravatarBaseURL = usersSiteInfo.GravatarBaseURL- //}- generalSiteInfo, _ := siteInfoCommonService.GetSiteGeneral(context.Background())- if generalSiteInfo != nil {- constant.DefaultSiteURL = generalSiteInfo.SiteUrl- }+ plugin.RegisterGetSiteURLFunc(func() string {+ generalSiteInfo, err := siteInfoCommonService.GetSiteGeneral(context.Background())+ if err != nil {+ log.Error(err)+ return ""+ }+ return generalSiteInfo.SiteUrl+ })return &SiteInfoService{siteInfoRepo: siteInfoRepo,diff --git a/plugin/agent.go b/plugin/agent.goindex 7de90ce5..6ac38d84 100644--- a/plugin/agent.go+++ b/plugin/agent.go@@ -1,7 +1,6 @@package pluginimport (- "github.com/answerdev/answer/internal/base/constant""github.com/gin-gonic/gin")@@ -15,10 +14,19 @@ type Agent interface {var (CallAgent,registerAgent = MakePlugin[Agent](true)+ siteURLFn func() string)// SiteURL The site url is the domain address of the current site. e.g. http://localhost:8080// When some Agent plugins want to redirect to the origin site, it can use this function to get the site url.func SiteURL() string {- return constant.DefaultSiteURL+ if siteURLFn != nil {+ return siteURLFn()+ }+ return ""+}++// RegisterGetSiteURLFunc Register a function to get the site url.+func RegisterGetSiteURLFunc(fn func() string) {+ siteURLFn = fn}