将 bookmark.go 重命名为 main.go,并调整包引用路径。将 bookmarks 和 user_np 两个模块的处理逻辑合并到同一个服务中,统一注册路由。同时更新了相关 API 的引用路径,确保生成代码与内部实现正确绑定。 此外,移除了独立的 user_np 服务入口文件,其功能已整合至 bookmark 服务中。 配置文件中调整了 user_np 和 vfs 服务的端口及部分接口定义,完善了用户 相关操作的路径参数和请求体结构。
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
api "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/vfs"
|
|
"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/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 {
|
|
api.RegisterHandlers(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.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))
|
|
}
|