feat(api): add file listing endpoint

- 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
This commit is contained in:
2026-02-06 21:23:59 -05:00
parent 2a9e793971
commit a074f5a854
4 changed files with 224 additions and 193 deletions

View File

@@ -38,10 +38,36 @@ func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func (h *APIHandler) handleListFiles(w http.ResponseWriter, r *http.Request) {
h.log.Info("GET request for file listing")
files, err := os.ReadDir(h.dataDir)
if err != nil {
h.log.Errorf("Error reading data directory: %v", err)
h.writeError(w, http.StatusInternalServerError, "failed to list files")
return
}
filenames := []string{}
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".md" {
filenames = append(filenames, file.Name())
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string][]string{"files": filenames})
}
func (h *APIHandler) handleGet(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filename := vars["filename"]
if filename == "" {
h.handleListFiles(w, r)
return
}
h.log.Infof("GET request for file: %s", filename)
filepath := filepath.Join(h.dataDir, filename)