add get customize config func and add wpHandle add err level

This commit is contained in:
xing 2024-04-06 16:43:18 +08:00
parent c7c97c469f
commit 3215b0c8ea
6 changed files with 37 additions and 9 deletions

View File

@ -85,6 +85,14 @@ type Mysql struct {
Pool Pool `yaml:"pool" json:"pool"` Pool Pool `yaml:"pool" json:"pool"`
} }
func GetCustomizedConfig[T any]() (T, error) {
var r T
err := yaml.Unmarshal(fileData.Load(), &r)
return r, err
}
var fileData = safety.NewVar([]byte{})
func InitConfig(conf string) error { func InitConfig(conf string) error {
if conf == "" { if conf == "" {
conf = "config.yaml" conf = "config.yaml"
@ -96,6 +104,7 @@ func InitConfig(conf string) error {
if err != nil { if err != nil {
return err return err
} }
defer get.Body.Close()
file, err = io.ReadAll(get.Body) file, err = io.ReadAll(get.Body)
} else { } else {
file, err = os.ReadFile(conf) file, err = os.ReadFile(conf)
@ -103,6 +112,7 @@ func InitConfig(conf string) error {
if err != nil { if err != nil {
return err return err
} }
fileData.Store(file)
var c Config var c Config
err = yaml.Unmarshal(file, &c) err = yaml.Unmarshal(file, &c)
if err != nil { if err != nil {

View File

@ -24,7 +24,7 @@ var GetCustomHeaderImgFn = reload.BuildValFnWithConfirm("headerImages", customHe
func customHeadImag(h *Handle) ([]models.PostThumbnail, bool) { func customHeadImag(h *Handle) ([]models.PostThumbnail, bool) {
hs, err := h.GetHeaderImages(h.theme) hs, err := h.GetHeaderImages(h.theme)
if err != nil { if err != nil {
h.SetErr(fmt.Errorf("get customheadimage err: %v", err)) h.SetErr(fmt.Errorf("get customheadimage err: %v", err), Low)
return nil, false return nil, false
} }
return hs, true return hs, true

View File

@ -103,7 +103,7 @@ func (d *DetailHandle) CommentData() {
pageComments := wpconfig.GetOption("page_comments") pageComments := wpconfig.GetOption("page_comments")
num, err := cachemanager.GetBy[int]("commentNumber", d.C, d.Post.Id, time.Second) num, err := cachemanager.GetBy[int]("commentNumber", d.C, d.Post.Id, time.Second)
if err != nil { if err != nil {
d.SetErr(err) d.SetErr(err, Low)
return return
} }
if num < 1 { if num < 1 {
@ -111,7 +111,7 @@ func (d *DetailHandle) CommentData() {
} }
topNum, err := cachemanager.GetBy[int]("postTopCommentsNum", d.C, d.Post.Id, time.Second) topNum, err := cachemanager.GetBy[int]("postTopCommentsNum", d.C, d.Post.Id, time.Second)
if err != nil { if err != nil {
d.SetErr(err) d.SetErr(err, Low)
return return
} }
d.TotalPage = number.DivideCeil(topNum, d.Limit) d.TotalPage = number.DivideCeil(topNum, d.Limit)
@ -133,12 +133,12 @@ func (d *DetailHandle) CommentData() {
d.ginH["page_comments"] = pageComments d.ginH["page_comments"] = pageComments
d.ginH["totalCommentPage"] = d.TotalPage d.ginH["totalCommentPage"] = d.TotalPage
if d.TotalPage < d.Page { if d.TotalPage < d.Page {
d.SetErr(errors.New("curren page above total page")) d.SetErr(errors.New("curren page above total page"), High)
return return
} }
data, err := cache.PostTopLevelCommentIds(d.C, d.Post.Id, d.Page, d.Limit, topNum, order, key) data, err := cache.PostTopLevelCommentIds(d.C, d.Post.Id, d.Page, d.Limit, topNum, order, key)
if err != nil { if err != nil {
d.SetErr(err) d.SetErr(err, Low)
return return
} }
d.TotalRaw = topNum d.TotalRaw = topNum
@ -163,7 +163,7 @@ func (d *DetailHandle) RenderComment() {
var err error var err error
d.ginH["comments"], err = RenderComment(d.C, d.Page, d.CommentRender, d.Comments, 2*time.Second, d.IsHttps()) d.ginH["comments"], err = RenderComment(d.C, d.Page, d.CommentRender, d.Comments, 2*time.Second, d.IsHttps())
if err != nil { if err != nil {
d.SetErr(err) d.SetErr(err, High)
return return
} }
if d.CommentPageEle == nil { if d.CommentPageEle == nil {
@ -195,7 +195,7 @@ func Detail(h *Handle) {
d := h.GetDetailHandle() d := h.GetDetailHandle()
err := d.BuildDetailData() err := d.BuildDetailData()
if err != nil { if err != nil {
d.SetErr(err) d.SetErr(err, High)
} }
h.SetData("scene", h.Scene()) h.SetData("scene", h.Scene())
} }

View File

@ -0,0 +1,8 @@
package wp
const (
None = iota
Low
High
Fatal
)

View File

@ -173,7 +173,7 @@ func Index(h *Handle) {
i := h.GetIndexHandle() i := h.GetIndexHandle()
err := i.BuildIndexData() err := i.BuildIndexData()
if err != nil { if err != nil {
i.SetErr(err) i.SetErr(err, High)
} }
h.SetData("scene", h.Scene()) h.SetData("scene", h.Scene())
} }

View File

@ -32,6 +32,7 @@ type Handle struct {
templ string templ string
themeMods wpconfig.ThemeMods themeMods wpconfig.ThemeMods
err error err error
errLevel int8
abort bool abort bool
stopPipe bool stopPipe bool
} }
@ -145,8 +146,17 @@ func (h *Handle) Err() error {
return h.err return h.err
} }
func (h *Handle) SetErr(err error) { func (h *Handle) SetErr(err error, level int8) {
h.err = errors.Join(err) h.err = errors.Join(err)
h.errLevel = level
}
func (h *Handle) ErrLevel() int8 {
return h.errLevel
}
func (h *Handle) SetErrLevel(errLevel int8) {
h.errLevel = errLevel
} }
func (h *Handle) SetTempl(templ string) { func (h *Handle) SetTempl(templ string) {