From e0798046fcb030c79dda0d0d2c8c1ae12e76a5ca Mon Sep 17 00:00:00 2001 From: haopengzhan Date: Mon, 23 Feb 2026 02:04:56 -0800 Subject: [PATCH] Fix: Force add cmd/toolbox directory --- cmd/toolbox/main.go | 86 ++++++++++++++ cmd/toolbox/web/index.html | 224 +++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 cmd/toolbox/main.go create mode 100644 cmd/toolbox/web/index.html diff --git a/cmd/toolbox/main.go b/cmd/toolbox/main.go new file mode 100644 index 0000000..a287d19 --- /dev/null +++ b/cmd/toolbox/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "embed" + "flag" + "io/fs" + "log" + "net/http" + "os" + "strconv" + "toolbox/pkg/base" + _ "toolbox/pkg/jitie" // 匿名导入以触发 init() + + "github.com/gin-gonic/gin" +) + +//go:embed web/* +var webFS embed.FS + +func main() { + // 定义端口 Flag + portFlag := flag.Int("port", 0, "端口号 (优先于环境变量 PORT)") + flag.Parse() + + // 禁用 Gin 的默认重定向逻辑,防止与静态资源冲突 + r := gin.New() + r.Use(gin.Logger(), gin.Recovery()) + r.RedirectTrailingSlash = false + r.RedirectFixedPath = false + + // 1. 自动发现并注册工具路由 + api := r.Group("/api") + { + for id, tool := range base.Registry { + log.Printf("Loading tool: %s (%s)", tool.Name(), id) + if err := tool.Init(); err != nil { + log.Fatalf("Failed to initialize tool %s: %v", id, err) + } + group := api.Group("/" + id) + tool.RegisterRoutes(group) + } + } + + // 2. 静态资源路由 (内嵌前端) + // 根路径直接返回 index.html 内容,不使用文件服务器转发 + r.GET("/", func(c *gin.Context) { + content, err := webFS.ReadFile("web/index.html") + if err != nil { + c.String(http.StatusNotFound, "index.html not found") + return + } + c.Data(http.StatusOK, "text/html; charset=utf-8", content) + }) + + // 如果将来有 css/js,可以挂载到 /web 路径 + subFS, _ := fs.Sub(webFS, "web") + r.StaticFS("/web", http.FS(subFS)) + + // 3. 工具列表发现接口 + r.GET("/api/tools", func(c *gin.Context) { + var list []map[string]string + for _, t := range base.Registry { + list = append(list, map[string]string{ + "id": t.ID(), + "name": t.Name(), + "desc": t.Description(), + }) + } + c.JSON(http.StatusOK, list) + }) + + // 4. 确定最终使用的端口 + var finalPort string + if *portFlag > 0 { + finalPort = strconv.Itoa(*portFlag) + } else if envPort := os.Getenv("PORT"); envPort != "" { + finalPort = envPort + } else { + finalPort = "8080" + } + + log.Printf("Toolbox server starting on :%s", finalPort) + if err := r.Run(":" + finalPort); err != nil { + log.Fatalf("Server failed: %v", err) + } +} diff --git a/cmd/toolbox/web/index.html b/cmd/toolbox/web/index.html new file mode 100644 index 0000000..16e5931 --- /dev/null +++ b/cmd/toolbox/web/index.html @@ -0,0 +1,224 @@ + + + + + + 个人工具箱 - 汉字字帖生成器 + + + +
+
+

汉字字帖工具箱

+

生成标准笔顺教学字帖,支持 A4/Letter 打印

+
+ +
+
+
+
+ + +
+
+
+
+ + +
+
+ + +
+ +
+
+
+ +
+ +
+ +
+

在上方输入汉字并点击按钮,即可生成高清矢量字帖预览

+
+
+ + + +