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 {
|
2021-11-15 08:37:58 +00:00
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2022-04-04 02:15:26 +00:00
|
|
|
if root == "" {
|
|
|
|
next(ctx)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-11-15 08:37:58 +00:00
|
|
|
|
2022-04-04 02:15:26 +00:00
|
|
|
_, err := os.Stat(filepath.Join(root, string(fasthttp.NewPathSlashesStripper(strip)(ctx))))
|
|
|
|
if 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
|
|
|
|
|
|
|
fasthttp.FSHandler(root, strip)(ctx)
|
2021-11-15 08:37:58 +00:00
|
|
|
}
|
|
|
|
}
|