package main import ( "encoding/json" "net/http" "os" "strings" ) func main() { http.HandleFunc("/files", func(w http.ResponseWriter, r *http.Request) { files, err := os.ReadDir("./static") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var names []string for _, f := range files { name := f.Name() if strings.HasSuffix(name, ".jpg") || strings.HasSuffix(name, ".png") || strings.HasSuffix(name, ".gif") { names = append(names, name) } } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(names) }) fs := http.FileServer(http.Dir("./static")) http.Handle("/", fs) http.ListenAndServe(":8080", nil) }