feat: list file contents
This commit is contained in:
38
api/main.go
38
api/main.go
@@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// "fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"encoding/base64"
|
||||
|
||||
"net/http"
|
||||
|
||||
@@ -17,7 +18,7 @@ func main() {
|
||||
router := gin.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")
|
||||
list := listFiles("/home/madundead/Syncthing/Obsidian/Personal")
|
||||
|
||||
router.GET("/api/v1/files", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -26,6 +27,21 @@ func main() {
|
||||
})
|
||||
})
|
||||
|
||||
router.GET("/api/v1/files/:id", func(c *gin.Context) {
|
||||
id := c.Params.ByName("id")
|
||||
// value, ok := db[user]
|
||||
// if ok {
|
||||
path, _ := base64.StdEncoding.DecodeString(id)
|
||||
|
||||
data, _ := os.ReadFile(string(path))
|
||||
// fmt.Print(string(dat))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": string(data)})
|
||||
// } else {
|
||||
// c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
|
||||
// }
|
||||
})
|
||||
|
||||
if err := router.Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -46,12 +62,12 @@ func listFiles(dir string) []string {
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
func readFile(string filePath) []string {
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading file:", err)
|
||||
return
|
||||
}
|
||||
return content
|
||||
}
|
||||
//
|
||||
// func readFile(string filePath) []string {
|
||||
// content, err := os.ReadFile(filePath)
|
||||
// if err != nil {
|
||||
// fmt.Println("Error reading file:", err)
|
||||
// return
|
||||
// }
|
||||
// return content
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user