122 lines
2.8 KiB
Go
122 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"flag"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"toolbox/pkg/base"
|
|
_ "toolbox/pkg/zitie" // 匿名导入以触发 init()
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
//go:embed web/*
|
|
var webFS embed.FS
|
|
|
|
func main() {
|
|
// 定义参数
|
|
portFlag := flag.Int("port", 0, "端口号 (优先于环境变量 PORT)")
|
|
gaFlag := flag.String("ga", "", "Google Analytics ID (例如: G-XXXXXXXXXX)")
|
|
flag.Parse()
|
|
|
|
// 环境变量支持
|
|
gaID := *gaFlag
|
|
if gaID == "" {
|
|
gaID = os.Getenv("GA_ID")
|
|
}
|
|
|
|
if os.Getenv("GIN_MODE") == "release" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
r := gin.New()
|
|
r.Use(gin.Logger(), gin.Recovery())
|
|
r.RedirectTrailingSlash = false
|
|
r.RedirectFixedPath = false
|
|
_ = r.SetTrustedProxies(nil)
|
|
|
|
// 1. 自动发现并注册工具路由
|
|
api := r.Group("/api")
|
|
{
|
|
for id, tool := range base.Registry {
|
|
if err := tool.Init(); err != nil {
|
|
log.Fatalf("Failed to initialize tool %s: %v", id, err)
|
|
}
|
|
tool.RegisterRoutes(api.Group("/" + id))
|
|
}
|
|
}
|
|
|
|
// 2. 静态资源与页面
|
|
subFS, _ := fs.Sub(webFS, "web")
|
|
r.StaticFS("/static", http.FS(subFS))
|
|
|
|
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
|
|
}
|
|
|
|
// 动态注入 Google Analytics
|
|
if gaID != "" {
|
|
gaScript := `
|
|
<!-- Google tag (gtag.js) -->
|
|
<script async src="https://www.googletagmanager.com/gtag/js?id=` + gaID + `"></script>
|
|
<script>
|
|
window.dataLayer = window.onLayer || [];
|
|
function gtag(){dataLayer.push(arguments);}
|
|
gtag('js', new Date());
|
|
gtag('config', '` + gaID + `');
|
|
</script>`
|
|
content = bytes.Replace(content, []byte("</head>"), []byte(gaScript+"</head>"), 1)
|
|
}
|
|
|
|
c.Data(http.StatusOK, "text/html; charset=utf-8", content)
|
|
})
|
|
// 改为 /static 避免与 /web 混淆
|
|
|
|
// 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. 全路径回退 (SPA Routing)
|
|
// 任何不匹配 API 或静态资源的请求,都返回 index.html
|
|
r.NoRoute(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)
|
|
})
|
|
|
|
// 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)
|
|
}
|
|
}
|