gres
资源管理
- 可将任意的文件打包为
Go
内容,支持开发者自定义加解密; - 资源管理器内容完全基于内存,并且内容只读,无法动态修改;
- 资源管理器默认整合支持到了WebServer、配置管理、模板引擎中;
- 任意文件如网站静态文件、配置文件等可编译到二进制文件中,也可编译到发布的可执行文件中;
- 开发者可只需编译发布一个可执行文件,除了方便了软件分发,也为保护软件知识产权内容提供了可能;
使用方式:
接口文档:
https://godoc.org/github.com/gogf/gf/os/gres
func Add(content []byte, prefix ...string) error
func Contains(path string) bool
func Dump()
func GetContent(path string) []byte
func IsEmpty() bool
func Get(path string) *File
func GetWithIndex(path string, indexFiles []string) *File
func ScanDir(path string, pattern string, recursive ...bool) []*File
func ScanDirFile(path string, pattern string, recursive ...bool) []*File
func Load(path string, prefix ...string) error
func Pack(srcPaths string, keyPrefix ...string) ([]byte, error)
func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error
func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error
func Unpack(path string) ([]*File, error)
type File
func (f *File) Close() error
func (f *File) Content() []byte
func (f *File) FileInfo() os.FileInfo
func (f *File) Name() string
func (f *File) Open() (io.ReadCloser, error)
func (f *File) Read(b []byte) (n int, err error)
func (f *File) Readdir(count int) ([]os.FileInfo, error)
func (f *File) Seek(offset int64, whence int) (int64, error)
func (f *File) Stat() (os.FileInfo, error)
type Resource
func Instance(name ...string) *Resource
func New() *Resource
func (r *Resource) Add(content []byte, prefix ...string) error
func (r *Resource) Contains(path string) bool
func (r *Resource) Dump()
func (r *Resource) Get(path string) *File
func (r *Resource) GetWithIndex(path string, indexFiles []string) *File
func (r *Resource) IsEmpty() bool
func (r *Resource) Load(path string, prefix ...string) error
func (r *Resource) ScanDir(path string, pattern string, recursive ...bool) []*File
func (r *Resource) ScanDirFile(path string, pattern string, recursive ...bool) []*File
- 通过
Pack*
/Unpack*
方法可以实现对任意文件的打包/解包功能,可以打包到二进制文件或者Go代码文件; - 资源管理由
Resource
对象实现,可实现对打包内容的添加,文件的检索查找,以及对目录的遍历等功能; - 资源文件由
File
对象实现,该文件对象和os.File
文件对象类似,并且该对象实现了http.File
接口; ScanDir
用于针对于特定目录下的文件/目录检索,并且支持递归检索;ScanDirFile
用于针对于特定目录下的文件检索,并且支持递归检索;- 此外,
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
文件内容类似于:
import (
_ "my-app/boot"
)
随后可以在项目的任何地方使用gres
模块来访问打包的资源文件。
如果使用
GF
推荐的,在目录结构中会存在boot
目录(对应包名也是boot
),用于程序启动设置。因此如果将Go资源文件打包到boot
目录下,那么将会被自动编译进可执行文件中。
可以通过Dump()
方法打印出当前资源管理器中所有的文件列表,输出内容类似于:
我们将项目根目录下的public
和config
目录打包为data.bin
二进制文件,并通过gaes
加密算法对生成的二进制内容进行加密。
package main
import (
"github.com/gogf/gf/crypto/gaes"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/gres"
)
var (
CryptoKey = []byte("x76cgqt36i9c863bzmotuf8626dxiwu0")
)
func main() {
binContent, err := gres.Pack("public,config")
if err != nil {
panic(err)
}
binContent, err = gaes.Encrypt(binContent, CryptoKey)
if err != nil {
panic(err)
}
if err := gfile.PutBytes("data.bin", binContent); err != nil {
panic(err)
}
最后,我们使用打印出添加成功的文件列表。