diff --git a/actions/comment.go b/actions/comment.go
index 379b036..a4eecea 100644
--- a/actions/comment.go
+++ b/actions/comment.go
@@ -11,7 +11,6 @@ import (
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/mail"
- "github/fthvgb1/wp-go/models/wp"
"io"
"net/http"
"net/url"
@@ -52,7 +51,7 @@ func PostComment(c *gin.Context) {
}
defer req.Body.Close()
req.Header = c.Request.Header.Clone()
- home, err := url.Parse(wp.Option["siteurl"])
+ home, err := url.Parse(config.Options.Value("siteurl"))
if err != nil {
return
}
@@ -104,7 +103,7 @@ func PostComment(c *gin.Context) {
logs.ErrPrintln(err, "获取文档", id)
return
}
- su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wp.Option["siteurl"], author, m, post.PostTitle)
+ su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", config.Options.Value("siteurl"), author, m, post.PostTitle)
err = mail.SendMail([]string{config.Conf.Mail.User}, su, comment)
logs.ErrPrintln(err, "发送邮件", config.Conf.Mail.User, su, comment)
}()
diff --git a/actions/common/common.go b/actions/common/common.go
index 96b939d..48b208c 100644
--- a/actions/common/common.go
+++ b/actions/common/common.go
@@ -131,10 +131,10 @@ func categories(a ...any) (terms []wp.WpTermsMy, err error) {
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
}, nil, 0, in)
for i := 0; i < len(terms); i++ {
- if v, ok := wp.Terms[terms[i].WpTerms.TermId]; ok {
+ if v, ok := config.Terms.Load(terms[i].WpTerms.TermId); ok {
terms[i].WpTerms = v
}
- if v, ok := wp.TermTaxonomies[terms[i].WpTerms.TermId]; ok {
+ if v, ok := config.TermTaxonomies.Load(terms[i].WpTerms.TermId); ok {
terms[i].TermTaxonomy = v
}
}
diff --git a/actions/detail.go b/actions/detail.go
index 38649ab..baef1a3 100644
--- a/actions/detail.go
+++ b/actions/detail.go
@@ -5,6 +5,7 @@ import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common"
+ "github/fthvgb1/wp-go/config"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/models/wp"
@@ -32,8 +33,8 @@ func Detail(c *gin.Context) {
categoryItems := common.Categories(c)
recentComments := common.RecentComments(c, 5)
var h = gin.H{
- "title": wp.Option["blogname"],
- "options": wp.Option,
+ "title": config.Options.Value("blogname"),
+ "options": config.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
@@ -91,11 +92,11 @@ func Detail(c *gin.Context) {
commentss := treeComments(comments)
prev, next, err := common.GetContextPost(c, post.Id, post.PostDate)
logs.ErrPrintln(err, "get pre and next post", post.Id, post.PostDate)
- h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wp.Option["blogname"])
+ h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, config.Options.Value("blogname"))
h["post"] = post
h["showComment"] = showComment
h["prev"] = prev
- depth := wp.Option["thread_comments_depth"]
+ depth := config.Options.Value("thread_comments_depth")
d, err := strconv.Atoi(depth)
if err != nil {
logs.ErrPrintln(err, "get comment depth")
@@ -272,7 +273,7 @@ func gravatar(c *gin.Context, email string) (u string) {
q := url.Values{}
q.Add("s", "112")
q.Add("d", "mm")
- q.Add("r", strings.ToLower(wp.Option["avatar_rating"]))
+ q.Add("r", strings.ToLower(config.Options.Value("avatar_rating")))
u = fmt.Sprintf("%s?%s", u, q.Encode())
return
}
diff --git a/actions/feed.go b/actions/feed.go
index 286277c..1583f5e 100644
--- a/actions/feed.go
+++ b/actions/feed.go
@@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/cache"
+ "github/fthvgb1/wp-go/config"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/models/wp"
@@ -25,14 +26,14 @@ var commentsFeedCache = cache.NewSliceCache(commentsFeed, time.Hour)
func InitFeed() {
templateRss = rss2.Rss2{
- Title: wp.Option["blogname"],
- AtomLink: fmt.Sprintf("%s/feed", wp.Option["home"]),
- Link: wp.Option["siteurl"],
- Description: wp.Option["blogdescription"],
+ Title: config.Options.Value("blogname"),
+ AtomLink: fmt.Sprintf("%s/feed", config.Options.Value("home")),
+ Link: config.Options.Value("siteurl"),
+ Description: config.Options.Value("blogdescription"),
Language: "zh-CN",
UpdatePeriod: "hourly",
UpdateFrequency: 1,
- Generator: wp.Option["home"],
+ Generator: config.Options.Value("home"),
}
}
@@ -92,9 +93,9 @@ func feed(arg ...any) (xml []string, err error) {
}
l := ""
if t.CommentStatus == "open" && t.CommentCount > 0 {
- l = fmt.Sprintf("%s/p/%d#comments", wp.Option["siteurl"], t.Id)
+ l = fmt.Sprintf("%s/p/%d#comments", config.Options.Value("siteurl"), t.Id)
} else if t.CommentStatus == "open" && t.CommentCount == 0 {
- l = fmt.Sprintf("%s/p/%d#respond", wp.Option["siteurl"], t.Id)
+ l = fmt.Sprintf("%s/p/%d#respond", config.Options.Value("siteurl"), t.Id)
}
user := common.GetUser(c, t.PostAuthor)
@@ -106,8 +107,8 @@ func feed(arg ...any) (xml []string, err error) {
Content: t.PostContent,
Category: strings.Join(t.Categories, "、"),
CommentLink: l,
- CommentRss: fmt.Sprintf("%s/p/%d/feed", wp.Option["siteurl"], t.Id),
- Link: fmt.Sprintf("%s/p/%d", wp.Option["siteurl"], t.Id),
+ CommentRss: fmt.Sprintf("%s/p/%d/feed", config.Options.Value("siteurl"), t.Id),
+ Link: fmt.Sprintf("%s/p/%d", config.Options.Value("siteurl"), t.Id),
Description: desc,
PubDate: t.PostDateGmt.Format(timeFormat),
}
@@ -169,8 +170,8 @@ func postFeed(arg ...any) (x string, err error) {
rs := templateRss
rs.Title = fmt.Sprintf("《%s》的评论", post.PostTitle)
- rs.AtomLink = fmt.Sprintf("%s/p/%d/feed", wp.Option["siteurl"], post.Id)
- rs.Link = fmt.Sprintf("%s/p/%d", wp.Option["siteurl"], post.Id)
+ rs.AtomLink = fmt.Sprintf("%s/p/%d/feed", config.Options.Value("siteurl"), post.Id)
+ rs.Link = fmt.Sprintf("%s/p/%d", config.Options.Value("siteurl"), post.Id)
rs.LastBuildDate = time.Now().Format(timeFormat)
if post.PostPassword != "" {
if len(comments) > 0 {
@@ -179,7 +180,7 @@ func postFeed(arg ...any) (x string, err error) {
rs.Items = []rss2.Item{
{
Title: fmt.Sprintf("评价者:%s", t.CommentAuthor),
- Link: fmt.Sprintf("%s/p/%d#comment-%d", wp.Option["siteurl"], post.Id, t.CommentId),
+ Link: fmt.Sprintf("%s/p/%d#comment-%d", config.Options.Value("siteurl"), post.Id, t.CommentId),
Creator: t.CommentAuthor,
PubDate: t.CommentDateGmt.Format(timeFormat),
Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId),
@@ -192,7 +193,7 @@ func postFeed(arg ...any) (x string, err error) {
rs.Items = helper.SliceMap(comments, func(t wp.Comments) rss2.Item {
return rss2.Item{
Title: fmt.Sprintf("评价者:%s", t.CommentAuthor),
- Link: fmt.Sprintf("%s/p/%d#comment-%d", wp.Option["siteurl"], post.Id, t.CommentId),
+ Link: fmt.Sprintf("%s/p/%d#comment-%d", config.Options.Value("siteurl"), post.Id, t.CommentId),
Creator: t.CommentAuthor,
PubDate: t.CommentDateGmt.Format(timeFormat),
Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId),
@@ -224,9 +225,9 @@ func commentsFeed(args ...any) (r []string, err error) {
c := args[0].(*gin.Context)
commens := common.RecentComments(c, 10)
rs := templateRss
- rs.Title = fmt.Sprintf("\"%s\"的评论", wp.Option["blogname"])
+ rs.Title = fmt.Sprintf("\"%s\"的评论", config.Options.Value("blogname"))
rs.LastBuildDate = time.Now().Format(timeFormat)
- rs.AtomLink = fmt.Sprintf("%s/comments/feed", wp.Option["siteurl"])
+ rs.AtomLink = fmt.Sprintf("%s/comments/feed", config.Options.Value("siteurl"))
com, err := common.GetCommentByIds(c, helper.SliceMap(commens, func(t wp.Comments) uint64 {
return t.CommentId
}))
@@ -247,7 +248,7 @@ func commentsFeed(args ...any) (r []string, err error) {
}
return rss2.Item{
Title: fmt.Sprintf("%s对《%s》的评论", t.CommentAuthor, post.PostTitle),
- Link: fmt.Sprintf("%s/p/%d#comment-%d", wp.Option["siteurl"], post.Id, t.CommentId),
+ Link: fmt.Sprintf("%s/p/%d#comment-%d", config.Options.Value("siteurl"), post.Id, t.CommentId),
Creator: t.CommentAuthor,
Description: desc,
PubDate: t.CommentDateGmt.Format(timeFormat),
diff --git a/actions/index.go b/actions/index.go
index dd05c6a..93032be 100644
--- a/actions/index.go
+++ b/actions/index.go
@@ -5,6 +5,7 @@ import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common"
+ "github/fthvgb1/wp-go/config"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/models/wp"
@@ -40,7 +41,7 @@ type indexHandle struct {
}
func newIndexHandle(ctx *gin.Context) *indexHandle {
- size := wp.Option["posts_per_page"]
+ size := config.Options.Value("posts_per_page")
si, _ := strconv.Atoi(size)
return &indexHandle{
c: ctx,
@@ -48,8 +49,8 @@ func newIndexHandle(ctx *gin.Context) *indexHandle {
page: 1,
pageSize: si,
paginationStep: 1,
- titleL: wp.Option["blogname"],
- titleR: wp.Option["blogdescription"],
+ titleL: config.Options.Value("blogname"),
+ titleR: config.Options.Value("blogdescription"),
where: models.SqlBuilder{
{"post_type", "in", ""},
{"post_status", "in", ""},
@@ -93,7 +94,7 @@ func (h *indexHandle) parseParams() {
})
ss := fmt.Sprintf("%s年%s月", year, strings.TrimLeft(month, "0"))
h.header = fmt.Sprintf("月度归档: %s", ss)
- h.setTitleLR(ss, wp.Option["blogname"])
+ h.setTitleLR(ss, config.Options.Value("blogname"))
h.scene = plugins.Archive
}
category := h.c.Param("category")
@@ -120,7 +121,7 @@ func (h *indexHandle) parseParams() {
}, []string{
"left join", "wp_terms d", "c.term_id=d.term_id",
})
- h.setTitleLR(category, wp.Option["blogname"])
+ h.setTitleLR(category, config.Options.Value("blogname"))
h.scene = plugins.Category
}
s := h.c.Query("s")
@@ -133,7 +134,7 @@ func (h *indexHandle) parseParams() {
}, []string{"post_password", ""})
h.postType = append(h.postType, "page", "attachment")
h.header = fmt.Sprintf("%s的搜索结果", s)
- h.setTitleLR(helper.StrJoin(`"`, s, `"`, "的搜索结果"), wp.Option["blogname"])
+ h.setTitleLR(helper.StrJoin(`"`, s, `"`, "的搜索结果"), config.Options.Value("blogname"))
h.search = s
h.scene = plugins.Search
}
@@ -150,7 +151,7 @@ func (h *indexHandle) parseParams() {
h.page = 1
}
if h.page > 1 && (h.category != "" || h.search != "" || month != "") {
- h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), wp.Option["blogname"])
+ h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), config.Options.Value("blogname"))
}
}
@@ -167,7 +168,7 @@ func Index(c *gin.Context) {
categoryItems := common.Categories(c)
recentComments := common.RecentComments(c, 5)
ginH := gin.H{
- "options": wp.Option,
+ "options": config.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
diff --git a/actions/login.go b/actions/login.go
index 59f6ef3..9c32d2b 100644
--- a/actions/login.go
+++ b/actions/login.go
@@ -4,8 +4,8 @@ import (
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
+ "github/fthvgb1/wp-go/config"
"github/fthvgb1/wp-go/helper"
- "github/fthvgb1/wp-go/models/wp"
"github/fthvgb1/wp-go/phpass"
"net/http"
"strings"
@@ -33,7 +33,7 @@ func Login(c *gin.Context) {
c.Error(err)
return
}
- cohash := fmt.Sprintf("wp-postpass_%s", helper.StringMd5(wp.Option["siteurl"]))
+ cohash := fmt.Sprintf("wp-postpass_%s", helper.StringMd5(config.Options.Value("siteurl")))
c.SetCookie(cohash, pass, 24*3600, "/", "", false, false)
c.Redirect(http.StatusFound, ref)
diff --git a/config/options.go b/config/options.go
new file mode 100644
index 0000000..97b2bb1
--- /dev/null
+++ b/config/options.go
@@ -0,0 +1,28 @@
+package config
+
+import (
+ "context"
+ "github/fthvgb1/wp-go/models"
+ "github/fthvgb1/wp-go/models/wp"
+ "github/fthvgb1/wp-go/safety"
+)
+
+var Options safety.Map[string, string]
+
+func InitOptions() error {
+ ctx := context.Background()
+ ops, err := models.SimpleFind[wp.Options](ctx, models.SqlBuilder{{"autoload", "yes"}}, "option_name, option_value")
+ if err != nil {
+ return err
+ }
+ if len(ops) == 0 {
+ ops, err = models.SimpleFind[wp.Options](ctx, nil, "option_name, option_value")
+ if err != nil {
+ return err
+ }
+ }
+ for _, options := range ops {
+ Options.Store(options.OptionName, options.OptionValue)
+ }
+ return nil
+}
diff --git a/config/term.go b/config/term.go
new file mode 100644
index 0000000..f3588a0
--- /dev/null
+++ b/config/term.go
@@ -0,0 +1,30 @@
+package config
+
+import (
+ "context"
+ "github/fthvgb1/wp-go/models"
+ "github/fthvgb1/wp-go/models/wp"
+ "github/fthvgb1/wp-go/safety"
+)
+
+var Terms safety.Map[uint64, wp.WpTerms]
+var TermTaxonomies safety.Map[uint64, wp.TermTaxonomy]
+
+func InitTerms() (err error) {
+ ctx := context.Background()
+ terms, err := models.SimpleFind[wp.WpTerms](ctx, nil, "*")
+ if err != nil {
+ return err
+ }
+ for _, wpTerms := range terms {
+ Terms.Store(wpTerms.TermId, wpTerms)
+ }
+ termTax, err := models.SimpleFind[wp.TermTaxonomy](ctx, nil, "*")
+ if err != nil {
+ return err
+ }
+ for _, taxonomy := range termTax {
+ TermTaxonomies.Store(taxonomy.TermTaxonomyId, taxonomy)
+ }
+ return
+}
diff --git a/main.go b/main.go
index 489fa3f..4971c30 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,6 @@ import (
"github/fthvgb1/wp-go/config"
"github/fthvgb1/wp-go/db"
"github/fthvgb1/wp-go/models"
- "github/fthvgb1/wp-go/models/wp"
"github/fthvgb1/wp-go/plugins"
"github/fthvgb1/wp-go/route"
"math/rand"
@@ -29,12 +28,11 @@ func init() {
panic(err)
}
models.InitDB(db.NewSqlxDb(db.Db))
- err = wp.InitOptions()
+ err = config.InitOptions()
if err != nil {
panic(err)
}
-
- err = wp.InitTerms()
+ err = config.InitTerms()
if err != nil {
panic(err)
}
diff --git a/middleware/sendmail.go b/middleware/sendmail.go
index 8ac8d91..2549bc5 100644
--- a/middleware/sendmail.go
+++ b/middleware/sendmail.go
@@ -7,7 +7,6 @@ import (
"github/fthvgb1/wp-go/config"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/mail"
- "github/fthvgb1/wp-go/models/wp"
"io"
"io/ioutil"
"net/http"
@@ -43,7 +42,7 @@ func RecoverAndSendMail(w io.Writer) func(ctx *gin.Context) {
er := mail.SendMail(
[]string{config.Conf.Mail.User},
- fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(wp.Option["siteurl"]), c.FullPath(), time.Now().Format(time.RFC1123Z)), content)
+ fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(config.Options.Value("siteurl")), c.FullPath(), time.Now().Format(time.RFC1123Z)), content)
if er != nil {
logs.ErrPrintln(er, "recover send mail fail", fmt.Sprintf("%v", err))
diff --git a/models/wp/globalInit.go b/models/wp/globalInit.go
deleted file mode 100644
index dabc21b..0000000
--- a/models/wp/globalInit.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package wp
-
-import (
- "context"
- "github/fthvgb1/wp-go/models"
-)
-
-var Option = make(map[string]string)
-var Terms = map[uint64]WpTerms{}
-var TermTaxonomies = map[uint64]TermTaxonomy{}
-
-func InitOptions() error {
- ctx := context.Background()
- ops, err := models.SimpleFind[Options](ctx, models.SqlBuilder{{"autoload", "yes"}}, "option_name, option_value")
- if err != nil {
- return err
- }
- if len(ops) == 0 {
- ops, err = models.SimpleFind[Options](ctx, nil, "option_name, option_value")
- if err != nil {
- return err
- }
- }
- for _, options := range ops {
- Option[options.OptionName] = options.OptionValue
- }
- return nil
-}
-
-func InitTerms() (err error) {
- ctx := context.Background()
- terms, err := models.SimpleFind[WpTerms](ctx, nil, "*")
- if err != nil {
- return err
- }
- for _, wpTerms := range terms {
- Terms[wpTerms.TermId] = wpTerms
- }
- termTax, err := models.SimpleFind[TermTaxonomy](ctx, nil, "*")
- if err != nil {
- return err
- }
- for _, taxonomy := range termTax {
- TermTaxonomies[taxonomy.TermTaxonomyId] = taxonomy
- }
- return
-}
diff --git a/route/route.go b/route/route.go
index 675c54b..cb67360 100644
--- a/route/route.go
+++ b/route/route.go
@@ -34,6 +34,9 @@ func SetupRouter() *gin.Engine {
"dateCh": func(t time.Time) any {
return t.Format("2006年 01月 02日")
},
+ "getOption": func(k string) string {
+ return config.Options.Value(k)
+ },
}).SetTemplate()
r.Use(
gin.Logger(),
diff --git a/safety/safemap.go b/safety/safemap.go
index bb161ec..5f5f6dc 100644
--- a/safety/safemap.go
+++ b/safety/safemap.go
@@ -98,6 +98,11 @@ func newEntry[V any](i V) *entry[V] {
return &entry[V]{p: unsafe.Pointer(&i)}
}
+func (m *Map[K, V]) Value(key K) (v V) {
+ v, _ = m.Load(key)
+ return
+}
+
// Load returns the value stored in the map for a key, or nil if no
// value is present.
// The ok result indicates whether value was found in the map.
diff --git a/templates/twentyfifteen/layout/base.gohtml b/templates/twentyfifteen/layout/base.gohtml
index e8dae82..559d6af 100644
--- a/templates/twentyfifteen/layout/base.gohtml
+++ b/templates/twentyfifteen/layout/base.gohtml
@@ -17,9 +17,9 @@