Skip to content

Setting up statigz with gin #13

Answered by vearutop
vearutop asked this question in Q&A
Discussion options

You must be logged in to vote

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 mount statigz as regular http.Handler.

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.

Replies: 1 comment

Comment options

vearutop
Jan 23, 2022
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by vearutop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant