gres资源管理

    • 可将任意的文件打包为Go内容,支持开发者自定义加解密;
    • 资源管理器内容完全基于内存,并且内容只读,无法动态修改;
    • 资源管理器默认整合支持到了WebServer、配置管理、模板引擎中;
    • 任意文件如网站静态文件、配置文件等可编译到二进制文件中,也可编译到发布的可执行文件中;
    • 开发者可只需编译发布一个可执行文件,除了方便了软件分发,也为保护软件知识产权内容提供了可能;

    使用方式

    接口文档

    https://godoc.org/github.com/gogf/gf/os/gres

    1. func Add(content []byte, prefix ...string) error
    2. func Contains(path string) bool
    3. func Dump()
    4. func GetContent(path string) []byte
    5. func IsEmpty() bool
    6. func Get(path string) *File
    7. func GetWithIndex(path string, indexFiles []string) *File
    8. func ScanDir(path string, pattern string, recursive ...bool) []*File
    9. func ScanDirFile(path string, pattern string, recursive ...bool) []*File
    10. func Load(path string, prefix ...string) error
    11. func Pack(srcPaths string, keyPrefix ...string) ([]byte, error)
    12. func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error
    13. func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error
    14. func Unpack(path string) ([]*File, error)
    15. type File
    16. func (f *File) Close() error
    17. func (f *File) Content() []byte
    18. func (f *File) FileInfo() os.FileInfo
    19. func (f *File) Name() string
    20. func (f *File) Open() (io.ReadCloser, error)
    21. func (f *File) Read(b []byte) (n int, err error)
    22. func (f *File) Readdir(count int) ([]os.FileInfo, error)
    23. func (f *File) Seek(offset int64, whence int) (int64, error)
    24. func (f *File) Stat() (os.FileInfo, error)
    25. type Resource
    26. func Instance(name ...string) *Resource
    27. func New() *Resource
    28. func (r *Resource) Add(content []byte, prefix ...string) error
    29. func (r *Resource) Contains(path string) bool
    30. func (r *Resource) Dump()
    31. func (r *Resource) Get(path string) *File
    32. func (r *Resource) GetWithIndex(path string, indexFiles []string) *File
    33. func (r *Resource) IsEmpty() bool
    34. func (r *Resource) Load(path string, prefix ...string) error
    35. func (r *Resource) ScanDir(path string, pattern string, recursive ...bool) []*File
    36. func (r *Resource) ScanDirFile(path string, pattern string, recursive ...bool) []*File
    1. 通过Pack*/Unpack*方法可以实现对任意文件的打包/解包功能,可以打包到二进制文件或者Go代码文件;
    2. 资源管理由Resource对象实现,可实现对打包内容的添加,文件的检索查找,以及对目录的遍历等功能;
    3. 资源文件由File对象实现,该文件对象和os.File文件对象类似,并且该对象实现了http.File接口;
    4. ScanDir用于针对于特定目录下的文件/目录检索,并且支持递归检索;
    5. ScanDirFile用于针对于特定目录下的文件检索,并且支持递归检索;
    6. 此外,gres资源管理模块提供了默认的Resource对象,并通过包方法提供了对该默认对象的操作;

    开发者对文件/目录的打包可以自定义通过Pack*方法实现,并通过Unpack*方法自行对二进制文件进行打包;或者通过import方式导入打包的Go资源文件。

    这种是比较常用的方式,我们这里直接使用gf工具来进行打包,不添加自定义的加密。

    比较推荐的方式是将Go文件直接生成到boot启动目录,并设置生成Go文件的包名为boot,这样该资源文件将会被自动引入到项目中。我们将项目的config,public,template三个目录的文件打包到Go文件,打包命令为:gf pack config,public,template boot/data.go -n boot

    生成的Go文件内容类似于:

    1. import (
    2. _ "my-app/boot"
    3. )

    随后可以在项目的任何地方使用gres模块来访问打包的资源文件。

    如果使用GF推荐的,在目录结构中会存在boot目录(对应包名也是boot),用于程序启动设置。因此如果将Go资源文件打包到boot目录下,那么将会被自动编译进可执行文件中。

    可以通过Dump()方法打印出当前资源管理器中所有的文件列表,输出内容类似于:

    我们将项目根目录下的publicconfig目录打包为data.bin二进制文件,并通过gaes加密算法对生成的二进制内容进行加密。

    1. package main
    2. import (
    3. "github.com/gogf/gf/crypto/gaes"
    4. "github.com/gogf/gf/os/gfile"
    5. "github.com/gogf/gf/os/gres"
    6. )
    7. var (
    8. CryptoKey = []byte("x76cgqt36i9c863bzmotuf8626dxiwu0")
    9. )
    10. func main() {
    11. binContent, err := gres.Pack("public,config")
    12. if err != nil {
    13. panic(err)
    14. }
    15. binContent, err = gaes.Encrypt(binContent, CryptoKey)
    16. if err != nil {
    17. panic(err)
    18. }
    19. if err := gfile.PutBytes("data.bin", binContent); err != nil {
    20. panic(err)
    21. }

    最后,我们使用打印出添加成功的文件列表。