package vfs import ( "log" "os" "path/filepath" "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/models" vfs_service "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/services" "github.com/casbin/casbin/v2" "github.com/casbin/casbin/v2/model" fileadapter "github.com/casbin/casbin/v2/persist/file-adapter" ) type VfsImpl struct { Core *vfs_service.VfsCoreService Proxy *vfs_service.ProxyService User *vfs_service.UserService DAO *models.VfsDAO Enfocer *casbin.Enforcer Config VFSConfig } // 在NewVfsHandler中注册中间件 func NewVfsHandler(config *Config) (*VfsImpl, error) { var err error dao, err := models.NewVfsDAO(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{ Core: vfs_service.NewVfsCoreService(dao), Proxy: vfs_service.NewProxyService(), User: vfs_service.NewUserService(dao, e), DAO: dao, Enfocer: e, Config: config.VFS, } // 注册中间件 // 注意:在注册handler时需要传入中间件 return impl, nil }