github.com/IRelaxxx/servefiles/v3@v3.4.6/echo_adapter/handler.go (about) 1 package echo_adapter 2 3 import ( 4 "net/http" 5 "strings" 6 "time" 7 8 "github.com/IRelaxxx/servefiles/v3" 9 "github.com/labstack/echo/v4" 10 "github.com/spf13/afero" 11 ) 12 13 // EchoAssets is merely an adapter for servefiles.Assets with the same API and with an 14 // additional HandlerFunc method. 15 type EchoAssets servefiles.Assets 16 17 // NewAssetHandler creates an Assets value. The parameter is the directory containing the asset files; 18 // this can be absolute or relative to the directory in which the server process is started. 19 // 20 // This function cleans (i.e. normalises) the asset path. 21 func NewAssetHandler(assetPath string) *EchoAssets { 22 return (*EchoAssets)(servefiles.NewAssetHandler(assetPath)) 23 } 24 25 // NewAssetHandlerFS creates an Assets value for a given filesystem. 26 func NewAssetHandlerFS(fs afero.Fs) *EchoAssets { 27 return (*EchoAssets)(servefiles.NewAssetHandlerFS(fs)) 28 } 29 30 // StripOff alters the handler to strip off a specified number of segments from the path before 31 // looking for the matching asset. For example, if StripOff(2) has been applied, the requested 32 // path "/a/b/c/d/doc.js" would be shortened to "c/d/doc.js". 33 // 34 // The returned handler is a new copy of the original one. 35 func (a EchoAssets) StripOff(unwantedPrefixSegments int) *EchoAssets { 36 return (*EchoAssets)((servefiles.Assets)(a).StripOff(unwantedPrefixSegments)) 37 } 38 39 // WithMaxAge alters the handler to set the specified max age on the served assets. 40 // 41 // The returned handler is a new copy of the original one. 42 func (a EchoAssets) WithMaxAge(maxAge time.Duration) *EchoAssets { 43 return (*EchoAssets)((servefiles.Assets)(a).WithMaxAge(maxAge)) 44 } 45 46 // WithNotFound alters the handler so that 404-not found cases are passed to a specified 47 // handler. Without this, the default handler is the one provided in the net/http package. 48 // 49 // The returned handler is a new copy of the original one. 50 func (a EchoAssets) WithNotFound(notFound http.Handler) *EchoAssets { 51 a.NotFound = notFound 52 return &a 53 } 54 55 // WithSPA alters the handler so that all requestet files without a file extention instead return index.html 56 // 57 // The returned handler is a new copy of the original one. 58 func (a EchoAssets) WithSPA() *EchoAssets { 59 a.Spa = true 60 return &a 61 } 62 63 // HandlerFunc gets the asset handler as an Echo handler. The handler is 64 // registered using a catch-all path such as "/files/*". The same 65 // match-any pattern can be passed in, in which case it is stripped off 66 // the leading part of the URL path seem by the asset handler. 67 func (a *EchoAssets) HandlerFunc(path string) echo.HandlerFunc { 68 trim := 0 69 if strings.HasSuffix(path, "/*") { 70 trim = len(path) - 2 71 } else { 72 panic(path + ": path must end /* or be blank") 73 } 74 75 return func(c echo.Context) error { 76 req := c.Request() 77 req.URL.Path = req.URL.Path[trim:] 78 (*servefiles.Assets)(a).ServeHTTP(c.Response(), c.Request()) 79 return nil 80 } 81 } 82 83 // Register registers the asset handler with an Echo engine using the specified 84 // path to handle GET and HEAD requests. 85 // 86 // The handler is registered using a catch-all path such as "/files/*". This 87 // pattern will be stripped off the leading part of the URL path seem by the 88 // asset handler when determining the file to be served. 89 func (a *EchoAssets) Register(e *echo.Echo, path string) { 90 if !strings.HasSuffix(path, "/*") { 91 panic(path + ": path must end /*") 92 } 93 h := a.HandlerFunc(path) 94 e.GET(path, h) 95 e.HEAD(path, h) 96 }