Files
zzyxyz_go_api/internal/handlers/bookmark.go
zzy 1e81e603de 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 参数,并优化响应结构体定义。
2025-09-23 21:52:51 +08:00

241 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 handlers
import (
"net/http"
api "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/bookmarks"
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/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
func validateApiKey(apiKey string) bool {
if adminToken != nil && apiKey == *adminToken {
return true
}
return false
}
func AuthMiddleware() api.MiddlewareFunc {
return func(c *gin.Context) {
// 检查当前请求是否需要认证
if _, exists := c.Get(api.ApiKeyAuthScopes); exists {
// 提取 API Key
apiKey := c.GetHeader("X-BookMark-Token")
return
// 验证 API Key您需要实现这个逻辑
if apiKey == "" || !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 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,
}
}
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)
}
// Make sure we conform to ServerInterface
var _ api.ServerInterface = (*BookMarksImpl)(nil)