db reload and fix bug

This commit is contained in:
xing 2023-04-05 20:54:38 +08:00
parent 78f0c0a87a
commit c09f54b9c0
9 changed files with 38 additions and 35 deletions

6
go.mod
View File

@ -33,9 +33,9 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/net v0.4.0 // indirect golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.3.0 // indirect golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.5.0 // indirect golang.org/x/text v0.6.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect

12
go.sum
View File

@ -96,22 +96,22 @@ golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6Zu
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=

View File

@ -16,7 +16,6 @@ import (
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
"log" "log"
"math/rand"
"os" "os"
"os/signal" "os/signal"
"regexp" "regexp"
@ -39,7 +38,6 @@ func init() {
if intReg.MatchString(address) && !strings.Contains(address, ":") { if intReg.MatchString(address) && !strings.Contains(address, ":") {
address = ":" + address address = ":" + address
} }
rand.Seed(time.Now().UnixNano())
err := initConf(confPath) err := initConf(confPath)
if err != nil { if err != nil {
panic(err) panic(err)
@ -101,6 +99,8 @@ func reloads() {
}() }()
err := config.InitConfig(confPath) err := config.InitConfig(confPath)
logs.ErrPrintln(err, "获取配置文件失败", confPath) logs.ErrPrintln(err, "获取配置文件失败", confPath)
_, err = db.InitDb()
logs.ErrPrintln(err, "重新读取db失败", config.GetConfig().Mysql)
err = wpconfig.InitOptions() err = wpconfig.InitOptions()
logs.ErrPrintln(err, "获取网站设置WpOption失败") logs.ErrPrintln(err, "获取网站设置WpOption失败")
err = wpconfig.InitTerms() err = wpconfig.InitTerms()

View File

@ -10,9 +10,9 @@ import (
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"os"
"runtime" "runtime"
"strings" "strings"
"time" "time"
@ -92,7 +92,7 @@ func stack(skip int) []byte {
// Print this much at least. If we can't find the source, it won't show. // Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile { if file != lastFile {
data, err := ioutil.ReadFile(file) data, err := os.ReadFile(file)
if err != nil { if err != nil {
continue continue
} }

View File

@ -4,18 +4,18 @@ import (
"context" "context"
"github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/safety"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"log" "log"
) )
var db *sqlx.DB var safeDb = safety.NewVar[*sqlx.DB](nil)
func InitDb() (*sqlx.DB, error) { func InitDb() (*safety.Var[*sqlx.DB], error) {
c := config.GetConfig() c := config.GetConfig()
dsn := c.Mysql.Dsn.GetDsn() dsn := c.Mysql.Dsn.GetDsn()
var err error db, err := sqlx.Open("mysql", dsn)
db, err = sqlx.Open("mysql", dsn)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -31,14 +31,14 @@ func InitDb() (*sqlx.DB, error) {
if c.Mysql.Pool.ConnMaxLifetime != 0 { if c.Mysql.Pool.ConnMaxLifetime != 0 {
db.SetConnMaxLifetime(c.Mysql.Pool.ConnMaxLifetime) db.SetConnMaxLifetime(c.Mysql.Pool.ConnMaxLifetime)
} }
return db, err safeDb.Store(db)
return safeDb, err
} }
func QueryDb(db *sqlx.DB) *model.SqlxQuery { func QueryDb(db *safety.Var[*sqlx.DB]) *model.SqlxQuery {
query := model.NewSqlxQuery(db, model.NewUniversalDb( query := model.NewSqlxQuery(db, model.NewUniversalDb(
nil, nil,
nil)) nil))
model.SetSelect(query, func(ctx context.Context, a any, s string, args ...any) error { model.SetSelect(query, func(ctx context.Context, a any, s string, args ...any) error {
if config.GetConfig().ShowQuerySql { if config.GetConfig().ShowQuerySql {
go log.Println(model.FormatSql(s, args...)) go log.Println(model.FormatSql(s, args...))

View File

@ -161,6 +161,7 @@ func categoryUl(h *wp.Handle, categories []models.TermsMy, conf map[any]any) str
func dropdown(h *wp.Handle, categories []models.TermsMy, id int, args map[string]string, conf map[any]any) string { func dropdown(h *wp.Handle, categories []models.TermsMy, id int, args map[string]string, conf map[any]any) string {
s := str.NewBuilder() s := str.NewBuilder()
ids := fmt.Sprintf(`wp-block-categories-%v`, id) ids := fmt.Sprintf(`wp-block-categories-%v`, id)
args = maps.Copy(args)
args["{$selectId}"] = ids args["{$selectId}"] = ids
attrs := reload.GetAnyValBys("block-category-attr", conf, parseAttr) attrs := reload.GetAnyValBys("block-category-attr", conf, parseAttr)
selects := widget.DropdownCategories(h, args, conf, categories) selects := widget.DropdownCategories(h, args, conf, categories)

View File

@ -57,7 +57,7 @@ func Archive(h *wp.Handle, id string) string {
args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args) args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args)
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive")) args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive"))
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { if conf["dropdown"].(int64) == 0 && slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string)) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string))
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }

View File

@ -63,6 +63,10 @@ func Category(h *wp.Handle, id string) string {
args = maps.FilterZeroMerge(categoryArgs(), CommonArgs(), commonArgs, args) args = maps.FilterZeroMerge(categoryArgs(), CommonArgs(), commonArgs, args)
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("categories-", id), str.Join("widget widget_", "categories")) args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("categories-", id), str.Join("widget widget_", "categories"))
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if conf["dropdown"].(int64) == 0 && slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{title}"])
args["{$navCloser}"] = "</nav>"
}
return args return args
}) })
@ -118,10 +122,6 @@ func CategoryLi(h *wp.Handle, conf map[any]any, categories []models.TermsMy) str
} }
func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string { func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{title}"])
args["{$navCloser}"] = "</nav>"
}
s := str.NewBuilder() s := str.NewBuilder()
s.WriteString("<ul>\n") s.WriteString("<ul>\n")
s.WriteString(CategoryLi(h, conf, categories)) s.WriteString(CategoryLi(h, conf, categories))

