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 }