refactor(path): remove init for path ignore var

This commit is contained in:
LinkinStars 2023-05-29 12:14:33 +08:00
parent 184a04c245
commit 7e9becd8f5
14 changed files with 129 additions and 103 deletions

View File

@ -48,7 +48,6 @@ func runApp() {
if err != nil {
panic(err)
}
conf.GetPathIgnoreList()
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
if err != nil {

File diff suppressed because one or more lines are too long

View File

@ -4,8 +4,6 @@ import (
"bytes"
"path/filepath"
"github.com/answerdev/answer/configs"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/server"
"github.com/answerdev/answer/internal/base/translator"
@ -14,7 +12,6 @@ import (
"github.com/answerdev/answer/internal/service/service_config"
"github.com/answerdev/answer/pkg/writer"
"github.com/segmentfault/pacman/contrib/conf/viper"
"github.com/segmentfault/pacman/log"
"gopkg.in/yaml.v3"
)
@ -69,18 +66,3 @@ func RewriteConfig(configFilePath string, allConfig *AllConfig) error {
}
return writer.ReplaceFile(configFilePath, buf.String())
}
func GetPathIgnoreList() map[string]bool {
list := make(map[string]bool, 0)
data := &PathIgnore{}
err := yaml.Unmarshal(configs.PathIgnore, data)
if err != nil {
log.Error(err)
return list
}
for _, item := range data.Users {
list[item] = true
}
constant.PathIgnoreMap = list
return list
}

View File

@ -0,0 +1,19 @@
package constant
import "time"
const (
UserStatusChangedCacheKey = "answer:user:status:"
UserStatusChangedCacheTime = 7 * 24 * time.Hour
UserTokenCacheKey = "answer:user:token:"
UserTokenCacheTime = 7 * 24 * time.Hour
AdminTokenCacheKey = "answer:admin:token:"
AdminTokenCacheTime = 7 * 24 * time.Hour
UserTokenMappingCacheKey = "answer:user-token:mapping:"
SiteInfoCacheKey = "answer:site-info:"
SiteInfoCacheTime = 1 * time.Hour
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
ConnectorUserExternalInfoCacheKey = "answer:connector:"
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
)

View File

@ -1,7 +0,0 @@
package constant
const (
PluginStatus = "plugin.status"
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
)

View File

@ -1,8 +0,0 @@
package constant
import "time"
const (
ConnectorUserExternalInfoCacheKey = "answer:connector:"
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
)

View File

@ -1,76 +1,10 @@
package constant
import "time"
const (
DefaultPageSize = 20 // Default number of pages
UserStatusChangedCacheKey = "answer:user:status:"
UserStatusChangedCacheTime = 7 * 24 * time.Hour
UserTokenCacheKey = "answer:user:token:"
UserTokenCacheTime = 7 * 24 * time.Hour
AdminTokenCacheKey = "answer:admin:token:"
AdminTokenCacheTime = 7 * 24 * time.Hour
AcceptLanguageFlag = "Accept-Language"
UserTokenMappingCacheKey = "answer:user-token:mapping:"
SiteInfoCacheKey = "answer:site-info:"
SiteInfoCacheTime = 1 * time.Hour
DefaultPageSize = 20 // Default number of pages
)
const (
QuestionObjectType = "question"
AnswerObjectType = "answer"
TagObjectType = "tag"
UserObjectType = "user"
CollectionObjectType = "collection"
CommentObjectType = "comment"
ReportObjectType = "report"
)
// ObjectTypeStrMapping key => value
// object TagID AnswerList
// key equal database's table name
var (
Version string = ""
Revision string = ""
PathIgnoreMap map[string]bool
ObjectTypeStrMapping = map[string]int{
QuestionObjectType: 1,
AnswerObjectType: 2,
TagObjectType: 3,
UserObjectType: 4,
CollectionObjectType: 6,
CommentObjectType: 7,
ReportObjectType: 8,
}
ObjectTypeNumberMapping = map[int]string{
1: QuestionObjectType,
2: AnswerObjectType,
3: TagObjectType,
4: UserObjectType,
6: CollectionObjectType,
7: CommentObjectType,
8: ReportObjectType,
}
)
const (
SiteTypeGeneral = "general"
SiteTypeInterface = "interface"
SiteTypeBranding = "branding"
SiteTypeWrite = "write"
SiteTypeLegal = "legal"
SiteTypeSeo = "seo"
SiteTypeLogin = "login"
SiteTypeCustomCssHTML = "css-html"
SiteTypeTheme = "theme"
SiteTypePrivileges = "privileges"
SiteTypeUsers = "users"
)
func ExistInPathIgnore(name string) bool {
_, ok := PathIgnoreMap[name]
return ok
}

View File

@ -0,0 +1,5 @@
package constant
const (
AcceptLanguageFlag = "Accept-Language"
)

View File

@ -0,0 +1,33 @@
package constant
const (
QuestionObjectType = "question"
AnswerObjectType = "answer"
TagObjectType = "tag"
UserObjectType = "user"
CollectionObjectType = "collection"
CommentObjectType = "comment"
ReportObjectType = "report"
)
var (
ObjectTypeStrMapping = map[string]int{
QuestionObjectType: 1,
AnswerObjectType: 2,
TagObjectType: 3,
UserObjectType: 4,
CollectionObjectType: 6,
CommentObjectType: 7,
ReportObjectType: 8,
}
ObjectTypeNumberMapping = map[int]string{
1: QuestionObjectType,
2: AnswerObjectType,
3: TagObjectType,
4: UserObjectType,
6: CollectionObjectType,
7: CommentObjectType,
8: ReportObjectType,
}
)

View File

@ -0,0 +1,5 @@
package constant
const (
PluginStatus = "plugin.status"
)

View File

@ -0,0 +1,15 @@
package constant
const (
SiteTypeGeneral = "general"
SiteTypeInterface = "interface"
SiteTypeBranding = "branding"
SiteTypeWrite = "write"
SiteTypeLegal = "legal"
SiteTypeSeo = "seo"
SiteTypeLogin = "login"
SiteTypeCustomCssHTML = "css-html"
SiteTypeTheme = "theme"
SiteTypePrivileges = "privileges"
SiteTypeUsers = "users"
)

View File

@ -14,6 +14,7 @@ import (
templaterender "github.com/answerdev/answer/internal/controller/template_render"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/pkg/checker"
"github.com/answerdev/answer/pkg/converter"
"github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/pkg/obj"
@ -421,7 +422,8 @@ func (tc *TemplateController) UserInfo(ctx *gin.Context) {
tc.Page404(ctx)
return
}
exist := constant.ExistInPathIgnore(username)
exist := checker.IsUsersIgnorePath(username)
if exist {
file, err := ui.Build.ReadFile("build/index.html")
if err != nil {

View File

@ -0,0 +1,47 @@
package checker
import (
"sync"
"github.com/answerdev/answer/configs"
"github.com/segmentfault/pacman/log"
"gopkg.in/yaml.v3"
)
type PathIgnore struct {
Users []string `yaml:"users"`
Questions []string `yaml:"questions"`
}
var (
ignorePathInit sync.Once
pathIgnore = &PathIgnore{}
)
func initPathIgnore() {
if err := yaml.Unmarshal(configs.PathIgnore, pathIgnore); err != nil {
log.Error(err)
}
}
// IsUsersIgnorePath checks whether the username is in ignore path
func IsUsersIgnorePath(username string) bool {
ignorePathInit.Do(initPathIgnore)
for _, u := range pathIgnore.Users {
if u == username {
return true
}
}
return false
}
// IsQuestionsIgnorePath checks whether the questionID is in ignore path
func IsQuestionsIgnorePath(questionID string) bool {
ignorePathInit.Do(initPathIgnore)
for _, u := range pathIgnore.Questions {
if u == questionID {
return true
}
}
return false
}