View File

@ -6,18 +6,18 @@ import (
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/safety"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"strconv" "strconv"
"strings" "strings"
) )
type SqlxQuery struct { type SqlxQuery struct {
sqlx *sqlx.DB sqlx *safety.Var[*sqlx.DB]
UniversalDb UniversalDb
} }
func NewSqlxQuery(sqlx *sqlx.DB, u UniversalDb) *SqlxQuery { func NewSqlxQuery(sqlx *safety.Var[*sqlx.DB], u UniversalDb) *SqlxQuery {
s := &SqlxQuery{sqlx: sqlx, UniversalDb: u} s := &SqlxQuery{sqlx: sqlx, UniversalDb: u}
if u.selects == nil { if u.selects == nil {
s.UniversalDb.selects = s.Selects s.UniversalDb.selects = s.Selects
@ -37,28 +37,30 @@ func SetGet(db *SqlxQuery, fn func(context.Context, any, string, ...any) error)
func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ...any) error { func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ...any) error {
v := helper.GetContextVal(ctx, "handle=>", "") v := helper.GetContextVal(ctx, "handle=>", "")
db := r.sqlx.Load()
if v != "" { if v != "" {
switch v { switch v {
case "string": case "string":
return ToMapSlice(r.sqlx, dest.(*[]map[string]string), sql, params...) return ToMapSlice(db, dest.(*[]map[string]string), sql, params...)
case "scanner": case "scanner":
fn := ctx.Value("fn") fn := ctx.Value("fn")
return Scanner[any](r.sqlx, dest, sql, params...)(fn.(func(any))) return Scanner[any](db, dest, sql, params...)(fn.(func(any)))
} }
} }
return r.sqlx.Select(dest, sql, params...) return db.Select(dest, sql, params...)
} }
func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error { func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error {
db := r.sqlx.Load()
v := helper.GetContextVal(ctx, "handle=>", "") v := helper.GetContextVal(ctx, "handle=>", "")
if v != "" { if v != "" {
switch v { switch v {
case "string": case "string":
return GetToMap(r.sqlx, dest.(*map[string]string), sql, params...) return GetToMap(db, dest.(*map[string]string), sql, params...)
} }
} }
return r.sqlx.Get(dest, sql, params...) return db.Get(dest, sql, params...)
} }
func Scanner[T any](db *sqlx.DB, v T, s string, params ...any) func(func(T)) error { func Scanner[T any](db *sqlx.DB, v T, s string, params ...any) func(func(T)) error {
@ -87,7 +89,7 @@ func ToMapSlice[V any](db *sqlx.DB, dest *[]map[string]V, sql string, params ...
defer rows.Close() defer rows.Close()
columnLen := len(columns) columnLen := len(columns)
c := make([]*V, columnLen) c := make([]*V, columnLen)
for i, _ := range c { for i := range c {
var a V var a V
c[i] = &a c[i] = &a
} }
@ -116,7 +118,7 @@ func GetToMap[V any](db *sqlx.DB, dest *map[string]V, sql string, params ...any)
} }
columnLen := len(columns) columnLen := len(columns)
c := make([]*V, columnLen) c := make([]*V, columnLen)
for i, _ := range c { for i := range c {
var a V var a V
c[i] = &a c[i] = &a
} }