package utils import ( "fmt" "github.com/goccy/go-json" "html/template" "log" "mime" "os" "strings" ) func ViteAsset(src string, attrs ...string) template.HTML { file, err := os.ReadFile("dist/.vite/manifest.json") if err != nil { log.Fatalln(err) } var viteManifestData map[string]map[string]interface{} if err := json.Unmarshal(file, &viteManifestData); err != nil { log.Fatalln(err) } src = strings.TrimPrefix(src, "/") var element string info, ok := viteManifestData[src] if !ok { return "" } filenameSplit := strings.Split(info["file"].(string), ".") if len(filenameSplit) < 2 { return "" } fileExt := filenameSplit[len(filenameSplit)-1] mimeType := mime.TypeByExtension("." + fileExt) if strings.HasPrefix(mimeType, "image/") { element = fmt.Sprintf(``, info["file"].(string)) } return template.HTML(element) }