本目录当前提供的是静态文件服务用法说明,而不是一个可直接 go run 的独立示例程序。
如果你想看可执行示例,建议从以下位置组合验证:
examples/basic/main.godocs/usage.mdpkg/static/zipfs.gogithub.com/darkit/gin 当前支持以下静态资源能力:
Static / StaticFSStaticFileEmbedFS / EmbedFilepkg/static 下的 RegisterZipFS / RegisterZipFilepackage main
import gin "github.com/darkit/gin"
func main() {
e := gin.New()
r := e.Router()
r.Static("/assets", "./public")
_ = e.Run(":8080")
}
访问示例:
http://localhost:8080/assets/style.css → ./public/style.csshttp://localhost:8080/assets/js/app.js → ./public/js/app.jsr.StaticFile("/favicon.ico", "./assets/favicon.ico")
import "net/http"
r.StaticFS("/files", http.Dir("./uploads"))
package main
import (
"embed"
gin "github.com/darkit/gin"
)
//go:embed dist/*
var embedFS embed.FS
func main() {
e := gin.New()
r := e.Router()
r.EmbedFS("/static", embedFS, "dist")
r.EmbedFile("/favicon.ico", embedFS, "dist/favicon.ico")
_ = e.Run(":8080")
}
Zip 相关能力位于 github.com/darkit/gin/pkg/static。
package main
import (
gin "github.com/darkit/gin"
"github.com/darkit/gin/pkg/static"
)
func main() {
e := gin.New()
r := e.Router()
zfs, err := static.NewZipFileSystem(static.ZipFSConfig{
ZipPath: "app.zip",
URLPrefix: "/app",
})
if err != nil {
panic(err)
}
static.RegisterZipFS(r.RouterGroup, "/app", zfs)
_ = e.Run(":8080")
}
单文件 Zip 注册:
zf, err := static.NewZipFile("app.zip", "index.html", nil)
if err != nil {
panic(err)
}
static.RegisterZipFile(r.RouterGroup, "/index", zf)
zfs, err := static.NewZipFileSystem(static.ZipFSConfig{
ZipPath: "protected.zip",
URLPrefix: "/secure",
Password: "mypassword",
})
if err != nil {
panic(err)
}
static.RegisterZipFS(r.RouterGroup, "/secure", zfs)
zfs, err := static.NewZipFileSystem(static.ZipFSConfig{
ZipPath: "app.zip",
URLPrefix: "/app",
HotReload: true,
CheckInterval: 3 * time.Second,
})
if err != nil {
panic(err)
}
static.RegisterZipFS(r.RouterGroup, "/app", zfs)
zfs.StartHotReload()
defer zfs.Stop()
也可使用辅助 option:
cfg := static.NewZipFSConfig(
"app.zip",
"/app",
static.WithPassword("mypassword"),
static.WithHotReload(3*time.Second),
)
zfs, err := static.NewZipFileSystem(cfg)
Engine 也直接暴露静态相关注册方法:
e.Static("/assets", "./public")
e.StaticFile("/favicon.ico", "./favicon.ico")
e.StaticFS("/files", http.Dir("./uploads"))
EmbedFS 或 CDNdocs/usage.mdpkg/static/zipfs.goREADME.md