Added /info handler to list the director

master
Tomasz Polgrabia 2024-10-22 23:46:23 +02:00
parent 0643bb9ce9
commit 7ad054f1ef
1 changed files with 29 additions and 0 deletions
2024/10/golang_matrix_demo1

View File

@ -2,6 +2,7 @@ package main
import (
"github.com/gin-gonic/gin"
"os"
"time"
)
@ -13,6 +14,34 @@ func main() {
"datetime": time.Now(),
})
})
r.GET("/info", func(c *gin.Context) {
var directories []map[string]string
dirs, err := os.ReadDir(".")
if err != nil {
c.JSON(500, gin.H{
"message": "it failed",
})
return
}
for i := 0; i < len(dirs); i++ {
m := make(map[string]string)
m["name"] = dirs[i].Name()
if dirs[i].IsDir() {
m["dir"] = "yes"
} else {
m["dir"] = "no"
}
directories = append(directories, m)
}
c.JSON(200, gin.H{
"dirs": directories,
})
})
err := r.Run("localhost:8080")
if err != nil {
panic("Couldn't run the server. It failed with the error: " + err.Error())