From 3215b0c8eabaa231a426fb0e9da265970a6ba776 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 6 Apr 2024 16:43:18 +0800 Subject: [PATCH] add get customize config func and add wpHandle add err level --- app/pkg/config/config.go | 10 ++++++++++ app/theme/wp/customheader.go | 2 +- app/theme/wp/detail.go | 12 ++++++------ app/theme/wp/errorlevel.go | 8 ++++++++ app/theme/wp/index.go | 2 +- app/theme/wp/wp.go | 12 +++++++++++- 6 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 app/theme/wp/errorlevel.go diff --git a/app/pkg/config/config.go b/app/pkg/config/config.go index 60fcf8d..c4165f6 100644 --- a/app/pkg/config/config.go +++ b/app/pkg/config/config.go @@ -85,6 +85,14 @@ type Mysql struct { 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 { if conf == "" { conf = "config.yaml" @@ -96,6 +104,7 @@ func InitConfig(conf string) error { if err != nil { return err } + defer get.Body.Close() file, err = io.ReadAll(get.Body) } else { file, err = os.ReadFile(conf) @@ -103,6 +112,7 @@ func InitConfig(conf string) error { if err != nil { return err } + fileData.Store(file) var c Config err = yaml.Unmarshal(file, &c) if err != nil { diff --git a/app/theme/wp/customheader.go b/app/theme/wp/customheader.go index 832adc4..6dfdecf 100644 --- a/app/theme/wp/customheader.go +++ b/app/theme/wp/customheader.go @@ -24,7 +24,7 @@ var GetCustomHeaderImgFn = reload.BuildValFnWithConfirm("headerImages", customHe func customHeadImag(h *Handle) ([]models.PostThumbnail, bool) { hs, err := h.GetHeaderImages(h.theme) 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 hs, true diff --git a/app/theme/wp/detail.go b/app/theme/wp/detail.go index 85ac3b7..5b7fe04 100644 --- a/app/theme/wp/detail.go +++ b/app/theme/wp/detail.go @@ -103,7 +103,7 @@ func (d *DetailHandle) CommentData() { pageComments := wpconfig.GetOption("page_comments") num, err := cachemanager.GetBy[int]("commentNumber", d.C, d.Post.Id, time.Second) if err != nil { - d.SetErr(err) + d.SetErr(err, Low) return } if num < 1 { @@ -111,7 +111,7 @@ func (d *DetailHandle) CommentData() { } topNum, err := cachemanager.GetBy[int]("postTopCommentsNum", d.C, d.Post.Id, time.Second) if err != nil { - d.SetErr(err) + d.SetErr(err, Low) return } d.TotalPage = number.DivideCeil(topNum, d.Limit) @@ -133,12 +133,12 @@ func (d *DetailHandle) CommentData() { d.ginH["page_comments"] = pageComments d.ginH["totalCommentPage"] = d.TotalPage 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 } data, err := cache.PostTopLevelCommentIds(d.C, d.Post.Id, d.Page, d.Limit, topNum, order, key) if err != nil { - d.SetErr(err) + d.SetErr(err, Low) return } d.TotalRaw = topNum @@ -163,7 +163,7 @@ func (d *DetailHandle) RenderComment() { var err error d.ginH["comments"], err = RenderComment(d.C, d.Page, d.CommentRender, d.Comments, 2*time.Second, d.IsHttps()) if err != nil { - d.SetErr(err) + d.SetErr(err, High) return } if d.CommentPageEle == nil { @@ -195,7 +195,7 @@ func Detail(h *Handle) { d := h.GetDetailHandle() err := d.BuildDetailData() if err != nil { - d.SetErr(err) + d.SetErr(err, High) } h.SetData("scene", h.Scene()) } diff --git a/app/theme/wp/errorlevel.go b/app/theme/wp/errorlevel.go new file mode 100644 index 0000000..951f055 --- /dev/null +++ b/app/theme/wp/errorlevel.go @@ -0,0 +1,8 @@ +package wp + +const ( + None = iota + Low + High + Fatal +) diff --git a/app/theme/wp/index.go b/app/theme/wp/index.go index 8c70020..26bee23 100644 --- a/app/theme/wp/index.go +++ b/app/theme/wp/index.go @@ -173,7 +173,7 @@ func Index(h *Handle) { i := h.GetIndexHandle() err := i.BuildIndexData() if err != nil { - i.SetErr(err) + i.SetErr(err, High) } h.SetData("scene", h.Scene()) } diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index f4e1106..2ffcf24 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -32,6 +32,7 @@ type Handle struct { templ string themeMods wpconfig.ThemeMods err error + errLevel int8 abort bool stopPipe bool } @@ -145,8 +146,17 @@ func (h *Handle) Err() error { return h.err } -func (h *Handle) SetErr(err error) { +func (h *Handle) SetErr(err error, level int8) { 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) {