refactor(agent): make a register for site URL

This commit is contained in:
LinkinStars 2023-05-29 15:08:03 +08:00
parent fb412cb417
commit 209dc4f623
2 changed files with 19 additions and 11 deletions

View File

@ -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,

View File

@ -1,7 +1,6 @@
package plugin
import (
"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
}