优化代码,添加两个配置
This commit is contained in:
parent
ef71192add
commit
cd636e2dc9
|
@ -60,13 +60,13 @@ cacheTime:
|
|||
commentsCacheTime: 24h
|
||||
# 主题的页眉图片缓存时间
|
||||
themeHeaderImagCacheTime: 5m
|
||||
# 随机sleep时间
|
||||
sleepTime: [ 1s,3s ]
|
||||
# 摘要字数
|
||||
digestWordCount: 300
|
||||
|
||||
# 到达指定并发请求数时随机sleep
|
||||
maxRequestSleepNum: 100
|
||||
# 随机sleep时间
|
||||
sleepTime: [1s,3s]
|
||||
# 全局最大请求数,超过直接403
|
||||
maxRequestNum: 500
|
||||
# 单ip同时最大搜索请求数
|
||||
|
@ -79,6 +79,10 @@ gzip: false
|
|||
postCommentUrl: http://wp.test/wp-comments-post.php
|
||||
# TrustIps
|
||||
trustIps: []
|
||||
# 分页器间隔数
|
||||
paginationStep: 1
|
||||
# 显示查询的sql语句
|
||||
showQuerySql: false
|
||||
# trust servername 信任的域名
|
||||
trustServerNames: ["xy.test","blog.xy.test"]
|
||||
# 主题 为空值为option template,没有就默认为twentyfifteen
|
||||
|
|
|
@ -2,18 +2,18 @@ package maps
|
|||
|
||||
import "strings"
|
||||
|
||||
func GetStrMapAnyVal[T any](key string, v map[string]any) (r T, o bool) {
|
||||
func GetStrMapAnyVal[T any](m map[string]any, key string) (r T, o bool) {
|
||||
k := strings.Split(key, ".")
|
||||
if len(k) > 1 {
|
||||
val, ok := v[k[0]]
|
||||
val, ok := m[k[0]]
|
||||
if ok {
|
||||
vx, ok := val.(map[string]any)
|
||||
if ok {
|
||||
r, o = GetStrMapAnyVal[T](strings.Join(k[1:], "."), vx)
|
||||
r, o = GetStrMapAnyVal[T](vx, strings.Join(k[1:], "."))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
x, ok := v[k[0]]
|
||||
x, ok := m[k[0]]
|
||||
if ok {
|
||||
vv, ok := x.(T)
|
||||
if ok {
|
||||
|
|
|
@ -54,7 +54,7 @@ func TestGetStrMapAnyVal(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotR, gotO := GetStrMapAnyVal[int](tt.args.key, tt.args.v)
|
||||
gotR, gotO := GetStrMapAnyVal[int](tt.args.v, tt.args.key)
|
||||
if !reflect.DeepEqual(gotR, tt.wantR) {
|
||||
t.Errorf("GetStrMapAnyVal() gotR = %v, want %v", gotR, tt.wantR)
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ func initConf(c string) (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
model.InitDB(model.NewSqlxQuery(database))
|
||||
model.InitDB(db.QueryDb(database))
|
||||
err = wpconfig.InitOptions()
|
||||
if err != nil {
|
||||
return
|
||||
|
|
6
internal/pkg/cache/cache.go
vendored
6
internal/pkg/cache/cache.go
vendored
|
@ -6,10 +6,10 @@ import (
|
|||
"github.com/fthvgb1/wp-go/helper"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/dao"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
@ -162,7 +162,7 @@ func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
|
|||
logs.ErrPrintln(err, "get category err")
|
||||
if len(t) > 0 {
|
||||
return slice.Filter(r, func(my models.TermsMy) bool {
|
||||
return helper.Or(t[0] == plugins.Tag, "post_tag", "category") == my.Taxonomy
|
||||
return helper.Or(t[0] == constraints.Tag, "post_tag", "category") == my.Taxonomy
|
||||
})
|
||||
}
|
||||
return r
|
||||
|
@ -171,7 +171,7 @@ func AllCategoryTagsNames(ctx context.Context, c int) map[string]struct{} {
|
|||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||
if helper.Or(c == plugins.Tag, "post_tag", "category") == t.Taxonomy {
|
||||
if helper.Or(c == constraints.Tag, "post_tag", "category") == t.Taxonomy {
|
||||
return t.Name, struct{}{}, true
|
||||
}
|
||||
return "", struct{}{}, false
|
||||
|
|
|
@ -32,6 +32,8 @@ type Config struct {
|
|||
UploadDir string `yaml:"uploadDir"`
|
||||
Pprof string `yaml:"pprof"`
|
||||
ListPagePlugins []string `yaml:"listPagePlugins"`
|
||||
PaginationStep int `yaml:"paginationStep"`
|
||||
ShowQuerySql bool `yaml:"showQuerySql"`
|
||||
}
|
||||
|
||||
type CacheTime struct {
|
||||
|
|
16
internal/pkg/constraints/plugins.go
Normal file
16
internal/pkg/constraints/plugins.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package constraints
|
||||
|
||||
const (
|
||||
Home = iota + 1
|
||||
Archive
|
||||
Category
|
||||
Tag
|
||||
Search
|
||||
Author
|
||||
Detail
|
||||
|
||||
Ok
|
||||
Empty404
|
||||
ParamError
|
||||
InternalErr
|
||||
)
|
|
@ -1,9 +1,12 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"log"
|
||||
)
|
||||
|
||||
var db *sqlx.DB
|
||||
|
@ -30,3 +33,23 @@ func InitDb() (*sqlx.DB, error) {
|
|||
}
|
||||
return db, err
|
||||
}
|
||||
|
||||
func QueryDb(db *sqlx.DB) model.UniversalDb {
|
||||
query := model.NewUniversalDb(
|
||||
|
||||
func(ctx context.Context, a any, s string, args ...any) error {
|
||||
if config.GetConfig().ShowQuerySql {
|
||||
go log.Println(model.FormatSql(s, args...))
|
||||
}
|
||||
return db.Select(a, s, args...)
|
||||
},
|
||||
|
||||
func(ctx context.Context, a any, s string, args ...any) error {
|
||||
if config.GetConfig().ShowQuerySql {
|
||||
go log.Println(model.FormatSql(s, args...))
|
||||
}
|
||||
return db.Get(a, s, args...)
|
||||
})
|
||||
|
||||
return query
|
||||
}
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
package plugins
|
||||
|
||||
const (
|
||||
Home = iota + 1
|
||||
Archive
|
||||
Category
|
||||
Tag
|
||||
Search
|
||||
Detail
|
||||
|
||||
Ok
|
||||
Empty404
|
||||
Error
|
||||
InternalErr
|
||||
)
|
||||
|
||||
var IndexSceneMap = map[int]struct{}{
|
||||
Home: {},
|
||||
Archive: {},
|
||||
Category: {},
|
||||
Tag: {},
|
||||
Search: {},
|
||||
}
|
|
@ -10,11 +10,13 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Handle struct {
|
||||
C *gin.Context
|
||||
Session sessions.Session
|
||||
GinH gin.H
|
||||
Password string
|
||||
Scene int
|
||||
|
@ -31,13 +33,22 @@ func (h Handle) Index() {
|
|||
}
|
||||
|
||||
func (h Handle) ExecListPagePlugin(m map[string]Plugin[models.Posts], calls ...func(*models.Posts)) {
|
||||
|
||||
pluginConf := config.GetConfig().ListPagePlugins
|
||||
plugin := GetPlugins(pluginConf, m)
|
||||
h.GinH["posts"] = slice.Map(
|
||||
h.GinH["posts"].([]models.Posts),
|
||||
PluginFn[models.Posts](plugin, h, Defaults(calls...)))
|
||||
|
||||
plugin := GetListPostPlugins(pluginConf, m)
|
||||
|
||||
posts, ok := maps.GetStrMapAnyVal[[]models.Posts](h.GinH, "posts")
|
||||
|
||||
if ok {
|
||||
h.GinH["posts"] = slice.Map(posts, PluginFn[models.Posts](plugin, h, Defaults(calls...)))
|
||||
}
|
||||
}
|
||||
|
||||
/*func (h Handle) Pagination(paginate pagination) {
|
||||
|
||||
}*/
|
||||
|
||||
type Fn[T any] func(T) T
|
||||
type Plugin[T any] func(next Fn[T], h Handle, t T) T
|
||||
|
||||
|
@ -54,7 +65,7 @@ var pluginFns = map[string]Plugin[models.Posts]{
|
|||
"digest": Digest,
|
||||
}
|
||||
|
||||
func Plugins() map[string]Plugin[models.Posts] {
|
||||
func ListPostPlugins() map[string]Plugin[models.Posts] {
|
||||
return maps.Copy(pluginFns)
|
||||
}
|
||||
|
||||
|
@ -71,7 +82,7 @@ func Default[T any](t T) T {
|
|||
return t
|
||||
}
|
||||
|
||||
func GetPlugins(name []string, m map[string]Plugin[models.Posts]) []Plugin[models.Posts] {
|
||||
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts]) []Plugin[models.Posts] {
|
||||
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts], bool) {
|
||||
v, ok := m[t]
|
||||
if ok {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package twentyfifteen
|
||||
|
||||
import (
|
||||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||
|
@ -20,30 +22,27 @@ func Hook(h2 common.Handle) {
|
|||
templ: "twentyfifteen/posts/index.gohtml",
|
||||
}
|
||||
//h.GinH["HeaderImage"] = h.getHeaderImage(h.C)
|
||||
if h.Stats == plugins.Empty404 {
|
||||
if h.Stats == constraints.Empty404 {
|
||||
h.C.HTML(h.Code, h.templ, h.GinH)
|
||||
return
|
||||
}
|
||||
if h.Scene == plugins.Detail {
|
||||
if h.Scene == constraints.Detail {
|
||||
h.Detail()
|
||||
return
|
||||
}
|
||||
h.Index()
|
||||
}
|
||||
|
||||
var plugin = common.Plugins()
|
||||
var plugin = common.ListPostPlugins()
|
||||
|
||||
func (h handle) Index() {
|
||||
if h.Stats != plugins.Empty404 {
|
||||
if h.Stats != constraints.Empty404 {
|
||||
|
||||
h.ExecListPagePlugin(plugin)
|
||||
|
||||
p, ok := h.GinH["pagination"]
|
||||
page, ok := maps.GetStrMapAnyVal[pagination.ParsePagination](h.GinH, "pagination")
|
||||
if ok {
|
||||
pp, ok := p.(pagination.ParsePagination)
|
||||
if ok {
|
||||
h.GinH["pagination"] = pagination.Paginate(plugins.TwentyFifteenPagination(), pp)
|
||||
}
|
||||
h.GinH["pagination"] = pagination.Paginate(plugins.TwentyFifteenPagination(), page)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
|
@ -38,7 +39,7 @@ func Hook(cHandle common.Handle) {
|
|||
templ: "twentyseventeen/posts/index.gohtml",
|
||||
}
|
||||
h.GinH["HeaderImage"] = h.getHeaderImage(h.C)
|
||||
if h.Scene == plugins.Detail {
|
||||
if h.Scene == constraints.Detail {
|
||||
h.Detail()
|
||||
return
|
||||
}
|
||||
|
@ -46,20 +47,17 @@ func Hook(cHandle common.Handle) {
|
|||
}
|
||||
|
||||
var pluginFns = func() map[string]common.Plugin[models.Posts] {
|
||||
return maps.Merge(common.Plugins(), map[string]common.Plugin[models.Posts]{
|
||||
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts]{
|
||||
"twentyseventeen_postThumbnail": postThumbnail,
|
||||
})
|
||||
}()
|
||||
|
||||
func (h handle) Index() {
|
||||
if h.Stats != plugins.Empty404 {
|
||||
if h.Stats != constraints.Empty404 {
|
||||
h.ExecListPagePlugin(pluginFns)
|
||||
p, ok := h.GinH["pagination"]
|
||||
page, ok := maps.GetStrMapAnyVal[pagination.ParsePagination](h.GinH, "pagination")
|
||||
if ok {
|
||||
pp, ok := p.(pagination.ParsePagination)
|
||||
if ok {
|
||||
h.GinH["pagination"] = pagination.Paginate(paginate, pp)
|
||||
}
|
||||
h.GinH["pagination"] = pagination.Paginate(paginate, page)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,7 +68,7 @@ func (h handle) Index() {
|
|||
func (h handle) Detail() {
|
||||
post := h.GinH["post"].(models.Posts)
|
||||
h.GinH["bodyClass"] = h.bodyClass()
|
||||
if h.Stats == plugins.Empty404 {
|
||||
if h.Stats == constraints.Empty404 {
|
||||
h.C.HTML(h.Code, h.templ, h.GinH)
|
||||
return
|
||||
}
|
||||
|
@ -115,7 +113,7 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls
|
|||
func postThumbnail(next common.Fn[models.Posts], h common.Handle, t models.Posts) models.Posts {
|
||||
if t.Thumbnail.Path != "" {
|
||||
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
|
||||
if h.Scene == plugins.Detail {
|
||||
if h.Scene == constraints.Detail {
|
||||
t.Thumbnail.Sizes = "100vw"
|
||||
}
|
||||
}
|
||||
|
@ -139,16 +137,16 @@ func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
|
|||
|
||||
func (h handle) bodyClass() string {
|
||||
s := ""
|
||||
if h.Stats == plugins.Empty404 {
|
||||
if h.Stats == constraints.Empty404 {
|
||||
return "error404"
|
||||
}
|
||||
switch h.Scene {
|
||||
case plugins.Search:
|
||||
case constraints.Search:
|
||||
s = "search-no-results"
|
||||
if len(h.GinH["posts"].([]models.Posts)) > 0 {
|
||||
s = "search-results"
|
||||
}
|
||||
case plugins.Category, plugins.Tag:
|
||||
case constraints.Category, constraints.Tag:
|
||||
cat := h.C.Param("category")
|
||||
if cat == "" {
|
||||
cat = h.C.Param("tag")
|
||||
|
@ -160,17 +158,17 @@ func (h handle) bodyClass() string {
|
|||
s = cate.Slug
|
||||
}
|
||||
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
|
||||
case plugins.Detail:
|
||||
case constraints.Detail:
|
||||
s = fmt.Sprintf("postid-%d", h.GinH["post"].(models.Posts).Id)
|
||||
}
|
||||
return str.Join(class[h.Scene], s)
|
||||
}
|
||||
|
||||
var class = map[int]string{
|
||||
plugins.Home: "home blog ",
|
||||
plugins.Archive: "archive date page-two-column",
|
||||
plugins.Category: "archive category page-two-column",
|
||||
plugins.Tag: "archive category page-two-column ",
|
||||
plugins.Search: "search ",
|
||||
plugins.Detail: "post-template-default single single-post single-format-standard ",
|
||||
constraints.Home: "home blog ",
|
||||
constraints.Archive: "archive date page-two-column",
|
||||
constraints.Category: "archive category page-two-column",
|
||||
constraints.Tag: "archive category page-two-column ",
|
||||
constraints.Search: "search ",
|
||||
constraints.Detail: "post-template-default single single-post single-format-standard ",
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ func InitDB(db dbQuery) {
|
|||
globalBb = db
|
||||
}
|
||||
|
||||
type QueryFn func(context.Context, any, string, ...any) error
|
||||
|
||||
type Model interface {
|
||||
PrimaryKey() string
|
||||
Table() string
|
||||
|
|
|
@ -20,19 +20,19 @@ func NewSqlxQuery(sqlx *sqlx.DB) SqlxQuery {
|
|||
|
||||
func (r SqlxQuery) Select(ctx context.Context, dest any, sql string, params ...any) error {
|
||||
if os.Getenv("SHOW_SQL") == "true" {
|
||||
go log.Println(formatSql(sql, params))
|
||||
go log.Println(FormatSql(sql, params...))
|
||||
}
|
||||
return r.sqlx.Select(dest, sql, params...)
|
||||
}
|
||||
|
||||
func (r SqlxQuery) Get(ctx context.Context, dest any, sql string, params ...any) error {
|
||||
if os.Getenv("SHOW_SQL") == "true" {
|
||||
go log.Println(formatSql(sql, params))
|
||||
go log.Println(FormatSql(sql, params...))
|
||||
}
|
||||
return r.sqlx.Get(dest, sql, params...)
|
||||
}
|
||||
|
||||
func formatSql(sql string, params []any) string {
|
||||
func FormatSql(sql string, params ...any) string {
|
||||
for _, param := range params {
|
||||
switch param.(type) {
|
||||
case string:
|
||||
|
|
20
model/universal.go
Normal file
20
model/universal.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package model
|
||||
|
||||
import "context"
|
||||
|
||||
type UniversalDb struct {
|
||||
selects QueryFn
|
||||
gets QueryFn
|
||||
}
|
||||
|
||||
func NewUniversalDb(selects QueryFn, gets QueryFn) UniversalDb {
|
||||
return UniversalDb{selects: selects, gets: gets}
|
||||
}
|
||||
|
||||
func (u UniversalDb) Select(ctx context.Context, a any, s string, args ...any) error {
|
||||
return u.selects(ctx, a, s, args...)
|
||||
}
|
||||
|
||||
func (u UniversalDb) Get(ctx context.Context, a any, s string, args ...any) error {
|
||||
return u.gets(ctx, a, s, args...)
|
||||
}
|
Loading…
Reference in New Issue
Block a user