Added /info handler to list the director
parent
0643bb9ce9
commit
7ad054f1ef
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,6 +14,34 @@ func main() {
|
||||||
"datetime": time.Now(),
|
"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")
|
err := r.Run("localhost:8080")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Couldn't run the server. It failed with the error: " + err.Error())
|
panic("Couldn't run the server. It failed with the error: " + err.Error())
|
||||||
|
|
Loading…
Reference in New Issue