重构reload config和flush cache 机制
This commit is contained in:
parent
6ad2b3cf64
commit
0faac992cf
2
cache/memorymapcache.go
vendored
2
cache/memorymapcache.go
vendored
|
@ -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] {
|
||||
|
|
2
cache/vars.go
vendored
2
cache/vars.go
vendored
|
@ -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 {
|
||||
|
|
53
internal/cmd/cachemanager/flush.go
Normal file
53
internal/cmd/cachemanager/flush.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
30
internal/cmd/reload/reload.go
Normal file
30
internal/cmd/reload/reload.go
Normal file
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
57
internal/pkg/cache/cache.go
vendored
57
internal/pkg/cache/cache.go
vendored
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -13,4 +13,6 @@ const (
|
|||
Error404
|
||||
ParamError
|
||||
InternalErr
|
||||
|
||||
Defaults = "default"
|
||||
)
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package common
|
||||
|
||||
func Reload() {
|
||||
backgroud.Store("default")
|
||||
icon.Store("default")
|
||||
css.Store("default")
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
{{define "common/head"}}
|
||||
{{if .customBackground}}
|
||||
{{.customBackground|unescaped}}
|
||||
{{end}}
|
||||
{{if .customHeader}}
|
||||
{{.customHeader|unescaped}}
|
||||
{{end}}
|
||||
|
|
|
@ -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 == "" {
|
||||
|
|
|
@ -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)
|
|
@ -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 = `<style type="text/css" id="twentyfifteen-header-css">`
|
||||
|
@ -79,11 +80,7 @@ var imgStyle = `.site-header {
|
|||
}
|
||||
}`
|
||||
|
||||
var header = safety.NewVar("default")
|
||||
|
||||
func Reload() {
|
||||
header.Store("default")
|
||||
}
|
||||
var header = reload.Vars(constraints.Defaults)
|
||||
|
||||
func (h *handle) CalCustomHeader() (r string, rand bool) {
|
||||
img, rand := h.IndexHandle.GetCustomHeader()
|
||||
|
@ -112,7 +109,7 @@ func (h *handle) CalCustomHeader() (r string, rand bool) {
|
|||
|
||||
func (h *handle) CustomHeader() {
|
||||
headers := header.Load()
|
||||
if headers == "default" {
|
||||
if headers == constraints.Defaults {
|
||||
headerss, rand := h.CalCustomHeader()
|
||||
headers = headerss
|
||||
if !rand {
|
||||
|
|
|
@ -60,4 +60,8 @@
|
|||
|
||||
{{template "common/head" .}}
|
||||
|
||||
{{if .customBackground}}
|
||||
{{.customBackground|unescaped}}
|
||||
{{end}}
|
||||
|
||||
{{end}}
|
33
internal/theme/twentyfifteen/themesupport.go
Normal file
33
internal/theme/twentyfifteen/themesupport.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package twentyfifteen
|
||||
|
||||
type themeSupport struct {
|
||||
CustomBackground customBackground `json:"custom-background"`
|
||||
EditorColorPalette []EditorColorPalette `json:"editor-color-palette"`
|
||||
EditorGradientPresets []EditorGradientPresets `json:"editor-gradient-presets"`
|
||||
}
|
||||
type customBackground struct {
|
||||
DefaultImage string `json:"default-image"`
|
||||
DefaultPreset string `json:"default-preset"`
|
||||
DefaultPositionX string `json:"default-position-x"`
|
||||
DefaultPositionY string `json:"default-position-y"`
|
||||
DefaultSize string `json:"default-size"`
|
||||
DefaultRepeat string `json:"default-repeat"`
|
||||
DefaultAttachment string `json:"default-attachment"`
|
||||
DefaultColor string `json:"default-color"`
|
||||
WpHeadCallback string `json:"wp-head-callback"`
|
||||
AdminHeadCallback string `json:"admin-head-callback"`
|
||||
AdminPreviewCallback string `json:"admin-preview-callback"`
|
||||
}
|
||||
|
||||
type EditorColorPalette struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
type EditorGradientPresets struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Gradient string `json:"gradient"`
|
||||
}
|
||||
|
||||
var themesupport = themeSupport{}
|
|
@ -37,10 +37,12 @@ func Hook(h *common.Handle) {
|
|||
|
||||
func (h *handle) Index() {
|
||||
h.CustomHeader()
|
||||
h.CustomBackGround()
|
||||
h.Indexs()
|
||||
}
|
||||
|
||||
func (h *handle) Detail() {
|
||||
h.CustomHeader()
|
||||
h.CustomBackGround()
|
||||
h.Details()
|
||||
}
|
||||
|
|
17
internal/theme/twentyseventeen/themesupport.go
Normal file
17
internal/theme/twentyseventeen/themesupport.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package twentyseventeen
|
||||
|
||||
type themeSupport struct {
|
||||
}
|
||||
|
||||
type EditorColorPalette struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
type EditorGradientPresets struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Gradient string `json:"gradient"`
|
||||
}
|
||||
|
||||
var themesupport = themeSupport{}
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||
"github.com/fthvgb1/wp-go/internal/phphelper"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
|
@ -103,11 +104,13 @@ func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except .
|
|||
return
|
||||
}
|
||||
|
||||
var themeModes = safety.Map[string, ThemeMod]{}
|
||||
|
||||
func FlushModes() {
|
||||
themeModes.Flush()
|
||||
}
|
||||
var themeModes = func() *safety.Map[string, ThemeMod] {
|
||||
m := safety.NewMap[string, ThemeMod]()
|
||||
reload.Push(func() {
|
||||
m.Flush()
|
||||
})
|
||||
return m
|
||||
}()
|
||||
|
||||
func GetThemeMods(theme string) (r ThemeMod, err error) {
|
||||
r, ok := themeModes.Load(theme)
|
||||
|
|
|
@ -1,27 +1,22 @@
|
|||
package wpconfig
|
||||
|
||||
type ThemeSupport struct {
|
||||
CoreBlockPatterns bool `json:"core-block-patterns"`
|
||||
WidgetsBlockEditor bool `json:"widgets-block-editor"`
|
||||
AutomaticFeedLinks bool `json:"automatic-feed-links"`
|
||||
TitleTag bool `json:"title-tag"`
|
||||
CustomLineHeight bool `json:"custom-line-height"`
|
||||
PostThumbnails bool `json:"post-thumbnails"`
|
||||
Menus bool `json:"menus"`
|
||||
HTML5 []string `json:"html5"`
|
||||
PostFormats []string `json:"post-formats"`
|
||||
CustomLogo CustomLogo `json:"custom-logo"`
|
||||
CustomBackground CustomBackground `json:"custom-background"`
|
||||
EditorStyle bool `json:"editor-style"`
|
||||
EditorStyles bool `json:"editor-styles"`
|
||||
WpBlockStyles bool `json:"wp-block-styles"`
|
||||
ResponsiveEmbeds bool `json:"responsive-embeds"`
|
||||
EditorColorPalette []EditorColorPalette `json:"editor-color-palette"`
|
||||
EditorGradientPresets []EditorGradientPresets `json:"editor-gradient-presets"`
|
||||
CustomizeSelectiveRefreshWidgets bool `json:"customize-selective-refresh-widgets"`
|
||||
StarterContent StarterContent `json:"starter-content"`
|
||||
CustomHeader CustomHeader `json:"custom-header"`
|
||||
Widgets bool `json:"widgets"`
|
||||
CoreBlockPatterns bool `json:"core-block-patterns"`
|
||||
WidgetsBlockEditor bool `json:"widgets-block-editor"`
|
||||
AutomaticFeedLinks bool `json:"automatic-feed-links"`
|
||||
TitleTag bool `json:"title-tag"`
|
||||
PostThumbnails bool `json:"post-thumbnails"`
|
||||
Menus bool `json:"menus"`
|
||||
HTML5 []string `json:"html5"`
|
||||
PostFormats []string `json:"post-formats"`
|
||||
CustomLogo CustomLogo `json:"custom-logo"`
|
||||
CustomizeSelectiveRefreshWidgets bool `json:"customize-selective-refresh-widgets"`
|
||||
EditorStyle bool `json:"editor-style"`
|
||||
EditorStyles bool `json:"editor-styles"`
|
||||
WpBlockStyles bool `json:"wp-block-styles"`
|
||||
ResponsiveEmbeds bool `json:"responsive-embeds"`
|
||||
CustomHeader CustomHeader `json:"custom-header"`
|
||||
Widgets bool `json:"widgets"`
|
||||
}
|
||||
type CustomLogo struct {
|
||||
Width int `json:"width"`
|
||||
|
@ -31,102 +26,7 @@ type CustomLogo struct {
|
|||
HeaderText string `json:"header-text"`
|
||||
UnlinkHomepageLogo bool `json:"unlink-homepage-logo"`
|
||||
}
|
||||
type Widgets struct {
|
||||
Sidebar1 []string `json:"sidebar-1"`
|
||||
Sidebar2 []string `json:"sidebar-2"`
|
||||
Sidebar3 []string `json:"sidebar-3"`
|
||||
}
|
||||
type About struct {
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
}
|
||||
type Contact struct {
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
}
|
||||
type Blog struct {
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
}
|
||||
type HomepageSection struct {
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
}
|
||||
type CustomBackground struct {
|
||||
DefaultImage string `json:"default-image"`
|
||||
DefaultPreset string `json:"default-preset"`
|
||||
DefaultPositionX string `json:"default-position-x"`
|
||||
DefaultPositionY string `json:"default-position-y"`
|
||||
DefaultSize string `json:"default-size"`
|
||||
DefaultRepeat string `json:"default-repeat"`
|
||||
DefaultAttachment string `json:"default-attachment"`
|
||||
DefaultColor string `json:"default-color"`
|
||||
WpHeadCallback string `json:"wp-head-callback"`
|
||||
AdminHeadCallback string `json:"admin-head-callback"`
|
||||
AdminPreviewCallback string `json:"admin-preview-callback"`
|
||||
}
|
||||
type EditorColorPalette struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
type EditorGradientPresets struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Gradient string `json:"gradient"`
|
||||
}
|
||||
|
||||
type Posts struct {
|
||||
Num0 string `json:"0"`
|
||||
About About `json:"about"`
|
||||
Contact Contact `json:"contact"`
|
||||
Blog Blog `json:"blog"`
|
||||
HomepageSection HomepageSection `json:"homepage-section"`
|
||||
}
|
||||
type ImageEspresso struct {
|
||||
PostTitle string `json:"post_title"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
type ImageSandwich struct {
|
||||
PostTitle string `json:"post_title"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
type ImageCoffee struct {
|
||||
PostTitle string `json:"post_title"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
type Attachments struct {
|
||||
ImageEspresso ImageEspresso `json:"image-espresso"`
|
||||
ImageSandwich ImageSandwich `json:"image-sandwich"`
|
||||
ImageCoffee ImageCoffee `json:"image-coffee"`
|
||||
}
|
||||
type Option struct {
|
||||
ShowOnFront string `json:"show_on_front"`
|
||||
PageOnFront string `json:"page_on_front"`
|
||||
PageForPosts string `json:"page_for_posts"`
|
||||
}
|
||||
type ThemeMods struct {
|
||||
Panel1 string `json:"panel_1"`
|
||||
Panel2 string `json:"panel_2"`
|
||||
Panel3 string `json:"panel_3"`
|
||||
Panel4 string `json:"panel_4"`
|
||||
}
|
||||
type Top struct {
|
||||
Name string `json:"name"`
|
||||
Items []string `json:"items"`
|
||||
}
|
||||
type Social struct {
|
||||
Name string `json:"name"`
|
||||
Items []string `json:"items"`
|
||||
}
|
||||
type NavMenus struct {
|
||||
Top Top `json:"top"`
|
||||
Social Social `json:"social"`
|
||||
}
|
||||
type StarterContent struct {
|
||||
Widgets Widgets `json:"widgets"`
|
||||
Posts Posts `json:"posts"`
|
||||
Attachments Attachments `json:"attachments"`
|
||||
Options Option `json:"options"`
|
||||
ThemeMods ThemeMods `json:"theme_mods"`
|
||||
NavMenus NavMenus `json:"nav_menus"`
|
||||
}
|
||||
type CustomHeader struct {
|
||||
DefaultImage string `json:"default-image"`
|
||||
RandomDefault bool `json:"random-default"`
|
||||
|
|
|
@ -58,8 +58,8 @@ type Map[K comparable, V any] struct {
|
|||
expunged unsafe.Pointer
|
||||
}
|
||||
|
||||
func NewMap[K comparable, V any]() Map[K, V] {
|
||||
return Map[K, V]{
|
||||
func NewMap[K comparable, V any]() *Map[K, V] {
|
||||
return &Map[K, V]{
|
||||
expunged: unsafe.Pointer(new(any)),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ func TestMap_Load(t *testing.T) {
|
|||
m.Store(1, 1)
|
||||
type testCase[K comparable, V any] struct {
|
||||
name string
|
||||
m Map[K, V]
|
||||
m *Map[K, V]
|
||||
args args[K]
|
||||
wantValue V
|
||||
wantOk bool
|
||||
|
|
|
@ -3,7 +3,7 @@ package safety
|
|||
import "sync"
|
||||
|
||||
type Slice[T any] struct {
|
||||
Var[[]T]
|
||||
*Var[[]T]
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ type Var[T any] struct {
|
|||
p unsafe.Pointer
|
||||
}
|
||||
|
||||
func NewVar[T any](val T) Var[T] {
|
||||
return Var[T]{val: val, p: unsafe.Pointer(&val)}
|
||||
func NewVar[T any](val T) *Var[T] {
|
||||
return &Var[T]{val: val, p: unsafe.Pointer(&val)}
|
||||
}
|
||||
|
||||
func (r *Var[T]) Load() T {
|
||||
|
|
|
@ -39,6 +39,12 @@ func TestVar_Load(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
r := NewVar("ff")
|
||||
fmt.Println(r.Load())
|
||||
q := r
|
||||
fmt.Println(q.Load())
|
||||
q.Store("xx")
|
||||
fmt.Println(r.Load(), q.Load())
|
||||
}
|
||||
|
||||
func TestVar_Delete(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user