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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AssetOverrideMiddleware allows overriding and serving of specific embedded assets from disk.
|
2022-04-04 02:15:26 +00:00
|
|
|
func AssetOverrideMiddleware(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
|
|
|
}
|
|
|
|
}
|