fix: resolve JSON.parse error in frontend API calls

- Add error handling for JSON encoding in all backend handlers
- Add CORS support to backend server
- Add proxy configuration for frontend development server
- Improve error handling in frontend API calls
- Build frontend to create static files for backend serving
- Add comprehensive error messages and user feedback

Fixes issue where frontend received malformed JSON responses from backend API.
This commit is contained in:
2026-02-05 20:14:12 -05:00
parent 551ae1890d
commit c9003a208a
11 changed files with 238 additions and 23 deletions

View File

@@ -31,9 +31,13 @@ func (h *Handlers) ListFiles() http.HandlerFunc {
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"files": files,
})
}); err != nil {
h.logger.Errorf("Failed to encode response: %v", err)
h.writeError(w, http.StatusInternalServerError, "failed to encode response")
return
}
}
}
@@ -62,9 +66,13 @@ func (h *Handlers) CreateFile() http.HandlerFunc {
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
if err := json.NewEncoder(w).Encode(map[string]string{
"message": "file created successfully",
})
}); err != nil {
h.logger.Errorf("Failed to encode response: %v", err)
h.writeError(w, http.StatusInternalServerError, "failed to encode response")
return
}
}
}
@@ -86,10 +94,14 @@ func (h *Handlers) GetFile() http.HandlerFunc {
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"filename": filename,
"content": string(content),
})
}); err != nil {
h.logger.Errorf("Failed to encode response: %v", err)
h.writeError(w, http.StatusInternalServerError, "failed to encode response")
return
}
}
}
@@ -120,9 +132,13 @@ func (h *Handlers) UpdateFile() http.HandlerFunc {
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
if err := json.NewEncoder(w).Encode(map[string]string{
"message": "file updated successfully",
})
}); err != nil {
h.logger.Errorf("Failed to encode response: %v", err)
h.writeError(w, http.StatusInternalServerError, "failed to encode response")
return
}
}
}
@@ -143,16 +159,23 @@ func (h *Handlers) DeleteFile() http.HandlerFunc {
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
if err := json.NewEncoder(w).Encode(map[string]string{
"message": "file deleted successfully",
})
}); err != nil {
h.logger.Errorf("Failed to encode response: %v", err)
h.writeError(w, http.StatusInternalServerError, "failed to encode response")
return
}
}
}
func (h *Handlers) writeError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]string{
if err := json.NewEncoder(w).Encode(map[string]string{
"error": message,
})
}); err != nil {
h.logger.Errorf("Failed to encode error response: %v", err)
w.Write([]byte(`{"error": "failed to encode error response"}`))
}
}

View File

@@ -10,6 +10,7 @@ import (
"markdown-editor/internal/logger"
"markdown-editor/internal/storage"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
)
@@ -73,6 +74,15 @@ func (s *Server) setupRoutes() {
frontendDir = frontendDirEnv
}
s.router.PathPrefix("/").Handler(http.FileServer(http.Dir(frontendDir)))
// Enable CORS
cors := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000", "http://localhost:8080"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Content-Type", "Authorization"},
AllowCredentials: true,
})
s.router.Use(cors.Handler)
}
func (s *Server) serve() error {