github.com/IRelaxxx/servefiles/v3@v3.4.6/gin_adapter/handler.go (about)

     1  package gin_adapter
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"github.com/IRelaxxx/servefiles/v3"
     8  	"github.com/gin-gonic/gin"
     9  	"github.com/spf13/afero"
    10  )
    11  
    12  // GinAssets is merely an adapter for servefiles.Assets with the same API and with an
    13  // additional HandlerFunc method.
    14  type GinAssets servefiles.Assets
    15  
    16  // NewAssetHandler creates an Assets value. The parameter is the directory containing the asset files;
    17  // this can be absolute or relative to the directory in which the server process is started.
    18  //
    19  // This function cleans (i.e. normalises) the asset path.
    20  func NewAssetHandler(assetPath string) *GinAssets {
    21  	return (*GinAssets)(servefiles.NewAssetHandler(assetPath))
    22  }
    23  
    24  // NewAssetHandlerFS creates an Assets value for a given filesystem.
    25  func NewAssetHandlerFS(fs afero.Fs) *GinAssets {
    26  	return (*GinAssets)(servefiles.NewAssetHandlerFS(fs))
    27  }
    28  
    29  // StripOff alters the handler to strip off a specified number of segments from the path before
    30  // looking for the matching asset. For example, if StripOff(2) has been applied, the requested
    31  // path "/a/b/c/d/doc.js" would be shortened to "c/d/doc.js".
    32  //
    33  // The returned handler is a new copy of the original one.
    34  func (a GinAssets) StripOff(unwantedPrefixSegments int) *GinAssets {
    35  	return (*GinAssets)((servefiles.Assets)(a).StripOff(unwantedPrefixSegments))
    36  }
    37  
    38  // WithMaxAge alters the handler to set the specified max age on the served assets.
    39  //
    40  // The returned handler is a new copy of the original one.
    41  func (a GinAssets) WithMaxAge(maxAge time.Duration) *GinAssets {
    42  	return (*GinAssets)((servefiles.Assets)(a).WithMaxAge(maxAge))
    43  }
    44  
    45  // WithNotFound alters the handler so that 404-not found cases are passed to a specified
    46  // handler. Without this, the default handler is the one provided in the net/http package.
    47  //
    48  // The returned handler is a new copy of the original one.
    49  func (a GinAssets) WithNotFound(notFound http.Handler) *GinAssets {
    50  	a.NotFound = notFound
    51  	return &a
    52  }
    53  
    54  // WithSPA alters the handler so that all requestet files without a file extention instead return index.html
    55  //
    56  // The returned handler is a new copy of the original one.
    57  func (a GinAssets) WithSPA() *GinAssets {
    58  	a.Spa = true
    59  	return &a
    60  }
    61  
    62  // HandlerFunc gets the asset handler as a Gin handler. The handler is
    63  // registered using a catch-all path such as "/files/*filepath". The name
    64  // of the catch-all parameter is passed in here (for example "filepath").
    65  func (a *GinAssets) HandlerFunc(paramName string) gin.HandlerFunc {
    66  	return func(c *gin.Context) {
    67  		req := c.Request
    68  		req.URL.Path = c.Param(paramName)
    69  		(*servefiles.Assets)(a).ServeHTTP(c.Writer, c.Request)
    70  	}
    71  }