-
How to use |
Beta Was this translation helpful? Give feedback.
Answered by
vearutop
Jan 23, 2022
Replies: 1 comment
-
Copied from #12 for better visibility. Gin's standard package main
import (
"embed"
"io/fs"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/vearutop/statigz"
)
// Declare your embedded assets.
//go:embed assets/*
var st embed.FS
func main() {
s, err := fs.Sub(st, "assets")
if err != nil {
log.Fatal(err)
}
router := gin.Default()
ss := http.StripPrefix("/static", statigz.FileServer(s.(fs.ReadDirFS)))
router.GET("/static/*w", func(c *gin.Context) {
ss.ServeHTTP(c.Writer, c.Request)
})
if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
} Or, if your embedding path matches http path, even simpler. package main
import (
"embed"
"log"
"github.com/gin-gonic/gin"
"github.com/vearutop/statigz"
)
// Declare your embedded assets.
//go:embed static/*
var st embed.FS
func main() {
router := gin.Default()
ss := statigz.FileServer(st)
router.GET("/static/*w", func(c *gin.Context) {
ss.ServeHTTP(c.Writer, c.Request)
})
if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
vearutop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copied from #12 for better visibility.
Gin's standard
StaticFS
is not suitable, because it operates on file system level and misses enough control to directly substitute compressed assets. However you can mountstatigz
as regularhttp.Handler
.