- Add GET /api endpoint to list all markdown files - Filter to only include .md files - Return JSON response with files array - Add comprehensive tests for file listing functionality
2.7 KiB
Implementation Summary: File Listing Feature
Problem
The frontend was unable to list existing files because the backend API did not have an endpoint to retrieve the list of files in the data directory.
Root Cause
The backend API handler only supported individual file operations (GET /api/{filename}.md, POST /api/{filename}.md, etc.) but had no endpoint to list all files in the data directory. The frontend was attempting to fetch /api to get the file list, but this route was not configured.
Solution
Added a new endpoint GET /api that returns a JSON list of all markdown files in the data directory.
Changes Made
1. Backend API (backend/internal/api/api.go)
- Added
handleListFiles()method that:- Reads all files from the data directory
- Filters to only include
.mdfiles - Returns JSON response with format:
{"files": ["file1.md", "file2.md", ...]}
- Modified
handleGet()to check if filename is empty and callhandleListFiles()if so
2. Backend Server (backend/internal/server/server.go)
- Added route handler for
/apiwith GET method - Kept existing route handler for
/api/{filename:.+.md}
API Endpoints
New Endpoint
- Method: GET
- Path:
/api - Response:
{"files": ["file1.md", "file2.md", ...]} - Status Codes:
- 200 OK - Success
- 500 Internal Server Error - Failed to read directory
Existing Endpoints (Unchanged)
- GET
/api/{filename}.md- Get file content - POST
/api/{filename}.md- Create file - PUT
/api/{filename}.md- Update file - DELETE
/api/{filename}.md- Delete file
Testing
Added comprehensive tests in backend/tests/file_listing_test.go:
TestFileListing- Verifies multiple markdown files are listedTestFileListingWithNonMarkdownFiles- Verifies only.mdfiles are returnedTestFileListingEmptyDirectory- Verifies empty array for empty directory
All existing tests continue to pass.
Frontend Compatibility
The frontend (frontend/src/App.tsx) already expects the correct response format:
const loadFiles = async () => {
const response = await fetch('/api')
if (response.ok) {
const data = await response.json()
setFiles(data.files || []) // Expects { files: string[] }
}
}
No frontend changes were required.
Verification
The implementation was verified by:
- Running all existing tests - ✅ PASS
- Running new file listing tests - ✅ PASS
- Manual API testing with curl - ✅ WORKING
- Frontend build verification - ✅ SUCCESS
Files Modified
backend/internal/api/api.go- Added file listing functionalitybackend/internal/server/server.go- Added/apiroutebackend/tests/file_listing_test.go- Added comprehensive tests (NEW FILE)