feat: list markdown files

This commit is contained in:
2025-10-17 17:43:18 +03:00
parent 0cc9367327
commit a256c3e16f
9 changed files with 153 additions and 81 deletions

BIN
api/api

Binary file not shown.

View File

@@ -1,23 +1,28 @@
package main
import (
// "os"
// "fmt"
"fmt"
"io/fs"
"log"
"os"
"path"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// port = os.Getenv("API_PORT") || 8912
router.SetTrustedProxies([]string{"127.0.0.1"}) // TODO: fix
router.Use(cors.Default()) // All origins allowed by default
router.SetTrustedProxies([]string{"127.0.0.1"}) // TODO: fix All origins allowed by default
router.Use(cors.Default())
list := listFiles("/Users/madundead/Syncthing/Obsidian/Personal")
router.GET("/api/v1/ping", func(c *gin.Context) {
router.GET("/api/v1/files", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
"status": "ok",
"data": list,
"status": "ok",
})
})
@@ -25,3 +30,28 @@ func main() {
panic(err)
}
}
func listFiles(dir string) []string {
root := os.DirFS(dir)
mdFiles, err := fs.Glob(root, "**/*.md")
if err != nil {
log.Fatal(err)
}
var files []string
for _, v := range mdFiles {
files = append(files, path.Join(dir, v))
}
return files
}
func readFile(string filePath) []string {
content, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
return content
}