将 bookmark 和 user_np 服务的生成配置分离为 server 和 client 包, 并更新了相应的导入路径。同时更新了 OpenAPI 配置文件中的包名、输出路径及 启用 strict-server 模式以增强类型安全。 此外,同步更新了 VFS 服务的客户端和服务端生成配置,并完善了其 OpenAPI 错误响应定义与二进制内容支持,增强了 API 的规范性和健壮性。 修复了部分权限校验逻辑,并调整了中间件注册方式以适配新的严格模式接口。
81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package vfs
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
"path/filepath"
|
||
"sync"
|
||
|
||
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/models"
|
||
"github.com/casbin/casbin/v2"
|
||
"github.com/casbin/casbin/v2/model"
|
||
fileadapter "github.com/casbin/casbin/v2/persist/file-adapter"
|
||
)
|
||
|
||
type VfsImpl struct {
|
||
vfs *models.Vfs
|
||
enfocer *casbin.Enforcer
|
||
config VFSConfig
|
||
proxyTable []*ProxyEntry // 动态代理表
|
||
proxyMutex sync.RWMutex // 保护代理表的读写锁
|
||
}
|
||
|
||
// 在NewVfsHandler中注册中间件
|
||
func NewVfsHandler(config *Config) (*VfsImpl, error) {
|
||
var err error
|
||
|
||
vfs, err := models.NewVfs(config.VFS.DbPath)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
var policyPath = config.VFS.PolicyPath
|
||
// 检查策略文件是否存在,如果不存在则创建
|
||
if _, err := os.Stat(policyPath); os.IsNotExist(err) {
|
||
// 确保目录存在
|
||
dir := filepath.Dir(policyPath)
|
||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||
log.Fatalf("error: failed to create policy directory: %s", err)
|
||
}
|
||
|
||
// 创建空的策略文件
|
||
file, err := os.Create(policyPath)
|
||
if err != nil {
|
||
log.Fatalf("error: failed to create policy file: %s", err)
|
||
}
|
||
file.Close()
|
||
|
||
log.Printf("Created policy file: %s", policyPath)
|
||
}
|
||
|
||
a := fileadapter.NewAdapter(policyPath)
|
||
if a == nil {
|
||
log.Fatalf("error: adapter: %s", err)
|
||
}
|
||
|
||
m, err := model.NewModelFromString(CasbinModel)
|
||
if err != nil {
|
||
log.Fatalf("error: model: %s", err)
|
||
}
|
||
|
||
e, err := casbin.NewEnforcer(m, a)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
log.Printf("Admin Token: %s", config.VFS.AdminToken)
|
||
log.Printf("Register Token: %s", config.VFS.RegisterToken)
|
||
|
||
impl := &VfsImpl{
|
||
vfs: vfs,
|
||
enfocer: e,
|
||
config: config.VFS,
|
||
proxyTable: make([]*ProxyEntry, 0),
|
||
proxyMutex: sync.RWMutex{},
|
||
}
|
||
|
||
// 注册中间件
|
||
// 注意:在注册handler时需要传入中间件
|
||
return impl, nil
|
||
}
|