optimize code and fix bug
This commit is contained in:
parent
802c7c7dcc
commit
f49ad731e3
|
@ -8,9 +8,9 @@ import (
|
||||||
|
|
||||||
func SearchLimit(num int64) func(ctx *gin.Context) {
|
func SearchLimit(num int64) func(ctx *gin.Context) {
|
||||||
fn, reFn := IpLimit(num)
|
fn, reFn := IpLimit(num)
|
||||||
reload.Push(func() {
|
reload.Append(func() {
|
||||||
reFn(config.GetConfig().SingleIpSearchNum)
|
reFn(config.GetConfig().SingleIpSearchNum)
|
||||||
})
|
}, "search-ip-limit-number")
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if c.Query("s") != "" {
|
if c.Query("s") != "" {
|
||||||
fn(c)
|
fn(c)
|
||||||
|
|
|
@ -19,7 +19,7 @@ func ValidateServerNames() func(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
})
|
}, "site-names")
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
m := sites.Load()
|
m := sites.Load()
|
||||||
|
|
6
app/pkg/cache/cache.go
vendored
6
app/pkg/cache/cache.go
vendored
|
@ -7,7 +7,7 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||||
"github.com/fthvgb1/wp-go/safety"
|
"github.com/fthvgb1/wp-go/cache/reload"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -102,9 +102,9 @@ type Arch struct {
|
||||||
month time.Month
|
month time.Month
|
||||||
}
|
}
|
||||||
|
|
||||||
var arch = safety.NewVar(Arch{
|
var arch = reload.Vars(Arch{
|
||||||
fn: dao.Archives,
|
fn: dao.Archives,
|
||||||
})
|
}, "archives-year-month-data")
|
||||||
|
|
||||||
func Archives(ctx context.Context) []models.PostArchive {
|
func Archives(ctx context.Context) []models.PostArchive {
|
||||||
a := arch.Load()
|
a := arch.Load()
|
||||||
|
|
|
@ -91,9 +91,9 @@ func SetupRouter() *gin.Engine {
|
||||||
fn(r)
|
fn(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
reload.Push(func() {
|
reload.Append(func() {
|
||||||
c := config.GetConfig()
|
c := config.GetConfig()
|
||||||
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
||||||
})
|
}, "site-flowLimit-config")
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var comFn = template.FuncMap{
|
func postsFn(fn func(models.Posts) string, a models.Posts) string {
|
||||||
|
return fn(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FuncMap() template.FuncMap {
|
||||||
|
return template.FuncMap{
|
||||||
"unescaped": func(s string) any {
|
"unescaped": func(s string) any {
|
||||||
return template.HTML(s)
|
return template.HTML(s)
|
||||||
},
|
},
|
||||||
|
@ -28,19 +33,5 @@ var comFn = template.FuncMap{
|
||||||
"callFuncString": func(fn func(string) string, s string) template.HTML {
|
"callFuncString": func(fn func(string) string, s string) template.HTML {
|
||||||
return template.HTML(fn(s))
|
return template.HTML(fn(s))
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
func postsFn(fn func(models.Posts) string, a models.Posts) string {
|
|
||||||
return fn(a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FuncMap() template.FuncMap {
|
|
||||||
return comFn
|
|
||||||
}
|
|
||||||
|
|
||||||
func addTemplateFunc(fnName string, fn any) {
|
|
||||||
if _, ok := comFn[fnName]; ok {
|
|
||||||
panic("exists same name func")
|
|
||||||
}
|
}
|
||||||
comFn[fnName] = fn
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ var imgStyle = `.site-header {
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
var header = reload.Vars(constraints.Defaults)
|
var header = reload.Vars(constraints.Defaults, "twentyfifteen-customheader")
|
||||||
|
|
||||||
func calCustomHeaderImg(h *wp.Handle) (r string, rand bool) {
|
func calCustomHeaderImg(h *wp.Handle) (r string, rand bool) {
|
||||||
img, rand := h.GetCustomHeaderImg()
|
img, rand := h.GetCustomHeaderImg()
|
||||||
|
|
|
@ -162,7 +162,7 @@ func postThumbnail(h *wp.Handle, posts *models.Posts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var header = reload.Vars(models.PostThumbnail{})
|
var header = reload.Vars(models.PostThumbnail{}, "twentyseventeen-headerImage")
|
||||||
|
|
||||||
func calCustomHeader(h *wp.Handle) {
|
func calCustomHeader(h *wp.Handle) {
|
||||||
h.SetData("HeaderImage", getHeaderImage(h))
|
h.SetData("HeaderImage", getHeaderImage(h))
|
||||||
|
|
|
@ -23,7 +23,7 @@ func PostsPlugins(initial PostsPlugin, calls ...func(PostsPlugin, *Handle, *mode
|
||||||
var pluginFns = reload.Vars(map[string]func(PostsPlugin, *Handle, *models.Posts){
|
var pluginFns = reload.Vars(map[string]func(PostsPlugin, *Handle, *models.Posts){
|
||||||
"passwordProject": PasswordProject,
|
"passwordProject": PasswordProject,
|
||||||
"digest": Digest,
|
"digest": Digest,
|
||||||
})
|
}, "list-post-plugins-fns")
|
||||||
|
|
||||||
func (h *Handle) PushPostsPlugin(name string, fn func(PostsPlugin, *Handle, *models.Posts)) {
|
func (h *Handle) PushPostsPlugin(name string, fn func(PostsPlugin, *Handle, *models.Posts)) {
|
||||||
m := pluginFns.Load()
|
m := pluginFns.Load()
|
||||||
|
@ -53,7 +53,7 @@ func Digest(next PostsPlugin, h *Handle, post *models.Posts) {
|
||||||
next(h, post)
|
next(h, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ordinaryPlugin = reload.Vars([]PostsPlugin{})
|
var ordinaryPlugin = reload.Vars([]PostsPlugin{}, "ordinaryPlugin")
|
||||||
|
|
||||||
func (h *Handle) PushPostPlugin(plugin ...PostsPlugin) {
|
func (h *Handle) PushPostPlugin(plugin ...PostsPlugin) {
|
||||||
p := ordinaryPlugin.Load()
|
p := ordinaryPlugin.Load()
|
||||||
|
|
|
@ -81,7 +81,7 @@ var plainRouteParam = reload.Vars([]Plain{
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}, "plainRouteParam")
|
||||||
|
|
||||||
var GetUsersIds = reload.BuildValFnWithConfirm("usersIds", func(h *wp.Handle) (map[uint64]string, bool) {
|
var GetUsersIds = reload.BuildValFnWithConfirm("usersIds", func(h *wp.Handle) (map[uint64]string, bool) {
|
||||||
users, err := cache.GetAllUsername(h.C)
|
users, err := cache.GetAllUsername(h.C)
|
||||||
|
|
|
@ -110,20 +110,17 @@ func BuildHandler(pipeScene string, keyFn func(*Handle, string) string,
|
||||||
|
|
||||||
func HookHandles(hooks map[string][]func(HandleCall) (HandleCall, bool),
|
func HookHandles(hooks map[string][]func(HandleCall) (HandleCall, bool),
|
||||||
handlers map[string][]HandleCall, sceneOrStats ...string) []HandleCall {
|
handlers map[string][]HandleCall, sceneOrStats ...string) []HandleCall {
|
||||||
|
|
||||||
hookedHandlers := slice.FilterAndMap(sceneOrStats, func(k string) ([]HandleCall, bool) {
|
hookedHandlers := slice.FilterAndMap(sceneOrStats, func(k string) ([]HandleCall, bool) {
|
||||||
r := handlers[k]
|
r := handlers[k]
|
||||||
for _, hook := range hooks[k] {
|
for _, hook := range hooks[k] {
|
||||||
r = HookHandler(hook, r)
|
r = slice.FilterAndMap(r, hook)
|
||||||
}
|
}
|
||||||
return r, true
|
return r, true
|
||||||
})
|
})
|
||||||
return slice.Decompress(hookedHandlers)
|
return slice.Decompress(hookedHandlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HookHandler(hookFn func(HandleCall) (HandleCall, bool), handlers []HandleCall) []HandleCall {
|
|
||||||
return slice.FilterAndMap(handlers, hookFn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PipeKey(h *Handle, pipScene string) string {
|
func PipeKey(h *Handle, pipScene string) string {
|
||||||
key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats)
|
key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats)
|
||||||
return h.DoActionFilter("pipeKey", key, pipScene)
|
return h.DoActionFilter("pipeKey", key, pipScene)
|
||||||
|
|
|
@ -20,10 +20,10 @@ var routeHook []func(Route) (Route, bool)
|
||||||
var regRoutes *safety.Map[string, *regexp.Regexp]
|
var regRoutes *safety.Map[string, *regexp.Regexp]
|
||||||
var routes = func() *safety.Map[string, Route] {
|
var routes = func() *safety.Map[string, Route] {
|
||||||
r := safety.NewMap[string, Route]()
|
r := safety.NewMap[string, Route]()
|
||||||
reload.Push(func() {
|
reload.Append(func() {
|
||||||
r.Flush()
|
r.Flush()
|
||||||
regRoutes.Flush()
|
regRoutes.Flush()
|
||||||
})
|
}, "wp-routers")
|
||||||
regRoutes = safety.NewMap[string, *regexp.Regexp]()
|
regRoutes = safety.NewMap[string, *regexp.Regexp]()
|
||||||
return r
|
return r
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -113,11 +113,6 @@ func SetConfigHandle(a ...any) Handle {
|
||||||
|
|
||||||
var GetInitHandleFn = reload.BuildValFnWithAnyParams("themeArgAndConfig", SetConfigHandle, false)
|
var GetInitHandleFn = reload.BuildValFnWithAnyParams("themeArgAndConfig", SetConfigHandle, false)
|
||||||
|
|
||||||
type ConfigParm struct {
|
|
||||||
ConfigFn func(*Handle)
|
|
||||||
H *Handle
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitHandle(configFn func(*Handle), h *Handle) {
|
func InitHandle(configFn func(*Handle), h *Handle) {
|
||||||
hh := GetInitHandleFn(configFn, h)
|
hh := GetInitHandleFn(configFn, h)
|
||||||
mods, err := wpconfig.GetThemeMods(h.theme)
|
mods, err := wpconfig.GetThemeMods(h.theme)
|
||||||
|
|
|
@ -111,10 +111,10 @@ func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except .
|
||||||
var themeModes = func() *safety.Map[string, ThemeMods] {
|
var themeModes = func() *safety.Map[string, ThemeMods] {
|
||||||
m := safety.NewMap[string, ThemeMods]()
|
m := safety.NewMap[string, ThemeMods]()
|
||||||
themeModsRaw = safety.NewMap[string, map[string]any]()
|
themeModsRaw = safety.NewMap[string, map[string]any]()
|
||||||
reload.Push(func() {
|
reload.Append(func() {
|
||||||
m.Flush()
|
m.Flush()
|
||||||
themeModsRaw.Flush()
|
themeModsRaw.Flush()
|
||||||
})
|
}, "theme-modes")
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}()
|
}()
|
||||||
|
|
39
cache/reload/reload.go
vendored
39
cache/reload/reload.go
vendored
|
@ -2,10 +2,11 @@ package reload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fthvgb1/wp-go/helper"
|
"github.com/fthvgb1/wp-go/helper"
|
||||||
"github.com/fthvgb1/wp-go/helper/number"
|
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/safety"
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
type queue struct {
|
type queue struct {
|
||||||
|
@ -165,7 +166,7 @@ func BuildSafetyMap[K comparable, V, A any](namespace string, args ...any) *Safe
|
||||||
m.Val.Delete(key)
|
m.Val.Delete(key)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Push(m.Val.Flush, args...)
|
Append(m.Val.Flush, args...)
|
||||||
safetyMaps.Store(namespace, m)
|
safetyMaps.Store(namespace, m)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
@ -205,7 +206,7 @@ func BuildAnyVal[T, A any](namespace string, counter bool, args ...any) *SafetyV
|
||||||
v := Val[T]{}
|
v := Val[T]{}
|
||||||
vv = &SafetyVar[T, A]{safety.NewVar(v), sync.Mutex{}}
|
vv = &SafetyVar[T, A]{safety.NewVar(v), sync.Mutex{}}
|
||||||
args = append(args, namespace)
|
args = append(args, namespace)
|
||||||
Push(vv.Val.Flush, args...)
|
Append(vv.Val.Flush, args...)
|
||||||
safetyMaps.Store(namespace, vv)
|
safetyMaps.Store(namespace, vv)
|
||||||
return vv
|
return vv
|
||||||
}
|
}
|
||||||
|
@ -233,9 +234,11 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) (T, bool), args ..
|
||||||
func BuildValFnWithConfirm[T, A any](namespace string, fn func(A) (T, bool), args ...any) func(A) T {
|
func BuildValFnWithConfirm[T, A any](namespace string, fn func(A) (T, bool), args ...any) func(A) T {
|
||||||
var vv = BuildAnyVal[T, A](namespace, false, args...)
|
var vv = BuildAnyVal[T, A](namespace, false, args...)
|
||||||
tryTimes := helper.ParseArgs(1, args...)
|
tryTimes := helper.ParseArgs(1, args...)
|
||||||
var counter func() int
|
var counter int64
|
||||||
if tryTimes > 1 {
|
if tryTimes > 1 {
|
||||||
counter = number.Counters[int]()
|
Append(func() {
|
||||||
|
atomic.StoreInt64(&counter, 0)
|
||||||
|
}, str.Join("reload-valFn-counter-", namespace))
|
||||||
}
|
}
|
||||||
return func(a A) T {
|
return func(a A) T {
|
||||||
v := vv.Val.Load()
|
v := vv.Val.Load()
|
||||||
|
@ -253,11 +256,11 @@ func BuildValFnWithConfirm[T, A any](namespace string, fn func(A) (T, bool), arg
|
||||||
vv.Val.Store(v)
|
vv.Val.Store(v)
|
||||||
return v.V
|
return v.V
|
||||||
}
|
}
|
||||||
if counter == nil {
|
if atomic.LoadInt64(&counter) <= 1 {
|
||||||
return v.V
|
return v.V
|
||||||
}
|
}
|
||||||
times := counter()
|
atomic.AddInt64(&counter, 1)
|
||||||
if times >= tryTimes {
|
if atomic.LoadInt64(&counter) >= int64(tryTimes) {
|
||||||
v.Ok = true
|
v.Ok = true
|
||||||
vv.Val.Store(v)
|
vv.Val.Store(v)
|
||||||
}
|
}
|
||||||
|
@ -316,7 +319,7 @@ func BuildValFnWithAnyParams[T any](namespace string, fn func(...any) T, args ..
|
||||||
|
|
||||||
// Vars get default value and whenever reloaded assign default value
|
// Vars get default value and whenever reloaded assign default value
|
||||||
//
|
//
|
||||||
// args same as Push
|
// args same as Append
|
||||||
//
|
//
|
||||||
// if give a name, then can be flushed by calls Reloads
|
// if give a name, then can be flushed by calls Reloads
|
||||||
//
|
//
|
||||||
|
@ -326,7 +329,7 @@ func BuildValFnWithAnyParams[T any](namespace string, fn func(...any) T, args ..
|
||||||
func Vars[T any](defaults T, args ...any) *safety.Var[T] {
|
func Vars[T any](defaults T, args ...any) *safety.Var[T] {
|
||||||
ss := safety.NewVar(defaults)
|
ss := safety.NewVar(defaults)
|
||||||
|
|
||||||
Push(func() {
|
Append(func() {
|
||||||
ss.Store(defaults)
|
ss.Store(defaults)
|
||||||
}, args...)
|
}, args...)
|
||||||
return ss
|
return ss
|
||||||
|
@ -350,11 +353,11 @@ func parseArgs(a ...any) (ord float64, name string) {
|
||||||
|
|
||||||
// VarsBy
|
// VarsBy
|
||||||
//
|
//
|
||||||
// args same as Push
|
// args same as Append
|
||||||
// if give a name, then can be flushed by calls Reloads
|
// if give a name, then can be flushed by calls Reloads
|
||||||
func VarsBy[T any](fn func() T, args ...any) *safety.Var[T] {
|
func VarsBy[T any](fn func() T, args ...any) *safety.Var[T] {
|
||||||
ss := safety.NewVar(fn())
|
ss := safety.NewVar(fn())
|
||||||
Push(func() {
|
Append(func() {
|
||||||
ss.Store(fn())
|
ss.Store(fn())
|
||||||
}, args...)
|
}, args...)
|
||||||
return ss
|
return ss
|
||||||
|
@ -364,7 +367,7 @@ func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), args ...any) *safety
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
fn(m)
|
fn(m)
|
||||||
}
|
}
|
||||||
Push(func() {
|
Append(func() {
|
||||||
m.Flush()
|
m.Flush()
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
fn(m)
|
fn(m)
|
||||||
|
@ -375,18 +378,18 @@ func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), args ...any) *safety
|
||||||
|
|
||||||
func SafeMap[K comparable, T any](args ...any) *safety.Map[K, T] {
|
func SafeMap[K comparable, T any](args ...any) *safety.Map[K, T] {
|
||||||
m := safety.NewMap[K, T]()
|
m := safety.NewMap[K, T]()
|
||||||
Push(m.Flush, args...)
|
Append(m.Flush, args...)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push the func that will be called whenever Reload called
|
// Append the func that will be called whenever Reload called
|
||||||
//
|
//
|
||||||
// if give a name, then can be called by called Reloads
|
// if give a name, then can be called by called Reloads
|
||||||
//
|
//
|
||||||
// if give a float then can be called early or lately when called Reload, more bigger more earlier
|
// if give a float then can be called early or lately when called Reload, more bigger more earlier
|
||||||
//
|
//
|
||||||
// if give a bool false will not flushed when called Reload, then can called GetValMap to flush manually
|
// if give a bool false will not flushed when called Reload, then can called GetValMap to flush manually
|
||||||
func Push(fn func(), a ...any) {
|
func Append(fn func(), a ...any) {
|
||||||
ord, name := parseArgs(a...)
|
ord, name := parseArgs(a...)
|
||||||
auto := helper.ParseArgs(true, a...)
|
auto := helper.ParseArgs(true, a...)
|
||||||
if name != "" && !auto {
|
if name != "" && !auto {
|
||||||
|
@ -434,11 +437,11 @@ func BuildFnVal[T any](name string, t T, fn func() T) func() T {
|
||||||
v: p,
|
v: p,
|
||||||
isManual: safety.NewVar(false),
|
isManual: safety.NewVar(false),
|
||||||
}
|
}
|
||||||
Push(func() {
|
Append(func() {
|
||||||
if !e.isManual.Load() {
|
if !e.isManual.Load() {
|
||||||
e.v.Store(fn())
|
e.v.Store(fn())
|
||||||
}
|
}
|
||||||
})
|
}, str.Join("fnval-", name))
|
||||||
setFnVal.Store(name, e)
|
setFnVal.Store(name, e)
|
||||||
return func() T {
|
return func() T {
|
||||||
return e.v.Load()
|
return e.v.Load()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user