package main import ( "embed" "fmt" "log" "net/http" "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/handlers" "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs" "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/services/vfsdriver" "github.com/gin-gonic/gin" "github.com/spf13/viper" ) //go:embed config/* dist/* var staticFiles embed.FS func main() { config, err := vfs.LoadConfig() if err != nil { log.Fatal("Failed to load config:", err) } viper.SafeWriteConfigAs("vfs_config.yaml") if config.Server.Mode == "debug" { gin.SetMode(gin.DebugMode) } else { gin.SetMode(gin.ReleaseMode) } router := gin.Default() router.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) api_router := router.Group("/api") { if server, err := vfs.NewVfsHandler(config); err != nil { log.Fatal("Failed to create bookmarks server:", err) } else { vfs.RegisterVFSRoutes(api_router, server) // 示例:在你的服务初始化代码中 bookmarkService, err := vfsdriver.NewVfsBookMarkService("http://localhost:8081/api") // 替换为实际的 bookmark 服务地址 if err != nil { log.Fatal("Failed to create bookmark service client:", err) } server.Proxy.RegisterProxy(bookmarkService) } handlers.TodoHandler(api_router) } // FIXME 可能有更好的方式实现这个代码 // 提供嵌入的静态文件访问 - OpenAPI YAML 文件和 dist 目录 router.GET("/config/*filename", func(ctx *gin.Context) { // 直接修改请求路径实现映射 r := ctx.Request originalPath := r.URL.Path filename := ctx.Param("filename") filepath := fmt.Sprintf("/config/%s/%s.yaml", filename, filename) r.URL.Path = filepath fs := http.FileServer(http.FS(staticFiles)) fs.ServeHTTP(ctx.Writer, r) // 恢复原始路径 r.URL.Path = originalPath }) router.GET("/swagger/*filepath", func(ctx *gin.Context) { // 直接修改请求路径实现映射 r := ctx.Request originalPath := r.URL.Path // 将 /swagger/* 映射为 /dist/* r.URL.Path = "/dist" + ctx.Param("filepath") fs := http.FileServer(http.FS(staticFiles)) fs.ServeHTTP(ctx.Writer, r) // 恢复原始路径 r.URL.Path = originalPath }) var listener = config.Server.Address log.Printf("Starting server at http://%s", listener) log.Printf("Swagger UI: http://%s/swagger/index.html", listener) log.Fatal(router.Run(listener)) }