Initial commit: Modular personal toolbox with high-fidelity Chinese stroke order tool and CI/CD
Build and Push Docker Image / build (push) Failing after 39s
Build and Push Docker Image / build (push) Failing after 39s
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user