diff --git a/cache/memorymapcache.go b/cache/memorymapcache.go index 9285146..028d7b5 100644 --- a/cache/memorymapcache.go +++ b/cache/memorymapcache.go @@ -8,7 +8,7 @@ import ( ) type MemoryMapCache[K comparable, V any] struct { - safety.Map[K, mapVal[V]] + *safety.Map[K, mapVal[V]] } func NewMemoryMapCacheByFn[K comparable, V any](fn func(...any) (V, error), expireTime time.Duration) *MapCache[K, V] { diff --git a/cache/vars.go b/cache/vars.go index 42a719b..b8d3274 100644 --- a/cache/vars.go +++ b/cache/vars.go @@ -10,7 +10,7 @@ import ( ) type VarCache[T any] struct { - v safety.Var[vars[T]] + v *safety.Var[vars[T]] } type vars[T any] struct { diff --git a/internal/cmd/cachemanager/flush.go b/internal/cmd/cachemanager/flush.go new file mode 100644 index 0000000..8d57617 --- /dev/null +++ b/internal/cmd/cachemanager/flush.go @@ -0,0 +1,53 @@ +package cachemanager + +import ( + "context" + "github.com/fthvgb1/wp-go/cache" + "time" +) + +var ctx = context.Background() + +type flush interface { + Flush(ctx context.Context) +} + +type clear interface { + ClearExpired(ctx context.Context) +} + +var clears []clear + +var flushes []flush + +func Flush() { + for _, f := range flushes { + f.Flush(ctx) + } +} + +func MapCacheBy[K comparable, V any](fn func(...any) (V, error), expireTime time.Duration) *cache.MapCache[K, V] { + m := cache.NewMemoryMapCacheByFn[K, V](fn, expireTime) + FlushPush(m) + ClearPush(m) + return m +} +func MapBatchCacheBy[K comparable, V any](fn func(...any) (map[K]V, error), expireTime time.Duration) *cache.MapCache[K, V] { + m := cache.NewMemoryMapCacheByBatchFn[K, V](fn, expireTime) + FlushPush(m) + ClearPush(m) + return m +} + +func FlushPush(f ...flush) { + flushes = append(flushes, f...) +} +func ClearPush(c ...clear) { + clears = append(clears, c...) +} + +func ClearExpired() { + for _, c := range clears { + c.ClearExpired(ctx) + } +} diff --git a/internal/cmd/main.go b/internal/cmd/main.go index 9afcb3c..fc98a17 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -3,6 +3,8 @@ package main import ( "flag" "fmt" + "github.com/fthvgb1/wp-go/internal/cmd/cachemanager" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/cmd/route" "github.com/fthvgb1/wp-go/internal/mail" "github.com/fthvgb1/wp-go/internal/pkg/cache" @@ -25,7 +27,6 @@ import ( var confPath string var address string -var middleWareReloadFn func() var intReg = regexp.MustCompile(`^\d`) func init() { @@ -76,8 +77,7 @@ func cronClearCache() { for { select { case <-t.C: - cache.ClearCache() - plugins.ClearDigestCache() + cachemanager.ClearExpired() } } } @@ -89,12 +89,11 @@ func flushCache() { logs.ErrPrintln(err, "发邮件失败") } }() - cache.FlushCache() - plugins.FlushCache() + cachemanager.Flush() log.Println("all cache flushed") } -func reload() { +func reloads() { defer func() { if r := recover(); r != nil { log.Println(r) @@ -105,12 +104,8 @@ func reload() { err = wpconfig.InitOptions() logs.ErrPrintln(err, "获取网站设置WpOption失败") err = wpconfig.InitTerms() - wpconfig.FlushModes() - theme.Reload() logs.ErrPrintln(err, "获取WpTerms表失败") - if middleWareReloadFn != nil { - middleWareReloadFn() - } + reload.Reload() flushCache() log.Println("reload complete") } @@ -121,7 +116,7 @@ func signalNotify() { for { switch <-c { case syscall.SIGUSR1: - go reload() + go reloads() case syscall.SIGUSR2: go flushCache() } @@ -130,9 +125,8 @@ func signalNotify() { func main() { go signalNotify() - Gin, reloadFn := route.SetupRouter() + Gin := route.SetupRouter() c := config.GetConfig() - middleWareReloadFn = reloadFn if c.Ssl.Key != "" && c.Ssl.Cert != "" { err := Gin.RunTLS(address, c.Ssl.Cert, c.Ssl.Key) if err != nil { diff --git a/internal/cmd/reload/reload.go b/internal/cmd/reload/reload.go new file mode 100644 index 0000000..429045e --- /dev/null +++ b/internal/cmd/reload/reload.go @@ -0,0 +1,30 @@ +package reload + +import "github.com/fthvgb1/wp-go/safety" + +var calls []func() + +func Vars[T any](defaults T) *safety.Var[T] { + ss := safety.NewVar(defaults) + calls = append(calls, func() { + ss.Store(defaults) + }) + return ss +} +func VarsBy[T any](fn func() T) *safety.Var[T] { + ss := safety.NewVar(fn()) + calls = append(calls, func() { + ss.Store(fn()) + }) + return ss +} + +func Push(fn ...func()) { + calls = append(calls, fn...) +} + +func Reload() { + for _, call := range calls { + call() + } +} diff --git a/internal/cmd/route/route.go b/internal/cmd/route/route.go index e4360f0..b94f08b 100644 --- a/internal/cmd/route/route.go +++ b/internal/cmd/route/route.go @@ -2,6 +2,7 @@ package route import ( "github.com/fthvgb1/wp-go/internal/actions" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/middleware" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/constraints" @@ -16,7 +17,7 @@ import ( "net/http" ) -func SetupRouter() (*gin.Engine, func()) { +func SetupRouter() *gin.Engine { // Disable Console Color // gin.DisableConsoleColor() r := gin.New() @@ -30,14 +31,12 @@ func SetupRouter() (*gin.Engine, func()) { r.HTMLRender = theme.GetTemplate() wpconfig.SetTemplateFs(theme.TemplateFs) - - validServerName, reloadValidServerNameFn := middleware.ValidateServerNames() - fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) + siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) r.Use( gin.Logger(), - validServerName, + middleware.ValidateServerNames(), middleware.RecoverAndSendMail(gin.DefaultErrorWriter), - fl, + siteFlowLimitMiddleware, middleware.SetStaticFileCache, ) //gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用 @@ -63,8 +62,7 @@ func SetupRouter() (*gin.Engine, func()) { } store := cookie.NewStore([]byte("secret")) r.Use(sessions.Sessions("go-wp", store)) - sl, slRload := middleware.SearchLimit(c.SingleIpSearchNum) - r.GET("/", sl, actions.ThemeHook(constraints.Home)) + r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home)) r.GET("/page/:page", actions.ThemeHook(constraints.Home)) r.GET("/p/category/:category", actions.ThemeHook(constraints.Category)) r.GET("/p/category/:category/page/:page", actions.ThemeHook(constraints.Category)) @@ -79,16 +77,14 @@ func SetupRouter() (*gin.Engine, func()) { r.GET("/p/:id/feed", actions.PostFeed) r.GET("/feed", actions.Feed) r.GET("/comments/feed", actions.CommentsFeed) - cfl, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime) - r.POST("/comment", cfl, actions.PostComment) + commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime) + r.POST("/comment", commentMiddleWare, actions.PostComment) if c.Pprof != "" { pprof.Register(r, c.Pprof) } - fn := func() { - reloadValidServerNameFn() + reload.Push(func() { c := config.GetConfig() - flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) - slRload(c.SingleIpSearchNum) - } - return r, fn + siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) + }) + return r } diff --git a/internal/middleware/searchlimit.go b/internal/middleware/searchlimit.go index eda4ab8..6c1b7e6 100644 --- a/internal/middleware/searchlimit.go +++ b/internal/middleware/searchlimit.go @@ -1,9 +1,16 @@ package middleware -import "github.com/gin-gonic/gin" +import ( + "github.com/fthvgb1/wp-go/internal/cmd/reload" + "github.com/fthvgb1/wp-go/internal/pkg/config" + "github.com/gin-gonic/gin" +) -func SearchLimit(num int64) (func(ctx *gin.Context), func(int64)) { +func SearchLimit(num int64) func(ctx *gin.Context) { fn, reFn := IpLimit(num) + reload.Push(func() { + reFn(config.GetConfig().SingleIpSearchNum) + }) return func(c *gin.Context) { if c.Query("s") != "" { fn(c) @@ -11,5 +18,5 @@ func SearchLimit(num int64) (func(ctx *gin.Context), func(int64)) { c.Next() } - }, reFn + } } diff --git a/internal/middleware/validateservername.go b/internal/middleware/validateservername.go index 6e3f261..ac7fadd 100644 --- a/internal/middleware/validateservername.go +++ b/internal/middleware/validateservername.go @@ -1,35 +1,33 @@ package middleware import ( + "github.com/fthvgb1/wp-go/helper/maps" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/config" - "github.com/fthvgb1/wp-go/safety" "github.com/gin-gonic/gin" "net/http" "strings" ) -func ValidateServerNames() (func(ctx *gin.Context), func()) { - var serverName safety.Map[string, struct{}] - fn := func() { +func ValidateServerNames() func(ctx *gin.Context) { + sites := reload.VarsBy(func() map[string]struct{} { r := config.GetConfig().TrustServerNames + m := map[string]struct{}{} if len(r) > 0 { for _, name := range r { - serverName.Store(name, struct{}{}) + m[name] = struct{}{} } - } else { - serverName.Flush() } + return m + }) - } - fn() return func(c *gin.Context) { - if serverName.Len() > 0 { - if _, ok := serverName.Load(strings.Split(c.Request.Host, ":")[0]); !ok { - c.Status(http.StatusForbidden) - c.Abort() - return - } + m := sites.Load() + if len(m) > 0 && !maps.IsExists(m, strings.Split(c.Request.Host, ":")[0]) { + c.Status(http.StatusForbidden) + c.Abort() + return } c.Next() - }, fn + } } diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index ded8458..8d683cb 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -5,6 +5,7 @@ import ( "github.com/fthvgb1/wp-go/cache" "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/internal/cmd/cachemanager" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/dao" @@ -54,17 +55,17 @@ func InitActionsCommonCache() { fn: dao.Archives, } - searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime) + searchPostIdsCache = cachemanager.MapCacheBy[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime) - postListIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.PostListCacheTime) + postListIdsCache = cachemanager.MapCacheBy[string](dao.SearchPostIds, c.CacheTime.PostListCacheTime) - monthPostsCache = cache.NewMemoryMapCacheByFn[string](dao.MonthPost, c.CacheTime.MonthPostCacheTime) + monthPostsCache = cachemanager.MapCacheBy[string](dao.MonthPost, c.CacheTime.MonthPostCacheTime) - postContextCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetPostContext, c.CacheTime.ContextPostCacheTime) + postContextCache = cachemanager.MapCacheBy[uint64](dao.GetPostContext, c.CacheTime.ContextPostCacheTime) - postsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostsByIds, c.CacheTime.PostDataCacheTime) + postsCache = cachemanager.MapBatchCacheBy(dao.GetPostsByIds, c.CacheTime.PostDataCacheTime) - postMetaCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.CacheTime.PostDataCacheTime) + postMetaCache = cachemanager.MapBatchCacheBy(dao.GetPostMetaByPostIds, c.CacheTime.PostDataCacheTime) categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime) @@ -72,63 +73,33 @@ func InitActionsCommonCache() { recentCommentsCaches = cache.NewVarCache(dao.RecentComments, c.CacheTime.RecentCommentsCacheTime) - postCommentCaches = cache.NewMemoryMapCacheByFn[uint64](dao.PostComments, c.CacheTime.PostCommentsCacheTime) + postCommentCaches = cachemanager.MapCacheBy[uint64](dao.PostComments, c.CacheTime.PostCommentsCacheTime) maxPostIdCache = cache.NewVarCache(dao.GetMaxPostId, c.CacheTime.MaxPostIdCacheTime) - usersCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetUserById, c.CacheTime.UserInfoCacheTime) + usersCache = cachemanager.MapCacheBy[uint64](dao.GetUserById, c.CacheTime.UserInfoCacheTime) - usersNameCache = cache.NewMemoryMapCacheByFn[string](dao.GetUserByName, c.CacheTime.UserInfoCacheTime) + usersNameCache = cachemanager.MapCacheBy[string](dao.GetUserByName, c.CacheTime.UserInfoCacheTime) - commentsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetCommentByIds, c.CacheTime.CommentsCacheTime) + commentsCache = cachemanager.MapBatchCacheBy(dao.GetCommentByIds, c.CacheTime.CommentsCacheTime) allUsernameCache = cache.NewVarCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime) - headerImagesCache = cache.NewMemoryMapCacheByFn[string](getHeaderImages, c.CacheTime.ThemeHeaderImagCacheTime) + headerImagesCache = cachemanager.MapCacheBy[string](getHeaderImages, c.CacheTime.ThemeHeaderImagCacheTime) feedCache = cache.NewVarCache(feed, time.Hour) - postFeedCache = cache.NewMemoryMapCacheByFn[string](postFeed, time.Hour) + postFeedCache = cachemanager.MapCacheBy[string](postFeed, time.Hour) commentsFeedCache = cache.NewVarCache(commentsFeed, time.Hour) - newCommentCache = cache.NewMemoryMapCacheByFn[string, string](nil, 15*time.Minute) + newCommentCache = cachemanager.MapCacheBy[string, string](nil, 15*time.Minute) ctx = context.Background() InitFeed() } -func ClearCache() { - searchPostIdsCache.ClearExpired(ctx) - searchPostIdsCache.ClearExpired(ctx) - postsCache.ClearExpired(ctx) - postMetaCache.ClearExpired(ctx) - postListIdsCache.ClearExpired(ctx) - monthPostsCache.ClearExpired(ctx) - postContextCache.ClearExpired(ctx) - usersCache.ClearExpired(ctx) - commentsCache.ClearExpired(ctx) - usersNameCache.ClearExpired(ctx) - postFeedCache.ClearExpired(ctx) - newCommentCache.ClearExpired(ctx) - headerImagesCache.ClearExpired(ctx) -} -func FlushCache() { - searchPostIdsCache.Flush(ctx) - postsCache.Flush(ctx) - postMetaCache.Flush(ctx) - postListIdsCache.Flush(ctx) - monthPostsCache.Flush(ctx) - postContextCache.Flush(ctx) - usersCache.Flush(ctx) - commentsCache.Flush(ctx) - usersCache.Flush(ctx) - postFeedCache.Flush(ctx) - newCommentCache.Flush(ctx) - headerImagesCache.Flush(ctx) -} - func Archives(ctx context.Context) (r []models.PostArchive) { return archivesCaches.getArchiveCache(ctx) } diff --git a/internal/pkg/constraints/plugins.go b/internal/pkg/constraints/const.go similarity index 85% rename from internal/pkg/constraints/plugins.go rename to internal/pkg/constraints/const.go index 19b5f37..ddd8252 100644 --- a/internal/pkg/constraints/plugins.go +++ b/internal/pkg/constraints/const.go @@ -13,4 +13,6 @@ const ( Error404 ParamError InternalErr + + Defaults = "default" ) diff --git a/internal/plugins/digest.go b/internal/plugins/digest.go index 93d46e4..b5e368a 100644 --- a/internal/plugins/digest.go +++ b/internal/plugins/digest.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/fthvgb1/wp-go/cache" + "github.com/fthvgb1/wp-go/internal/cmd/cachemanager" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/plugin/digest" @@ -12,18 +13,9 @@ import ( ) var digestCache *cache.MapCache[uint64, string] -var ctx context.Context func InitDigestCache() { - ctx = context.Background() - digestCache = cache.NewMemoryMapCacheByFn[uint64](digestRaw, config.GetConfig().CacheTime.DigestCacheTime) -} - -func ClearDigestCache() { - digestCache.ClearExpired(ctx) -} -func FlushCache() { - digestCache.Flush(ctx) + digestCache = cachemanager.MapCacheBy[uint64](digestRaw, config.GetConfig().CacheTime.DigestCacheTime) } func digestRaw(arg ...any) (string, error) { diff --git a/internal/theme/common/customcss.go b/internal/theme/common/customcss.go index 3af457b..f2aea02 100644 --- a/internal/theme/common/customcss.go +++ b/internal/theme/common/customcss.go @@ -3,12 +3,13 @@ package common import ( "fmt" "github.com/fthvgb1/wp-go/helper/html" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/wpconfig" - "github.com/fthvgb1/wp-go/safety" ) -var css = safety.NewVar("default") +var css = reload.Vars(constraints.Defaults) func (h *Handle) CalCustomCss() (r string) { mods, err := wpconfig.GetThemeMods(h.Theme) @@ -25,7 +26,7 @@ func (h *Handle) CalCustomCss() (r string) { func (h *Handle) CustomCss() { cs := css.Load() - if cs == "default" { + if cs == constraints.Defaults { cs = h.CalCustomCss() css.Store(cs) } diff --git a/internal/theme/common/customlogo.go b/internal/theme/common/customlogo.go index 5702cd3..cd5f754 100644 --- a/internal/theme/common/customlogo.go +++ b/internal/theme/common/customlogo.go @@ -4,12 +4,13 @@ import ( "fmt" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/wpconfig" - "github.com/fthvgb1/wp-go/safety" ) -var logo = safety.NewVar("default") +var logo = reload.Vars(constraints.Defaults) func (h *Handle) CalCustomLogo() (r string) { mods, err := wpconfig.GetThemeMods(h.Theme) @@ -50,7 +51,7 @@ func (h *Handle) CalCustomLogo() (r string) { func (h *Handle) CustomLogo() { s := logo.Load() - if s == "default" { + if s == constraints.Defaults { s = h.CalCustomLogo() logo.Store(s) } diff --git a/internal/theme/common/detail.go b/internal/theme/common/detail.go index 9decf6b..9ec9ad1 100644 --- a/internal/theme/common/detail.go +++ b/internal/theme/common/detail.go @@ -105,7 +105,6 @@ func (d *DetailHandle) Render() { if d.Templ == "" { d.Templ = fmt.Sprintf("%s/posts/detail.gohtml", d.Theme) } - d.CustomBackGround() d.C.HTML(d.Code, d.Templ, d.GinH) } diff --git a/internal/theme/common/index.go b/internal/theme/common/index.go index 17d0a07..26948e6 100644 --- a/internal/theme/common/index.go +++ b/internal/theme/common/index.go @@ -119,7 +119,6 @@ func (i *IndexHandle) Render() { if i.Templ == "" { i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) } - i.CustomBackGround() i.C.HTML(i.Code, i.Templ, i.GinH) } diff --git a/internal/theme/common/reload.go b/internal/theme/common/reload.go deleted file mode 100644 index 27ffc32..0000000 --- a/internal/theme/common/reload.go +++ /dev/null @@ -1,7 +0,0 @@ -package common - -func Reload() { - backgroud.Store("default") - icon.Store("default") - css.Store("default") -} diff --git a/internal/theme/common/siteicon.go b/internal/theme/common/siteicon.go index 05b41d6..830fdc2 100644 --- a/internal/theme/common/siteicon.go +++ b/internal/theme/common/siteicon.go @@ -4,13 +4,14 @@ import ( "fmt" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/wpconfig" - "github.com/fthvgb1/wp-go/safety" "strings" ) -var icon = safety.NewVar("default") +var icon = reload.Vars(constraints.Defaults) var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} func (h *Handle) CalSiteIcon() (r string) { @@ -44,7 +45,7 @@ func (h *Handle) CalSiteIcon() (r string) { func (h *Handle) SiteIcon() { s := icon.Load() - if s == "default" { + if s == constraints.Defaults { s = h.CalSiteIcon() icon.Store(s) } diff --git a/internal/theme/common/template.gohtml b/internal/theme/common/template.gohtml index be79074..41439c2 100644 --- a/internal/theme/common/template.gohtml +++ b/internal/theme/common/template.gohtml @@ -1,7 +1,4 @@ {{define "common/head"}} - {{if .customBackground}} - {{.customBackground|unescaped}} - {{end}} {{if .customHeader}} {{.customHeader|unescaped}} {{end}} diff --git a/internal/theme/theme.go b/internal/theme/theme.go index 3a5fe99..7e82af4 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -2,7 +2,6 @@ package theme import ( "github.com/fthvgb1/wp-go/internal/pkg/config" - "github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/twentyfifteen" "github.com/fthvgb1/wp-go/internal/theme/twentyseventeen" "github.com/fthvgb1/wp-go/internal/wpconfig" @@ -13,11 +12,6 @@ func InitTheme() { addThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook) } -func Reload() { - twentyfifteen.Reload() - common.Reload() -} - func GetTemplateName() string { tmlp := config.GetConfig().Theme if tmlp == "" { diff --git a/internal/theme/common/custombackground.go b/internal/theme/twentyfifteen/custombackground.go similarity index 66% rename from internal/theme/common/custombackground.go rename to internal/theme/twentyfifteen/custombackground.go index 35a5ab2..df4619b 100644 --- a/internal/theme/common/custombackground.go +++ b/internal/theme/twentyfifteen/custombackground.go @@ -1,12 +1,13 @@ -package common +package twentyfifteen import ( "fmt" "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/cmd/reload" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/wpconfig" - "github.com/fthvgb1/wp-go/safety" ) var postx = map[string]string{ @@ -31,45 +32,45 @@ var repeat = map[string]string{ "no-repeat": "no-repeat", } -var backgroud = safety.NewVar("default") +var backgroud = reload.Vars(constraints.Defaults) -func (h *Handle) CustomBackGround() { +func (h *handle) CustomBackGround() { b := backgroud.Load() - if b == "default" { + if b == constraints.Defaults { b = h.CalCustomBackGround() backgroud.Store(b) } - h.GinH["customBackground"] = b + h.IndexHandle.GinH["customBackground"] = b } -func (h *Handle) CalCustomBackGround() (r string) { - mods, err := wpconfig.GetThemeMods(h.Theme) +func (h *handle) CalCustomBackGround() (r string) { + mods, err := wpconfig.GetThemeMods(h.IndexHandle.Theme) if err != nil { return } - if mods.BackgroundImage == "" && mods.BackgroundColor == mods.ThemeSupport.CustomBackground.DefaultColor { + if mods.BackgroundImage == "" && mods.BackgroundColor == themesupport.CustomBackground.DefaultColor { return } s := str.NewBuilder() if mods.BackgroundImage != "" { s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(mods.BackgroundImage)) } - backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, mods.ThemeSupport.CustomBackground.DefaultPositionX) + backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, themesupport.CustomBackground.DefaultPositionX) backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left") - backgroundPositionY := helper.Defaults(mods.BackgroundPositionY, mods.ThemeSupport.CustomBackground.DefaultPositionY) + backgroundPositionY := helper.Defaults(mods.BackgroundPositionY, themesupport.CustomBackground.DefaultPositionY) backgroundPositionY = maps.WithDefaultVal(posty, backgroundPositionY, "top") positon := fmt.Sprintf(" background-position: %s %s;", backgroundPositionX, backgroundPositionY) - siz := helper.DefaultVal(mods.BackgroundSize, mods.ThemeSupport.CustomBackground.DefaultSize) + siz := helper.DefaultVal(mods.BackgroundSize, themesupport.CustomBackground.DefaultSize) siz = maps.WithDefaultVal(size, siz, "auto") siz = fmt.Sprintf(" background-size: %s;", siz) - repeats := helper.Defaults(mods.BackgroundRepeat, mods.ThemeSupport.CustomBackground.DefaultRepeat) + repeats := helper.Defaults(mods.BackgroundRepeat, themesupport.CustomBackground.DefaultRepeat) repeats = maps.WithDefaultVal(repeat, repeats, "repeat") repeats = fmt.Sprintf(" background-repeat: %s;", repeats) - attachment := helper.Defaults(mods.BackgroundAttachment, mods.ThemeSupport.CustomBackground.DefaultAttachment) + attachment := helper.Defaults(mods.BackgroundAttachment, themesupport.CustomBackground.DefaultAttachment) attachment = helper.Or(attachment == "fixed", "fixed", "scroll") attachment = fmt.Sprintf(" background-attachment: %s;", attachment) s.WriteString(positon, siz, repeats, attachment) diff --git a/internal/theme/twentyfifteen/customheader.go b/internal/theme/twentyfifteen/customheader.go index a005f3f..6543022 100644 --- a/internal/theme/twentyfifteen/customheader.go +++ b/internal/theme/twentyfifteen/customheader.go @@ -2,7 +2,8 @@ package twentyfifteen import ( str "github.com/fthvgb1/wp-go/helper/strings" - "github.com/fthvgb1/wp-go/safety" + "github.com/fthvgb1/wp-go/internal/cmd/reload" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" ) var style = `