Files
zzyxyz_go_api/main.go
zzy 36b311ff86 ```
feat(bookmark): 实现书签和文件夹的增删改查功能

- 添加 GetFolderDefaultRoot 方法用于获取默认根文件夹
- 实现 bookmark 和 folder 的请求/模型/响应转换函数
- 完善 CreateBookmark、CreateFolder、DeleteBookmark、DeleteFolder 等接口逻辑
- 支持删除空文件夹,非空文件夹禁止删除
- 修复根文件夹创建逻辑并添加错误处理
- 删除未实现的示例路由和无用代码

build: 引入 mage 构建工具支持多平台编译

- 添加 magefile.go 实现跨平台构建脚本
- 更新 go.mod 和 go.sum 引入 github.com/magefile/mage 依赖
- 在 README 中补充 mage 使用说明和 VSCode 配置

docs: 更新 README 并添加 mage 使用示例

- 添加 govulncheck 和 mage 构建相关文档
- 补充 VSCode gopls 配置说明

ci: 更新 .gitignore 忽略 bin 目录和可执行文件

- 添加 bin/ 到忽略列表
- 统一忽略 *.exe 和 *.sqlite3 文件

refactor(main): 优化主函数启动逻辑与日志输出

- 移除示例 helloworld 路由
- 设置 gin 为 ReleaseMode 模式
- 统一服务监听地址为变量,并增加启动日志提示
```
2025-09-21 15:45:37 +08:00

67 lines
1.7 KiB
Go

package main
import (
"embed"
"log"
"net/http"
"git.zzyxyz.com/zzy/zzyxyz_go_api/gen/api"
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/handlers"
"github.com/gin-gonic/gin"
)
//go:generate go tool oapi-codegen -config config/cfg.yaml config/api.yaml
//go:embed config/api.yaml dist/*
var staticFiles embed.FS
func main() {
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")
{
// create a type that satisfies the `api.ServerInterface`,
// which contains an implementation of every operation from the generated code
if server, err := handlers.NewBookMarks("user.sqlite3"); err != nil {
log.Fatal("Failed to create bookmarks server:", err)
} else {
api.RegisterHandlers(api_router, server)
}
handlers.TodoHandler(api_router)
}
// FIXME 可能有更好的方式实现这个代码
// 提供嵌入的静态文件访问 - OpenAPI YAML 文件和 dist 目录
router.GET("/swagger.yaml", func(c *gin.Context) {
file, _ := staticFiles.ReadFile("config/api.yaml")
c.Data(http.StatusOK, "application/x-yaml", file)
})
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 = "127.0.0.1:8080"
log.Printf("Starting server at http://%s", listener)
log.Printf("Swagger UI: http://%s/swagger/index.html", listener)
log.Fatal(router.Run(listener))
}