chore: optimize SEO and fix golangci-lint issues
- Implement dynamic SEO meta tags and sitemap.xml for toolbox.pengzhan.dev - Fix SPA title/meta refresh issue in layout.html - Address golangci-lint findings: - Refactor switch-case for tool path and SVG token parsing - Replace math.Pow with direct multiplication for performance - Fix unhandled error returns and unused imports - Omit redundant type declarations
This commit is contained in:
+46
-6
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
@@ -56,6 +57,23 @@ func main() {
|
||||
subFS, _ := fs.Sub(webFS, "web")
|
||||
r.StaticFS("/static", http.FS(subFS))
|
||||
|
||||
// 2.5 SEO 静态文件映射
|
||||
r.GET("/robots.txt", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "User-agent: *\nAllow: /\nSitemap: https://toolbox.pengzhan.dev/sitemap.xml\n")
|
||||
})
|
||||
r.GET("/sitemap.xml", func(c *gin.Context) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
||||
buf.WriteString("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n")
|
||||
buf.WriteString(" <url><loc>https://toolbox.pengzhan.dev/</loc><priority>1.0</priority></url>\n")
|
||||
|
||||
// 动态根据注册的工具生成路由
|
||||
for id := range base.Registry {
|
||||
fmt.Fprintf(&buf, " <url><loc>https://toolbox.pengzhan.dev/%s</loc><priority>0.8</priority></url>\n", id)
|
||||
}
|
||||
buf.WriteString("</urlset>")
|
||||
c.Data(http.StatusOK, "application/xml; charset=utf-8", buf.Bytes())
|
||||
})
|
||||
// 3. 模板引擎初始化
|
||||
// 递归加载 web 目录下所有的 .html 文件
|
||||
tmpl, err := template.ParseFS(subFS, "layout.html", "index.html", "tools/*.html")
|
||||
@@ -65,11 +83,32 @@ func main() {
|
||||
|
||||
// 通用页面渲染函数
|
||||
serveIndex := func(c *gin.Context) {
|
||||
data := gin.H{
|
||||
"Title": "Own-Tools",
|
||||
"GA_ID": gaID,
|
||||
}
|
||||
path := c.Request.URL.Path
|
||||
title := "探索工具箱"
|
||||
desc := "基于 Go 语言构建的模块化个人工具箱,提供汉字字帖生成、数字学习等多种实用生产力工具。"
|
||||
keywords := "个人工具箱, 汉字字帖生成, 书法练习, 数字学习, Own-Tools"
|
||||
baseURL := "https://toolbox.pengzhan.dev"
|
||||
canonical := baseURL + path
|
||||
// 针对不同路径设置 SEO 标签
|
||||
switch path {
|
||||
case "/zitie":
|
||||
title = "汉字字帖生成器 - 教学方格与步进式分解"
|
||||
desc = "在线生成 2x3 教学方格字帖和 9 列步进式笔顺分解字帖,支持多种书法字体和古风排版。"
|
||||
keywords = "字帖生成, 笔顺分解, 汉字教学, 书法字帖, 练字"
|
||||
case "/learn-number":
|
||||
title = "趣味数字学习工具"
|
||||
desc = "为儿童设计的数字学习与计数练习工具,生动活泼,寓教于乐。"
|
||||
keywords = "数字学习, 儿童计数, 幼小衔接"
|
||||
}
|
||||
|
||||
data := gin.H{
|
||||
"Title": title,
|
||||
"Description": desc,
|
||||
"Keywords": keywords,
|
||||
"CanonicalURL": canonical,
|
||||
"GA_ID": gaID,
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
// 因为已经 Sub 了,模板名就是文件名
|
||||
if err := tmpl.ExecuteTemplate(&buf, "layout.html", data); err != nil {
|
||||
@@ -78,12 +117,13 @@ func main() {
|
||||
}
|
||||
c.Data(http.StatusOK, "text/html; charset=utf-8", buf.Bytes())
|
||||
}
|
||||
|
||||
r.GET("/", serveIndex)
|
||||
r.GET("/api/tools", func(c *gin.Context) {
|
||||
var list []map[string]string
|
||||
ids := make([]string, 0, len(base.Registry))
|
||||
for id := range base.Registry { ids = append(ids, id) }
|
||||
for id := range base.Registry {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
|
||||
for _, id := range ids {
|
||||
|
||||
Reference in New Issue
Block a user