Files
zzyxyz_go_api/internal/bookmarks/bookmark.go
zzy 9cab95c0f7 feat(bookmark): 添加 401 和 403 响应引用并完善错误定义
在 bookmark.yaml 配置文件中,为多个接口路径添加了 '401' 和 '403' 状态码的响应引用,
分别指向 components 中定义的 Unauthorized 和 Forbidden 响应。同时在 components
部分补充了 Forbidden 响应的定义,增强了 API 文档的完整性与规范性。

feat(user_np): 新增用户信息接口与基础错误结构定义

在 user_np.yaml 中新增了 /auth/info 路径下的 GET 和 PUT 接口,用于获取和保存用户信息。
同时,在 components 中定义了 ServerInternalError 响应和 Error 结构体,统一错误返回格式,
提升接口一致性与可维护性。

feat(vfs): 调整内容类型为 text/plain 并增强节点名称校验逻辑

将 vfs.yaml 中涉及二进制流传输的内容类型由 application/octet-stream 修改为 text/plain,
简化数据处理方式。同时在 vfs.go 模型中新增 CheckNameValid 方法,用于校验节点名称合法性,
防止非法字符(如斜杠)造成路径问题。

refactor(bookmark): 优化 API Key 验证逻辑并暴露更新时间字段

重构 BookMarksImpl 的 validateApiKey 函数,简化认证判断流程,并将 adminToken 从指针改为字符串常量。
此外,在 bookmarkModel2Res 函数中新增 UpdatedAt 字段,使书签响应包含更新时间信息。

feat(user_np): 实现用户信息相关接口占位函数

在 UserNPImpl 中新增 GetUserInfo 和 SaveUserInfo 两个方法的占位实现,为后续业务逻辑开发做好准备。

refactor(vfs): 使用文本请求体并加强服务节点操作校验

修改 vfs_impl.go 中读取请求体的方式,由 io.Reader 改为直接解引用文本内容,提升处理效率。
更新 CreateVFSNode、GetVFSNode 和 UpdateVFSNode 方法中对请求体和响应体的处理逻辑,
统一使用文本格式,增强代码一致性与健壮性。

feat(vfs): 为书签代理服务添加认证 Token 支持

在 vfs_bookmark.go 中为 VfsBookMarkService 结构体增加 token 字段,并在调用 bookmark 服务各接口时,
通过 HTTP 请求头设置 X-BookMark-Token,确保服务间通信的安全性与权限控制。
2025-09-26 14:42:22 +08:00

