feat(bookmark): 初始化书签服务并配置路由与权限控制
新增书签服务主程序,使用 Gin 框架搭建 HTTP 服务器,并注册书签相关接口处理器。 配置 CORS 中间件以支持跨域请求。服务监听端口为 8081。 feat(user_np): 初始化用户权限服务并注册认证接口 新增用户权限服务主程序,使用 Gin 框架搭建 HTTP 服务器,并注册登录、注册及修改密码等接口处理器。 服务监听端口为 8082。 refactor(config): 重构 OpenAPI 配置文件结构并拆分模块 将原有合并的 OpenAPI 配置文件按功能模块拆分为 bookmark 和 user_np 两个独立目录, 分别管理各自的 server、client 及 API 定义文件,便于后续维护和扩展。 refactor(vfs): 调整虚拟文件系统 API 接口路径与参数定义 更新 VFS API 配置文件,修改部分接口路径及参数结构, 如将文件路径参数由 path 转为 query 参数,并优化响应结构体定义。
This commit is contained in:
45
main.go
45
main.go
@ -2,17 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
bookmarks "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/bookmarks"
|
||||
vfs "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/vfs"
|
||||
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/handlers"
|
||||
vfsdriver "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/handlers/vfs_driver"
|
||||
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//go:generate go tool oapi-codegen -config config/bookmark_cfg.yaml config/bookmark.yaml
|
||||
//go:generate go tool oapi-codegen -config config/vfs_cfg.yaml config/vfs.yaml
|
||||
//go:generate go tool oapi-codegen -config config/bookmark/client.yaml config/bookmark/bookmark.yaml
|
||||
//go:generate go tool oapi-codegen -config config/bookmark/server.yaml config/bookmark/bookmark.yaml
|
||||
//go:generate go tool oapi-codegen -config config/vfs/server.yaml config/vfs/vfs.yaml
|
||||
//go:generate go tool oapi-codegen -config config/vfs/server.yaml config/vfs/vfs.yaml
|
||||
//go:generate go tool oapi-codegen -config config/user_np/server.yaml config/user_np/user_np.yaml
|
||||
|
||||
//go:embed config/* dist/*
|
||||
var staticFiles embed.FS
|
||||
|
||||
@ -28,24 +35,38 @@ func main() {
|
||||
|
||||
api_router := router.Group("/api")
|
||||
{
|
||||
// create a type that satisfies the `api.ServerInterface`,
|
||||
// which contains an implementation of every operation from the generated code
|
||||
if server, err := handlers.NewBookMarks("./data/bookmark.sqlite3"); err != nil {
|
||||
if vfsImpl, err := models.NewVfs("./data/vfs.sqlite3"); err != nil {
|
||||
log.Fatal("Failed to create vfs server:", err)
|
||||
} else if server, err := handlers.NewVfsHandler(*vfsImpl); err != nil {
|
||||
log.Fatal("Failed to create bookmarks server:", err)
|
||||
} else if permission, err := handlers.NewBookMarkPermission(); err != nil || permission == nil {
|
||||
log.Fatal("Failed to create bookmarks permission:", err)
|
||||
} else {
|
||||
bookmarks.RegisterHandlersWithOptions(api_router, server, *permission)
|
||||
vfs.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(&handlers.ProxyEntry{Name: "bk", Proxy: bookmarkService})
|
||||
}
|
||||
|
||||
handlers.TodoHandler(api_router)
|
||||
}
|
||||
|
||||
// FIXME 可能有更好的方式实现这个代码
|
||||
// 提供嵌入的静态文件访问 - OpenAPI YAML 文件和 dist 目录
|
||||
router.GET("/config/*filepath", func(ctx *gin.Context) {
|
||||
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, ctx.Request)
|
||||
fs.ServeHTTP(ctx.Writer, r)
|
||||
|
||||
// 恢复原始路径
|
||||
r.URL.Path = originalPath
|
||||
})
|
||||
router.GET("/swagger/*filepath", func(ctx *gin.Context) {
|
||||
// 直接修改请求路径实现映射
|
||||
|
Reference in New Issue
Block a user