Files
zzyxyz_go_api/internal/vfs/vfs.go
zzy 35e79e54f1 refactor(vfs): 重构VFS模块,拆分数据访问逻辑与路径解析逻辑
将原先的 `vfs.go` 文件中的功能进行拆分,创建了独立的 DAO 层文件 `vfs_dao.go`
和路径处理文件 `vfs_path.go`,以提升代码结构清晰度和可维护性。

- 将数据库操作相关方法迁移至 `VfsDAO` 结构体中
- 新增 `vfs_dao.go` 文件用于管理底层数据访问对象
- 新增 `vfs_path.go` 文件专门处理路径解析逻辑
- 移除了原 `vfs.go` 中的数据库初始化、用户及节点操作等冗余代码
2025-09-29 00:42:45 +08:00

83 lines
1.9 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.

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
}