diff --git a/main.go b/main.go index 681601d..d3cf23c 100644 --- a/main.go +++ b/main.go @@ -21,15 +21,26 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, prefix string) erro cleanPath = "/index.html" } + // Try to read the exact file first content, err := frontend.ReadFile(prefix + cleanPath) - if err != nil { - return err + ext := strings.ToLower(filepath.Ext(cleanPath)) + + // If file not found OR the path has no extension (likely a route path), serve index.html + if err != nil || ext == "" { + content, err = frontend.ReadFile(prefix + "/index.html") + if err != nil { + return err + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Write(content) + return nil } - ext := strings.ToLower(filepath.Ext(cleanPath)) + // For actual files, set the correct MIME type mimeType := mime.TypeByExtension(ext) if mimeType == "" { - mimeType = "application/octet-stream" + // Try to detect content type from the content itself + mimeType = http.DetectContentType(content) } w.Header().Set("Content-Type", mimeType) @@ -94,11 +105,6 @@ func handleNotes(w http.ResponseWriter, r *http.Request) { `) if err != nil { log.Printf("ERROR: Failed to query notes: %v", err) - log.Printf("ERROR: Failed to scan note row: %v", err) - log.Printf("ERROR: Failed to insert note: %v", err) - log.Printf("ERROR: Failed to query note: %v", err) - log.Printf("ERROR: Failed to update note: %v", err) - log.Printf("ERROR: Failed to delete note: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -121,7 +127,6 @@ func handleNotes(w http.ResponseWriter, r *http.Request) { var note Note if err := json.NewDecoder(r.Body).Decode(¬e); err != nil { log.Printf("ERROR: Failed to decode note: %v", err) - log.Printf("ERROR: Failed to decode note update: %v", err) http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -198,10 +203,7 @@ func handleFrontend(w http.ResponseWriter, r *http.Request) { } err := serveStaticFile(w, r, "build") - if err != nil { - // If file not found, serve index.html for client-side routing - if err := serveStaticFile(w, r, "build"); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } + if err != nil { // if serveStaticFile returns an error, it has already tried to serve index.html as fallback + http.Error(w, err.Error(), http.StatusInternalServerError) } }