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.
feat(theme): site info add theme options
LinkinStarc946b08b239d
diff --git a/internal/base/handler/handler.go b/internal/base/handler/handler.goindex a29b19e9..8e7ef118 100644--- a/internal/base/handler/handler.go+++ b/internal/base/handler/handler.go@@ -63,7 +63,6 @@ func BindAndCheck(ctx *gin.Context, data interface{}) bool {// BindAndCheckReturnErr bind request and checkfunc BindAndCheckReturnErr(ctx *gin.Context, data interface{}) (errFields []*validator.FormErrorField) {lang := GetLang(ctx)- ctx.Set(constant.AcceptLanguageFlag, lang)if err := ctx.ShouldBind(data); err != nil {log.Errorf("http_handle BindAndCheck fail, %s", err.Error())HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)diff --git a/internal/base/middleware/accept_language.go b/internal/base/middleware/accept_language.gonew file mode 100644index 00000000..01cdcdab--- /dev/null+++ b/internal/base/middleware/accept_language.go@@ -0,0 +1,13 @@+package middleware++import (+ "github.com/answerdev/answer/internal/base/constant"+ "github.com/answerdev/answer/internal/base/handler"+ "github.com/gin-gonic/gin"+)++// ExtractAndSetAcceptLanguage extract accept language from header and set to context+func ExtractAndSetAcceptLanguage(ctx *gin.Context) {+ lang := handler.GetLang(ctx)+ ctx.Set(constant.AcceptLanguageFlag, lang)+}diff --git a/internal/base/server/http.go b/internal/base/server/http.goindex 70f0d96d..0a545118 100644--- a/internal/base/server/http.go+++ b/internal/base/server/http.go@@ -41,7 +41,7 @@ func NewHTTPServer(debug bool,gin.SetMode(gin.ReleaseMode)}r := gin.New()- r.Use(brotli.Brotli(brotli.DefaultCompression))+ r.Use(brotli.Brotli(brotli.DefaultCompression), middleware.ExtractAndSetAcceptLanguage)r.GET("/healthz", func(ctx *gin.Context) { ctx.String(200, "OK") })viewRouter.Register(r)diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.goindex 0efcd1d2..94e6786e 100644--- a/internal/schema/siteinfo_schema.go+++ b/internal/schema/siteinfo_schema.go@@ -1,8 +1,12 @@package schemaimport (+ "context""fmt""net/url"++ "github.com/answerdev/answer/internal/base/handler"+ "github.com/answerdev/answer/internal/base/translator")const PermaLinkQuestionIDAndTitle = 1@@ -118,7 +122,28 @@ type SiteLoginResp SiteLoginReqtype SiteCustomCssHTMLResp SiteCustomCssHTMLReq// SiteThemeResp site theme response-type SiteThemeResp SiteThemeReq+type SiteThemeResp struct {+ ThemeOptions []*ThemeOption `json:"theme_options"`+ Theme string `json:"theme"`+ ThemeConfig map[string]interface{} `json:"theme_config"`+}++func (s *SiteThemeResp) TrTheme(ctx context.Context) {+ la := handler.GetLangByCtx(ctx)+ for _, option := range s.ThemeOptions {+ tr := translator.GlobalTrans.Tr(la, option.Value)+ // if tr is equal the option value means not found translation, so use the original label+ if tr != option.Value {+ option.Label = tr+ }+ }+}++// ThemeOption get label option+type ThemeOption struct {+ Label string `json:"label"`+ Value string `json:"value"`+}// SiteWriteResp site write responsetype SiteWriteResp SiteWriteReqdiff --git a/internal/schema/theme_schema.go b/internal/schema/theme_schema.goindex ecf4bf77..da1a79ae 100644--- a/internal/schema/theme_schema.go+++ b/internal/schema/theme_schema.go@@ -1,12 +1,6 @@package schema-// GetThemeOption get label option-type GetThemeOption struct {- Label string `json:"label"`- Value string `json:"value"`-}--var GetThemeOptions = []*GetThemeOption{+var GetThemeOptions = []*ThemeOption{{Label: "Default",Value: "default",diff --git a/internal/service/siteinfo_common/siteinfo_service.go b/internal/service/siteinfo_common/siteinfo_service.goindex b4d3ec95..0690097b 100644--- a/internal/service/siteinfo_common/siteinfo_service.go+++ b/internal/service/siteinfo_common/siteinfo_service.go@@ -91,10 +91,13 @@ func (s *SiteInfoCommonService) GetSiteCustomCssHTML(ctx context.Context) (resp// GetSiteTheme get site themefunc (s *SiteInfoCommonService) GetSiteTheme(ctx context.Context) (resp *schema.SiteThemeResp, err error) {- resp = &schema.SiteThemeResp{}+ resp = &schema.SiteThemeResp{+ ThemeOptions: schema.GetThemeOptions,+ }if err = s.getSiteInfoByType(ctx, constant.SiteTypeTheme, resp); err != nil {return nil, err}+ resp.TrTheme(ctx)return resp, nil}