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:
@@ -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)
|
||||
|
||||
@@ -31,6 +31,7 @@ func NewServer(host string, port int, handler http.Handler, log *logrus.Logger)
|
||||
|
||||
func (s *Server) Start() error {
|
||||
router := mux.NewRouter()
|
||||
router.Handle("/api", s.handler).Methods("GET")
|
||||
router.Handle("/api/{filename:.+.md}", s.handler)
|
||||
router.PathPrefix("/").Handler(http.FileServer(http.Dir("frontend/dist")))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user