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