237 lines
5.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// internal/handlers/note_link.go
package bookmarks
import (
"net/http"
api "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/bookmarks_server"
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/bookmarks/models"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type BookMarksImpl struct {
db *gorm.DB
}
var adminToken string = "random_token"
func validateApiKey(apiKey string) bool {
return apiKey == adminToken
}
func AuthMiddleware() api.MiddlewareFunc {
return func(c *gin.Context) {
// 检查当前请求是否需要认证
if _, exists := c.Get(api.ApiKeyAuthScopes); exists {
// 提取 API Key
apiKey := c.GetHeader("X-BookMark-Token")
// 验证 API Key您需要实现这个逻辑
if !validateApiKey(apiKey) {
c.JSON(http.StatusUnauthorized, api.Error{
Errtype: "Unauthorized",
Message: "Invalid or missing API key",
})
c.Abort()
return
}
}
c.Next()
}
}
func NewBookMarkPermission() (*api.GinServerOptions, error) {
return &api.GinServerOptions{
Middlewares: []api.MiddlewareFunc{AuthMiddleware()},
}, nil
}
func NewBookMarks(dbPath string) (*BookMarksImpl, error) {
var err error
var db *gorm.DB
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
if err != nil {
return nil, err
}
// 自动迁移表结构
err = db.AutoMigrate(&models.Bookmark{})
if err != nil {
return nil, err
}
return &BookMarksImpl{db: db}, nil
}
func (b *BookMarksImpl) FindBMFromExternalID(externalID int64) (models.Bookmark, error) {
var db = b.db
var bookmark models.Bookmark
// 使用ExternalID查询书签
if err := db.Where("external_id = ?", externalID).First(&bookmark).Error; err != nil {
return bookmark, err
}
return bookmark, nil
}
// CreateBookmark implements api.ServerInterface.
func (b *BookMarksImpl) CreateBookmark(c *gin.Context, id int64) {
var db = b.db
var req api.BookmarkRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, api.Error{
Errtype: "ParameterError",
Message: "Invalid request parameters",
})
return
}
// 检查外部ID是否已经存在
var existingBookmark models.Bookmark
result := db.Where("external_id = ?", id).First(&existingBookmark)
if result.Error == nil {
// ExternalID已存在返回冲突错误
c.JSON(http.StatusConflict, api.Error{
Errtype: "ConflictError",
Message: "Bookmark with this External ID already exists",
})
return
} else if result.Error != gorm.ErrRecordNotFound {
// 数据库查询出错
c.JSON(http.StatusInternalServerError, api.Error{
Errtype: "DatabaseError",
Message: "Database query error",
})
return
}
bookmark := bookmarkReq2Model(req)
bookmark.ExternalID = id // 设置外部ID
if err := db.Create(&bookmark).Error; err != nil {
c.JSON(http.StatusInternalServerError, api.Error{
Errtype: "DatabaseError",
Message: "Failed to create bookmark",
})
return
}
response := bookmarkModel2Res(bookmark)
c.JSON(http.StatusCreated, response)
}
// DeleteBookmark implements api.ServerInterface.
func (b *BookMarksImpl) DeleteBookmark(c *gin.Context, id int64) {
var db = b.db
var bookmark models.Bookmark
// 使用ExternalID删除书签
if err := db.Where("external_id = ?", id).Delete(&bookmark).Error; err != nil {
c.JSON(http.StatusInternalServerError, api.Error{
Errtype: "DatabaseError",
Message: "Failed to delete bookmark",
})
return
}
c.Status(http.StatusNoContent)
}
// GetBookmark implements api.ServerInterface.
func (b *BookMarksImpl) GetBookmark(c *gin.Context, id int64) {
var db = b.db
var bookmark models.Bookmark
// 使用ExternalID查询书签
if err := db.Where("external_id = ?", id).First(&bookmark).Error; err != nil {
c.JSON(http.StatusNotFound, api.Error{
Errtype: "NotFoundError",
Message: "Bookmark not found",
})
return
}
// 构造响应
response := bookmarkModel2Res(bookmark)
c.JSON(http.StatusOK, response)
}
// UpdateBookmark implements api.ServerInterface.
func (b *BookMarksImpl) UpdateBookmark(c *gin.Context, id int64) {
var db = b.db
var req api.BookmarkRequest
// 绑定请求参数
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, api.Error{
Errtype: "ParameterError",
Message: "Invalid request parameters",
})
return
}
// 查找要更新的书签使用ExternalID
var bookmark models.Bookmark
if err := db.Where("external_id = ?", id).First(&bookmark).Error; err != nil {
c.JSON(http.StatusNotFound, api.Error{
Errtype: "NotFoundError",
Message: "Bookmark not found",
})
return
}
// 更新书签字段
if req.Name != "" {
bookmark.Name = req.Name
}
if req.Link != nil {
bookmark.Link = req.Link
}
if req.Detail != nil {
bookmark.Detail = req.Detail
}
if req.Description != nil {
bookmark.Description = req.Description
}
// 保存更新
if err := db.Save(&bookmark).Error; err != nil {
c.JSON(http.StatusInternalServerError, api.Error{
Errtype: "DatabaseError",
Message: "Failed to update bookmark",
})
return
}
// 构造响应
response := bookmarkModel2Res(bookmark)
c.JSON(http.StatusOK, response)
}
func bookmarkReq2Model(req api.BookmarkRequest) models.Bookmark {
return models.Bookmark{
Name: req.Name,
Link: req.Link,
Detail: req.Detail,
Description: req.Description,
}
}
func bookmarkModel2Res(bookmark models.Bookmark) api.BookmarkResponse {
return api.BookmarkResponse{
Id: bookmark.ID,
Name: bookmark.Name,
Link: bookmark.Link,
Detail: bookmark.Detail,
Description: bookmark.Description,
CreatedAt: bookmark.CreatedAt,
UpdatedAt: bookmark.UpdatedAt,
}
}
// Make sure we conform to ServerInterface
var _ api.ServerInterface = (*BookMarksImpl)(nil)