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:
2026-03-01 09:54:04 -08:00
parent c3d0dee806
commit 5c185da0a9
9 changed files with 657 additions and 229 deletions
+46 -6
View File
@@ -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 {
+50 -1
View File
@@ -4,6 +4,24 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}} | Own-Tools</title>
<meta name="description" content="{{.Description}}">
<meta name="keywords" content="{{.Keywords}}">
<link rel="canonical" href="{{.CanonicalURL}}">
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website">
<meta property="og:url" content="{{.CanonicalURL}}">
<meta property="og:title" content="{{.Title}} | Own-Tools">
<meta property="og:description" content="{{.Description}}">
<meta property="og:image" content="/static/og-image.png">
<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="{{.CanonicalURL}}">
<meta property="twitter:title" content="{{.Title}} | Own-Tools">
<meta property="twitter:description" content="{{.Description}}">
<meta property="twitter:image" content="/static/og-image.png">
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🖋️</text></svg>">
{{if .GA_ID}}
@@ -126,10 +144,41 @@
renderCurrentPath();
}
const metaConfig = {
'welcome': {
title: '探索工具箱',
desc: '基于 Go 语言构建的模块化个人工具箱,提供汉字字帖生成、数字学习等多种实用生产力工具。',
keywords: '个人工具箱, 汉字字帖生成, 书法练习, 数字学习, Own-Tools'
},
'zitie': {
title: '汉字字帖生成器 - 教学方格与步进式分解',
desc: '在线生成 2x3 教学方格字帖和 9 列步进式笔顺分解字帖,支持多种书法字体和古风排版。',
keywords: '字帖生成, 笔顺分解, 汉字教学, 书法字帖, 练字'
},
'learn-number': {
title: '趣味数字学习工具',
desc: '为儿童设计的数字学习与计数练习工具,生动活泼,寓教于乐。',
keywords: '数字学习, 儿童计数, 幼小衔接'
}
};
function renderCurrentPath() {
const path = window.location.pathname.replace('/', '') || 'welcome';
// 隐藏所有面板
// 1. 更新标题与元数据
const meta = metaConfig[path] || metaConfig['welcome'];
document.title = `${meta.title} | Own-Tools`;
document.querySelector('meta[name="description"]').setAttribute('content', meta.desc);
document.querySelector('meta[name="keywords"]').setAttribute('content', meta.keywords);
// 更新 Open Graph 标签 (可选,社交分享主要看服务端渲染)
const canonical = `https://toolbox.pengzhan.dev/${path === 'welcome' ? '' : path}`;
document.querySelector('link[rel="canonical"]').setAttribute('href', canonical);
document.querySelector('meta[property="og:title"]').setAttribute('content', `${meta.title} | Own-Tools`);
document.querySelector('meta[property="og:description"]').setAttribute('content', meta.desc);
document.querySelector('meta[property="og:url"]').setAttribute('content', canonical);
// 2. 隐藏所有面板
document.querySelectorAll('.tool-panel').forEach(p => p.classList.remove('active'));
// 显示目标面板