package dashboard

import (
	"net/http"
	"path"

	"k8s.io/klog/v2"
)

// Asset replies with the contents of the loaded asset from disk
func Asset(assetPath string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		data, err := content.ReadFile(path.Join("assets", assetPath))
		if err != nil {
			klog.Errorf("Error getting asset: %v", err)
			http.Error(w, "Error getting asset", http.StatusInternalServerError)
			return
		}
		_, err = w.Write(data)
		if err != nil {
			klog.Errorf("Error writing asset: %v", err)
		}
	})
}

// StaticAssets replies with a FileServer for all assets
func StaticAssets(prefix string) http.Handler {
	klog.V(3).Infof("stripping prefix: %s", prefix)
	return http.StripPrefix(prefix, http.FileServer(getFileSystem()))
}
