错误日志路径及优化
This commit is contained in:
parent
1f3ca99441
commit
9e293e7f39
|
@ -69,8 +69,8 @@ maxRequestSleepNum: 100
|
||||||
maxRequestNum: 500
|
maxRequestNum: 500
|
||||||
# 单ip同时最大搜索请求数
|
# 单ip同时最大搜索请求数
|
||||||
singleIpSearchNum: 10
|
singleIpSearchNum: 10
|
||||||
|
# 错误日志输出路径
|
||||||
|
logOutput: err.log
|
||||||
# Gzip
|
# Gzip
|
||||||
gzip: false
|
gzip: false
|
||||||
# 提交评论url
|
# 提交评论url
|
||||||
|
|
|
@ -94,17 +94,17 @@ func PostComment(c *gin.Context) {
|
||||||
go func() {
|
go func() {
|
||||||
id := str.ToInteger[uint64](i, 0)
|
id := str.ToInteger[uint64](i, 0)
|
||||||
if id <= 0 {
|
if id <= 0 {
|
||||||
logs.ErrPrintln(err, "获取文档id", i)
|
logs.Error(errors.New("获取文档id错误"), "", i)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post, err := cache.GetPostById(cc, id)
|
post, err := cache.GetPostById(cc, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "获取文档", id)
|
logs.Error(err, "获取文档错误", id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.GetOption("siteurl"), author, m, post.PostTitle)
|
su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.GetOption("siteurl"), author, m, post.PostTitle)
|
||||||
err = mail.SendMail([]string{conf.Mail.User}, su, comment)
|
err = mail.SendMail([]string{conf.Mail.User}, su, comment)
|
||||||
logs.ErrPrintln(err, "发送邮件", conf.Mail.User, su, comment)
|
logs.IfError(err, "发送邮件", conf.Mail.User, su, comment)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s, er := io.ReadAll(ress.Body)
|
s, er := io.ReadAll(ress.Body)
|
||||||
|
|
|
@ -53,7 +53,10 @@ func initConf(c string) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
err = logs.InitLogger()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
database, err := db.InitDb()
|
database, err := db.InitDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -84,7 +87,7 @@ func flushCache() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
err := mail.SendMail([]string{config.GetConfig().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r))
|
err := mail.SendMail([]string{config.GetConfig().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r))
|
||||||
logs.ErrPrintln(err, "发邮件失败")
|
logs.IfError(err, "发邮件失败")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
cachemanager.Flush()
|
cachemanager.Flush()
|
||||||
|
@ -98,13 +101,15 @@ func reloads() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err := config.InitConfig(confPath)
|
err := config.InitConfig(confPath)
|
||||||
logs.ErrPrintln(err, "获取配置文件失败", confPath)
|
logs.IfError(err, "获取配置文件失败", confPath)
|
||||||
|
err = logs.InitLogger()
|
||||||
|
logs.IfError(err, "日志配置错误")
|
||||||
_, err = db.InitDb()
|
_, err = db.InitDb()
|
||||||
logs.ErrPrintln(err, "重新读取db失败", config.GetConfig().Mysql)
|
logs.IfError(err, "重新读取db失败", config.GetConfig().Mysql)
|
||||||
err = wpconfig.InitOptions()
|
err = wpconfig.InitOptions()
|
||||||
logs.ErrPrintln(err, "获取网站设置WpOption失败")
|
logs.IfError(err, "获取网站设置WpOption失败")
|
||||||
err = wpconfig.InitTerms()
|
err = wpconfig.InitTerms()
|
||||||
logs.ErrPrintln(err, "获取WpTerms表失败")
|
logs.IfError(err, "获取WpTerms表失败")
|
||||||
reload.Reload()
|
reload.Reload()
|
||||||
flushCache()
|
flushCache()
|
||||||
log.Println("reload complete")
|
log.Println("reload complete")
|
||||||
|
|
|
@ -48,7 +48,7 @@ func RecoverAndSendMail(w io.Writer) func(ctx *gin.Context) {
|
||||||
fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(wpconfig.GetOption("siteurl")), c.FullPath(), time.Now().Format(time.RFC1123Z)), content)
|
fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(wpconfig.GetOption("siteurl")), c.FullPath(), time.Now().Format(time.RFC1123Z)), content)
|
||||||
|
|
||||||
if er != nil {
|
if er != nil {
|
||||||
logs.ErrPrintln(er, "recover send mail fail", fmt.Sprintf("%v", err))
|
logs.IfError(er, "recover send mail fail", fmt.Sprintf("%v", err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
9
internal/pkg/cache/cache.go
vendored
9
internal/pkg/cache/cache.go
vendored
|
@ -103,7 +103,7 @@ func Archives(ctx context.Context) []models.PostArchive {
|
||||||
if l > 0 && a.month != m || l < 1 {
|
if l > 0 && a.month != m || l < 1 {
|
||||||
r, err := a.fn(ctx)
|
r, err := a.fn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "set cache err[%s]")
|
logs.Error(err, "set cache fail")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
a.month = m
|
a.month = m
|
||||||
|
@ -123,7 +123,7 @@ func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
|
||||||
tt = t[0]
|
tt = t[0]
|
||||||
}
|
}
|
||||||
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
|
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
|
||||||
logs.ErrPrintln(err, "get category err")
|
logs.IfError(err, "get category fail")
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
func AllCategoryTagsNames(ctx context.Context, t ...int) map[string]struct{} {
|
func AllCategoryTagsNames(ctx context.Context, t ...int) map[string]struct{} {
|
||||||
|
@ -132,7 +132,10 @@ func AllCategoryTagsNames(ctx context.Context, t ...int) map[string]struct{} {
|
||||||
tt = t[0]
|
tt = t[0]
|
||||||
}
|
}
|
||||||
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
|
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
|
||||||
logs.ErrPrintln(err, "get category err")
|
if err != nil {
|
||||||
|
logs.Error(err, "get category fail")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return slice.ToMap(r, func(t models.TermsMy) (string, struct{}) {
|
return slice.ToMap(r, func(t models.TermsMy) (string, struct{}) {
|
||||||
return t.Name, struct{}{}
|
return t.Name, struct{}{}
|
||||||
}, true)
|
}, true)
|
||||||
|
|
2
internal/pkg/cache/comments.go
vendored
2
internal/pkg/cache/comments.go
vendored
|
@ -15,7 +15,7 @@ func RecentComments(ctx context.Context, n int) (r []models.Comments) {
|
||||||
if len(r) > n {
|
if len(r) > n {
|
||||||
r = r[0:n]
|
r = r[0:n]
|
||||||
}
|
}
|
||||||
logs.ErrPrintln(err, "get recent comment")
|
logs.IfError(err, "get recent comment fail")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
internal/pkg/cache/feed.go
vendored
2
internal/pkg/cache/feed.go
vendored
|
@ -96,7 +96,7 @@ func postFeed(arg ...any) (x string, err error) {
|
||||||
id := arg[1].(string)
|
id := arg[1].(string)
|
||||||
ID := str.ToInteger[uint64](id, 0)
|
ID := str.ToInteger[uint64](id, 0)
|
||||||
maxId, err := GetMaxPostId(c)
|
maxId, err := GetMaxPostId(c)
|
||||||
logs.ErrPrintln(err, "get max post id")
|
logs.IfError(err, "get max post id")
|
||||||
if ID < 1 || ID > maxId || err != nil {
|
if ID < 1 || ID > maxId || err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
2
internal/pkg/cache/posts.go
vendored
2
internal/pkg/cache/posts.go
vendored
|
@ -53,7 +53,7 @@ func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
|
||||||
if n < len(r) {
|
if n < len(r) {
|
||||||
r = r[:n]
|
r = r[:n]
|
||||||
}
|
}
|
||||||
logs.ErrPrintln(err, "get recent post")
|
logs.IfError(err, "get recent post")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
internal/pkg/cache/users.go
vendored
2
internal/pkg/cache/users.go
vendored
|
@ -25,6 +25,6 @@ func GetAllUsername(ctx context.Context) (map[string]struct{}, error) {
|
||||||
|
|
||||||
func GetUserById(ctx context.Context, uid uint64) models.Users {
|
func GetUserById(ctx context.Context, uid uint64) models.Users {
|
||||||
r, err := usersCache.GetCache(ctx, uid, time.Second, ctx, uid)
|
r, err := usersCache.GetCache(ctx, uid, time.Second, ctx, uid)
|
||||||
logs.ErrPrintln(err, "get user", uid)
|
logs.IfError(err, "get user", uid)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,65 +15,66 @@ func GetConfig() Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Ssl Ssl `yaml:"ssl"`
|
Ssl Ssl `yaml:"ssl" json:"ssl"`
|
||||||
Mysql Mysql `yaml:"mysql"`
|
Mysql Mysql `yaml:"mysql" json:"mysql"`
|
||||||
Mail Mail `yaml:"mail"`
|
Mail Mail `yaml:"mail" json:"mail"`
|
||||||
CacheTime CacheTime `yaml:"cacheTime"`
|
CacheTime CacheTime `yaml:"cacheTime" json:"cacheTime"`
|
||||||
DigestWordCount int `yaml:"digestWordCount"`
|
DigestWordCount int `yaml:"digestWordCount" json:"digestWordCount,omitempty"`
|
||||||
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"`
|
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum" json:"maxRequestSleepNum,omitempty"`
|
||||||
MaxRequestNum int64 `yaml:"maxRequestNum"`
|
MaxRequestNum int64 `yaml:"maxRequestNum" json:"maxRequestNum,omitempty"`
|
||||||
SingleIpSearchNum int64 `yaml:"singleIpSearchNum"`
|
SingleIpSearchNum int64 `yaml:"singleIpSearchNum" json:"singleIpSearchNum,omitempty"`
|
||||||
Gzip bool `yaml:"gzip"`
|
Gzip bool `yaml:"gzip" json:"gzip,omitempty"`
|
||||||
PostCommentUrl string `yaml:"postCommentUrl"`
|
PostCommentUrl string `yaml:"postCommentUrl" json:"postCommentUrl,omitempty"`
|
||||||
TrustIps []string `yaml:"trustIps"`
|
TrustIps []string `yaml:"trustIps" json:"trustIps,omitempty"`
|
||||||
TrustServerNames []string `yaml:"trustServerNames"`
|
TrustServerNames []string `yaml:"trustServerNames" json:"trustServerNames,omitempty"`
|
||||||
Theme string `yaml:"theme"`
|
Theme string `yaml:"theme" json:"theme,omitempty"`
|
||||||
PostOrder string `yaml:"postOrder"`
|
PostOrder string `yaml:"postOrder" json:"postOrder,omitempty"`
|
||||||
UploadDir string `yaml:"uploadDir"`
|
UploadDir string `yaml:"uploadDir" json:"uploadDir,omitempty"`
|
||||||
Pprof string `yaml:"pprof"`
|
Pprof string `yaml:"pprof" json:"pprof,omitempty"`
|
||||||
ListPagePlugins []string `yaml:"listPagePlugins"`
|
ListPagePlugins []string `yaml:"listPagePlugins" json:"listPagePlugins,omitempty"`
|
||||||
PaginationStep int `yaml:"paginationStep"`
|
PaginationStep int `yaml:"paginationStep" json:"paginationStep,omitempty"`
|
||||||
ShowQuerySql bool `yaml:"showQuerySql"`
|
ShowQuerySql bool `yaml:"showQuerySql" json:"showQuerySql,omitempty"`
|
||||||
Plugins []string `yaml:"plugins"`
|
Plugins []string `yaml:"plugins" json:"plugins,omitempty"`
|
||||||
|
LogOutput string `yaml:"logOutput" json:"logOutput,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheTime struct {
|
type CacheTime struct {
|
||||||
CacheControl time.Duration `yaml:"cacheControl"`
|
CacheControl time.Duration `yaml:"cacheControl" json:"cacheControl,omitempty"`
|
||||||
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
|
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime" json:"recentPostCacheTime,omitempty"`
|
||||||
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
|
CategoryCacheTime time.Duration `yaml:"categoryCacheTime" json:"categoryCacheTime,omitempty"`
|
||||||
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
|
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime" json:"archiveCacheTime,omitempty"`
|
||||||
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
|
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime" json:"contextPostCacheTime,omitempty"`
|
||||||
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
|
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime" json:"recentCommentsCacheTime,omitempty"`
|
||||||
DigestCacheTime time.Duration `yaml:"digestCacheTime"`
|
DigestCacheTime time.Duration `yaml:"digestCacheTime" json:"digestCacheTime,omitempty"`
|
||||||
PostListCacheTime time.Duration `yaml:"postListCacheTime"`
|
PostListCacheTime time.Duration `yaml:"postListCacheTime" json:"postListCacheTime,omitempty"`
|
||||||
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
|
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime" json:"searchPostCacheTime,omitempty"`
|
||||||
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
|
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime" json:"monthPostCacheTime,omitempty"`
|
||||||
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
|
PostDataCacheTime time.Duration `yaml:"postDataCacheTime" json:"postDataCacheTime,omitempty"`
|
||||||
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
|
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime" json:"postCommentsCacheTime,omitempty"`
|
||||||
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
|
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime" json:"crontabClearCacheTime,omitempty"`
|
||||||
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
|
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime" json:"maxPostIdCacheTime,omitempty"`
|
||||||
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
|
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime" json:"userInfoCacheTime,omitempty"`
|
||||||
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
|
CommentsCacheTime time.Duration `yaml:"commentsCacheTime" json:"commentsCacheTime,omitempty"`
|
||||||
SleepTime []time.Duration `yaml:"sleepTime"`
|
SleepTime []time.Duration `yaml:"sleepTime" json:"sleepTime,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ssl struct {
|
type Ssl struct {
|
||||||
Cert string `yaml:"cert"`
|
Cert string `yaml:"cert" json:"cert,omitempty"`
|
||||||
Key string `yaml:"key"`
|
Key string `yaml:"key" json:"key,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mail struct {
|
type Mail struct {
|
||||||
User string `yaml:"user"`
|
User string `yaml:"user" json:"user,omitempty"`
|
||||||
Alias string `yaml:"alias"`
|
Alias string `yaml:"alias" json:"alias,omitempty"`
|
||||||
Pass string `yaml:"pass"`
|
Pass string `yaml:"pass" json:"pass,omitempty"`
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host" json:"host,omitempty"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port" json:"port,omitempty"`
|
||||||
Ssl bool `yaml:"ssl"`
|
Ssl bool `yaml:"ssl" json:"ssl,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mysql struct {
|
type Mysql struct {
|
||||||
Dsn Dsn `yaml:"dsn"`
|
Dsn Dsn `yaml:"dsn" json:"dsn"`
|
||||||
Pool Pool `yaml:"pool"`
|
Pool Pool `yaml:"pool" json:"pool"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(conf string) error {
|
func InitConfig(conf string) error {
|
||||||
|
@ -94,12 +95,12 @@ func InitConfig(conf string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Dsn struct {
|
type Dsn struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host" json:"host,omitempty"`
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port" json:"port,omitempty"`
|
||||||
Db string `yaml:"db"`
|
Db string `yaml:"db" json:"db,omitempty"`
|
||||||
User string `yaml:"user"`
|
User string `yaml:"user" json:"user,omitempty"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password" json:"password,omitempty"`
|
||||||
Charset string `yaml:"charset"`
|
Charset string `yaml:"charset" json:"charset,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Dsn) GetDsn() string {
|
func (m Dsn) GetDsn() string {
|
||||||
|
@ -111,8 +112,8 @@ func (m Dsn) GetDsn() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime"`
|
ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime" json:"connMaxIdleTime,omitempty"`
|
||||||
MaxOpenConn int `yaml:"maxOpenConn"`
|
MaxOpenConn int `yaml:"maxOpenConn" json:"maxOpenConn,omitempty"`
|
||||||
MaxIdleConn int `yaml:"maxIdleConn"`
|
MaxIdleConn int `yaml:"maxIdleConn" json:"maxIdleConn,omitempty"`
|
||||||
ConnMaxLifetime time.Duration `yaml:"connMaxLifetime"`
|
ConnMaxLifetime time.Duration `yaml:"connMaxLifetime" json:"connMaxLifetime,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error)
|
||||||
if postmeta.MetaKey == "_wp_attachment_metadata" {
|
if postmeta.MetaKey == "_wp_attachment_metadata" {
|
||||||
metadata, err := phphelper.UnPHPSerializeToStruct[models.WpAttachmentMetadata](postmeta.MetaValue)
|
metadata, err := phphelper.UnPHPSerializeToStruct[models.WpAttachmentMetadata](postmeta.MetaValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "解析postmeta失败", postmeta.MetaId, postmeta.MetaValue)
|
logs.Error(err, "解析postmeta失败", postmeta.MetaId, postmeta.MetaValue)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r[postmeta.PostId][postmeta.MetaKey] = metadata
|
r[postmeta.PostId][postmeta.MetaKey] = metadata
|
||||||
|
|
|
@ -2,17 +2,45 @@ package logs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ErrPrintln(err error, desc string, args ...any) {
|
var logs = safety.NewVar[*log.Logger](nil)
|
||||||
if err == nil {
|
|
||||||
return
|
func InitLogger() error {
|
||||||
|
l := &log.Logger{}
|
||||||
|
c := config.GetConfig()
|
||||||
|
if c.LogOutput == "" {
|
||||||
|
c.LogOutput = "stderr"
|
||||||
}
|
}
|
||||||
|
var out io.Writer
|
||||||
|
switch c.LogOutput {
|
||||||
|
case "stdout":
|
||||||
|
out = os.Stdout
|
||||||
|
case "stderr":
|
||||||
|
out = os.Stderr
|
||||||
|
default:
|
||||||
|
file, err := os.OpenFile(c.LogOutput, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
out = file
|
||||||
|
}
|
||||||
|
logs.Store(l)
|
||||||
|
l.SetFlags(log.Ldate | log.Ltime)
|
||||||
|
l.SetOutput(out)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Errs(err error, depth int, desc string, args ...any) {
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
runtime.Callers(2, pcs[:])
|
runtime.Callers(depth, pcs[:])
|
||||||
f := runtime.CallersFrames([]uintptr{pcs[0]})
|
f := runtime.CallersFrames([]uintptr{pcs[0]})
|
||||||
ff, _ := f.Next()
|
ff, _ := f.Next()
|
||||||
s := strings.Builder{}
|
s := strings.Builder{}
|
||||||
|
@ -23,7 +51,16 @@ func ErrPrintln(err error, desc string, args ...any) {
|
||||||
_, _ = fmt.Fprintf(&s, "%v", arg)
|
_, _ = fmt.Fprintf(&s, "%v", arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
logs.Load().Println(s.String())
|
||||||
log.Println(s.String())
|
}
|
||||||
}
|
|
||||||
|
func Error(err error, desc string, args ...any) {
|
||||||
|
Errs(err, 3, desc, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IfError(err error, desc string, args ...any) {
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Errs(err, 3, desc, args...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,19 +19,24 @@ const ThemeName = "twentyfifteen"
|
||||||
func Init(fs embed.FS) {
|
func Init(fs embed.FS) {
|
||||||
b, err := fs.ReadFile("twentyfifteen/themesupport.json")
|
b, err := fs.ReadFile("twentyfifteen/themesupport.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logs.Error(err, "读取themesupport.json失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(b, &themesupport)
|
err = json.Unmarshal(b, &themesupport)
|
||||||
logs.ErrPrintln(err, "解析themesupport失败")
|
if err != nil {
|
||||||
|
logs.Error(err, "解析themesupport失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
bytes, err := fs.ReadFile("twentyfifteen/colorscheme.json")
|
bytes, err := fs.ReadFile("twentyfifteen/colorscheme.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logs.Error(err, "读取colorscheme.json失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(bytes, &colorscheme)
|
err = json.Unmarshal(bytes, &colorscheme)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logs.Error(err, "解析colorscheme失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logs.ErrPrintln(err, "解析colorscheme失败")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, data)...)
|
var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, data)...)
|
||||||
|
@ -56,7 +61,7 @@ func data(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||||
next(h)
|
next(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configs(h *wp.Handle) *wp.Handle {
|
func configs(h *wp.Handle) {
|
||||||
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
|
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
|
||||||
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
||||||
})
|
})
|
||||||
|
@ -67,5 +72,4 @@ func configs(h *wp.Handle) *wp.Handle {
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50))
|
||||||
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50))
|
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50))
|
||||||
return h
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ func Hook(h *wp.Handle) {
|
||||||
pipe(h)
|
pipe(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configs(h *wp.Handle) *wp.Handle {
|
func configs(h *wp.Handle) {
|
||||||
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
|
||||||
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
||||||
|
@ -67,7 +67,6 @@ func configs(h *wp.Handle) *wp.Handle {
|
||||||
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10))
|
||||||
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10))
|
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10))
|
||||||
return h
|
|
||||||
}
|
}
|
||||||
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||||
wp.InitThemeArgAndConfig(configs, h)
|
wp.InitThemeArgAndConfig(configs, h)
|
||||||
|
@ -95,7 +94,7 @@ var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
|
||||||
func errorsHandle(h *wp.Handle) {
|
func errorsHandle(h *wp.Handle) {
|
||||||
switch h.Stats {
|
switch h.Stats {
|
||||||
case constraints.Error404, constraints.InternalErr, constraints.ParamError:
|
case constraints.Error404, constraints.InternalErr, constraints.ParamError:
|
||||||
logs.ErrPrintln(h.Err(), "报错:")
|
logs.IfError(h.Err(), "报错:")
|
||||||
h.SetTempl("twentyseventeen/posts/error.gohtml")
|
h.SetTempl("twentyseventeen/posts/error.gohtml")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string,
|
||||||
var con any
|
var con any
|
||||||
err = json.Unmarshal([]byte(blockParser.Attrs), &con)
|
err = json.Unmarshal([]byte(blockParser.Attrs), &con)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "解析category attr错误", blockParser.Attrs)
|
logs.Error(err, "解析category attr错误", blockParser.Attrs)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var conf map[any]any
|
var conf map[any]any
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
|
||||||
return hs
|
return hs
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "获取页眉背景图失败")
|
logs.Error(err, "获取页眉背景图失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hs := slice.Copy(img)
|
hs := slice.Copy(img)
|
||||||
|
|
|
@ -38,7 +38,7 @@ func (d *DetailHandle) BuildDetailData() (err error) {
|
||||||
func (d *DetailHandle) CheckAndGetPost() (err error) {
|
func (d *DetailHandle) CheckAndGetPost() (err error) {
|
||||||
id := str.ToInteger[uint64](d.C.Param("id"), 0)
|
id := str.ToInteger[uint64](d.C.Param("id"), 0)
|
||||||
maxId, err := cache.GetMaxPostId(d.C)
|
maxId, err := cache.GetMaxPostId(d.C)
|
||||||
logs.ErrPrintln(err, "get max post id")
|
logs.IfError(err, "get max post id")
|
||||||
if id > maxId || id <= 0 {
|
if id > maxId || id <= 0 {
|
||||||
d.Stats = constraints.ParamError
|
d.Stats = constraints.ParamError
|
||||||
err = errors.New("无效的文档id")
|
err = errors.New("无效的文档id")
|
||||||
|
@ -49,7 +49,7 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
|
||||||
post, err := cache.GetPostById(d.C, id)
|
post, err := cache.GetPostById(d.C, id)
|
||||||
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
||||||
d.Stats = constraints.Error404
|
d.Stats = constraints.Error404
|
||||||
logs.ErrPrintln(err, "获取id失败")
|
logs.IfError(err, "获取id失败")
|
||||||
err = errors.New(str.Join("无效的文档id "))
|
err = errors.New(str.Join("无效的文档id "))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ func (d *DetailHandle) PasswordProject() {
|
||||||
}
|
}
|
||||||
func (d *DetailHandle) Comment() {
|
func (d *DetailHandle) Comment() {
|
||||||
comments, err := cache.PostComments(d.C, d.Post.Id)
|
comments, err := cache.PostComments(d.C, d.Post.Id)
|
||||||
logs.ErrPrintln(err, "get d.Post comment", d.Post.Id)
|
logs.IfError(err, "get d.Post comment", d.Post.Id)
|
||||||
d.ginH["comments"] = comments
|
d.ginH["comments"] = comments
|
||||||
d.Comments = comments
|
d.Comments = comments
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ func (d *DetailHandle) RenderComment() {
|
||||||
|
|
||||||
func (d *DetailHandle) ContextPost() {
|
func (d *DetailHandle) ContextPost() {
|
||||||
prev, next, err := cache.GetContextPost(d.C, d.Post.Id, d.Post.PostDate)
|
prev, next, err := cache.GetContextPost(d.C, d.Post.Id, d.Post.PostDate)
|
||||||
logs.ErrPrintln(err, "get pre and next post", d.Post.Id, d.Post.PostDate)
|
logs.IfError(err, "get pre and next post", d.Post.Id, d.Post.PostDate)
|
||||||
d.ginH["next"] = next
|
d.ginH["next"] = next
|
||||||
d.ginH["prev"] = prev
|
d.ginH["prev"] = prev
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle
|
||||||
if ok {
|
if ok {
|
||||||
return v, true
|
return v, true
|
||||||
}
|
}
|
||||||
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
|
logs.IfError(errors.New(str.Join("插件", t, "不存在")), "")
|
||||||
return nil, false
|
return nil, false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func (h *Handle) StickPosts() []models.Posts {
|
||||||
}
|
}
|
||||||
array, err := phpserialize.UnmarshalIndexedArray([]byte(v))
|
array, err := phpserialize.UnmarshalIndexedArray([]byte(v))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "解析option sticky_posts错误")
|
logs.Error(err, "解析option sticky_posts错误", v)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r = slice.FilterAndMap(array, func(t any) (models.Posts, bool) {
|
r = slice.FilterAndMap(array, func(t any) (models.Posts, bool) {
|
||||||
|
|
|
@ -65,22 +65,23 @@ type HandleCall struct {
|
||||||
Order int
|
Order int
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitThemeArgAndConfig(fn func(*Handle) *Handle, h *Handle) {
|
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
||||||
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
|
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
|
||||||
h.components = make(map[string][]Components[string])
|
h.components = make(map[string][]Components[string])
|
||||||
h.handleFns = make(map[int][]HandleCall)
|
h.handleFns = make(map[int][]HandleCall)
|
||||||
h.componentsArgs = make(map[string]any)
|
h.componentsArgs = make(map[string]any)
|
||||||
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
|
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
|
||||||
hh := fn(h)
|
h.ginH = gin.H{}
|
||||||
return *hh
|
fn(h)
|
||||||
|
return *h
|
||||||
})
|
})
|
||||||
m := make(map[string][]Components[string])
|
m := make(map[string][]Components[string])
|
||||||
for k, v := range hh.components {
|
for k, v := range hh.components {
|
||||||
vv := make([]Components[string], len(v))
|
vv := make([]Components[string], len(v))
|
||||||
copy(vv, v)
|
copy(vv, v)
|
||||||
m[k] = vv // slice.Copy(v)
|
m[k] = vv
|
||||||
}
|
}
|
||||||
h.components = m // maps.Copy(hh.components)
|
h.components = m
|
||||||
h.handleFns = hh.handleFns
|
h.handleFns = hh.handleFns
|
||||||
h.componentsArgs = hh.componentsArgs
|
h.componentsArgs = hh.componentsArgs
|
||||||
h.componentFilterFn = hh.componentFilterFn
|
h.componentFilterFn = hh.componentFilterFn
|
||||||
|
@ -201,12 +202,11 @@ func (h *Handle) SetComponentsArgs(key string, value any) {
|
||||||
|
|
||||||
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
mods, err := wpconfig.GetThemeMods(theme)
|
mods, err := wpconfig.GetThemeMods(theme)
|
||||||
logs.ErrPrintln(err, "获取mods失败")
|
logs.IfError(err, "获取mods失败")
|
||||||
return &Handle{
|
return &Handle{
|
||||||
C: c,
|
C: c,
|
||||||
theme: theme,
|
theme: theme,
|
||||||
Session: sessions.Default(c),
|
Session: sessions.Default(c),
|
||||||
ginH: gin.H{},
|
|
||||||
scene: scene,
|
scene: scene,
|
||||||
Stats: constraints.Ok,
|
Stats: constraints.Ok,
|
||||||
themeMods: mods,
|
themeMods: mods,
|
||||||
|
|
|
@ -149,7 +149,7 @@ func GetThemeMods(theme string) (r ThemeMods, err error) {
|
||||||
//这里在的err可以不用处理,因为php的默认值和有设置过的类型可能不一样,直接按有设置的类型处理就行
|
//这里在的err可以不用处理,因为php的默认值和有设置过的类型可能不一样,直接按有设置的类型处理就行
|
||||||
r, err = maps.StrAnyMapToStruct[ThemeMods](m)
|
r, err = maps.StrAnyMapToStruct[ThemeMods](m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.ErrPrintln(err, "解析thememods错误")
|
logs.Error(err, "解析thememods错误(可忽略)")
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
r.setThemeSupport(theme)
|
r.setThemeSupport(theme)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user