首页 近期文章 归档

This commit is contained in:
xing 2022-09-14 21:30:59 +08:00
parent 3188d19cc2
commit e0517643a6
8 changed files with 72 additions and 52 deletions

View File

@ -11,4 +11,5 @@ func SetStaticFileCache(c *gin.Context) {
if len(f) > 1 && helper.IsContainInArr(f[0], []string{"wp-includes", "wp-content"}) {
c.Header("Cache-Control", "private, max-age=86400")
}
c.Next()
}

View File

@ -27,7 +27,7 @@ func InitTerms() (err error) {
name = append(name, "twentyfifteen")
terms, err := Find[WpTerms](SqlBuilder{{
"tt.taxonomy", "in", "",
}, {"t.name", "in", ""}}, "t.term_id", nil, SqlBuilder{{
}, {"t.name", "in", ""}}, "t.term_id", "", nil, SqlBuilder{{
"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id",
}}, 1, themes, name)
for _, wpTerms := range terms {

View File

@ -213,16 +213,23 @@ func Select[T Model](sql string, params ...interface{}) ([]T, error) {
return r, nil
}
func Find[T Model](where ParseWhere, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
var rr T
w, args := where.ParseWhere(in...)
j := join.parseJoin()
tp := "select %s from %s %s %s %s %s"
groupBy := ""
if group != "" {
g := strings.Builder{}
g.WriteString(" group by ")
g.WriteString(group)
groupBy = g.String()
}
tp := "select %s from %s %s %s %s %s %s"
l := ""
if limit > 0 {
l = fmt.Sprintf(" limit %d", limit)
}
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, order.parseOrderBy(), l)
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, order.parseOrderBy(), l)
err = db.Db.Select(&r, sql, args...)
return
}

View File

@ -38,3 +38,17 @@ func (w WpPosts) PrimaryKey() string {
func (w WpPosts) Table() string {
return "wp_posts"
}
func (w PostArchive) PrimaryKey() string {
return "ID"
}
func (w PostArchive) Table() string {
return "wp_posts"
}
type PostArchive struct {
Year string `db:"year"`
Month string `db:"month"`
Posts int `db:"posts"`
}

View File

@ -36,7 +36,7 @@ func index(c *gin.Context) {
if len(needQuery) > 0 {
rawPosts, err := models.Find[models.WpPosts](models.SqlBuilder{{
"Id", "in", "",
}}, "a.*,d.name category_name", nil, models.SqlBuilder{{
}}, "a.*,d.name category_name", "", nil, models.SqlBuilder{{
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
}, {
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
@ -56,8 +56,31 @@ func index(c *gin.Context) {
pp := post.(models.WpPosts)
allPosts = append(allPosts, pp)
}
recent, _ := recentPosts()
archive, _ := archives()
c.HTML(http.StatusOK, "index.html", gin.H{
"posts": allPosts,
"options": models.Options,
"posts": allPosts,
"options": models.Options,
"recentPosts": recent,
"archives": archive,
})
}
func recentPosts() (r []models.WpPosts, err error) {
r, err = models.Find[models.WpPosts](models.SqlBuilder{{
"post_type", "post",
}, {"post_status", "publish"}}, "ID,post_title", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5)
if err != nil {
return
}
return
}
func archives() (r []models.PostArchive, err error) {
r, err = models.Find[models.PostArchive](models.SqlBuilder{
{"post_type", "post"}, {"post_status", "publish"},
}, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0)
return
}

View File

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/middleware"
"github/fthvgb1/wp-go/static"
"github/fthvgb1/wp-go/templates"
"html/template"
"net/http"
"time"
@ -28,9 +29,14 @@ func SetupRouter() *gin.Engine {
FS: static.FsEx,
Path: "wp-content",
}))
r.LoadHTMLGlob("templates/**/*")
// Ping test
loadTemplates(r, "**/*")
r.GET("/", index)
return r
}
func loadTemplates(engine *gin.Engine, pattern string) {
templ := template.New("").Funcs(engine.FuncMap).Delims("{{", "}}")
templ = template.Must(templ.ParseFS(templates.TemplateFs, pattern))
engine.SetHTMLTemplate(templ)
}

View File

@ -13,21 +13,11 @@
<h2 class="widget-title">近期文章</h2>
<nav aria-label="近期文章">
<ul>
{{range $k,$v:=.recentPosts}}
<li>
<a href="https://www.xloyy.com/p/2799">密码保护2022.9.12</a>
</li>
<li>
<a href="https://www.xloyy.com/p/2797">密码保护2022.9.11</a>
</li>
<li>
<a href="https://www.xloyy.com/p/2795">密码保护2022.9.10</a>
</li>
<li>
<a href="https://www.xloyy.com/p/2793">密码保护2022.9.9</a>
</li>
<li>
<a href="https://www.xloyy.com/p/2789">密码保护2022.9.8</a>
<a href="/p/{{$v.Id}}">密码保护:{{$v.PostTitle}}</a>
</li>
{{end}}
</ul>
</nav>
@ -40,36 +30,9 @@
<aside id="archives-2" class="widget widget_archive"><h2 class="widget-title">归档</h2>
<nav aria-label="归档">
<ul>
<li><a href="https://www.xloyy.com/p/date/2022/09">2022年9月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/08">2022年8月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/07">2022年7月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/06">2022年6月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/05">2022年5月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/04">2022年4月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/03">2022年3月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/02">2022年2月</a></li>
<li><a href="https://www.xloyy.com/p/date/2022/01">2022年1月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/12">2021年12月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/11">2021年11月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/10">2021年10月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/09">2021年9月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/08">2021年8月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/07">2021年7月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/06">2021年6月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/05">2021年5月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/04">2021年4月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/03">2021年3月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/02">2021年2月</a></li>
<li><a href="https://www.xloyy.com/p/date/2021/01">2021年1月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/12">2020年12月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/11">2020年11月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/10">2020年10月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/09">2020年9月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/08">2020年8月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/07">2020年7月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/06">2020年6月</a></li>
<li><a href="https://www.xloyy.com/p/date/2020/05">2020年5月</a></li>
<li><a href="https://www.xloyy.com/p/date/2019/05">2019年5月</a></li>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month|printf "%02s"}}月</a></li>
{{end}}
</ul>
</nav>

6
templates/templatefs.go Normal file
View File

@ -0,0 +1,6 @@
package templates
import "embed"
//go:embed index layout
var TemplateFs embed.FS