2021-11-15 08:37:58 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-04-04 02:15:26 +00:00
|
|
|
"path/filepath"
|
2021-11-15 08:37:58 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2022-05-03 02:19:30 +00:00
|
|
|
// AssetOverride allows overriding and serving of specific embedded assets from disk.
|
|
|
|
func AssetOverride(root string, strip int, next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
2022-09-16 01:19:16 +00:00
|
|
|
if root == "" {
|
|
|
|
return next
|
|
|
|
}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
handler := fasthttp.FSHandler(root, strip)
|
|
|
|
stripper := fasthttp.NewPathSlashesStripper(strip)
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
asset := filepath.Join(root, string(stripper(ctx)))
|
2021-11-15 08:37:58 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
if _, err := os.Stat(asset); err != nil {
|
2021-11-15 08:37:58 +00:00
|
|
|
next(ctx)
|
2022-04-04 02:15:26 +00:00
|
|
|
|
|
|
|
return
|
2021-11-15 08:37:58 +00:00
|
|
|
}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
handler(ctx)
|
2021-11-15 08:37:58 +00:00
|
|
|
}
|
|
|
|
}
|