add api route, but not use
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package api
|
||||
@@ -0,0 +1,68 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
|
||||
"git.trj.tw/golang/go-file-serve/module/config"
|
||||
"git.trj.tw/golang/go-file-serve/module/context"
|
||||
)
|
||||
|
||||
// GetFileList -
|
||||
func GetFileList(c *context.Context) {
|
||||
var err error
|
||||
limit := -1
|
||||
skip := 0
|
||||
if str, ok := c.GetQuery("limit"); ok && len(str) > 0 {
|
||||
limit, err = strconv.Atoi(str)
|
||||
if err != nil {
|
||||
limit = -1
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = -1
|
||||
}
|
||||
}
|
||||
if str, ok := c.GetQuery("skip"); ok && len(str) > 0 {
|
||||
skip, err = strconv.Atoi(str)
|
||||
if err != nil {
|
||||
skip = 0
|
||||
}
|
||||
if skip < 0 {
|
||||
skip = 0
|
||||
}
|
||||
}
|
||||
|
||||
conf := config.GetConf()
|
||||
if conf == nil {
|
||||
c.ServerError()
|
||||
return
|
||||
}
|
||||
|
||||
fileInfo, err := ioutil.ReadDir(conf.FilePath)
|
||||
if err != nil {
|
||||
c.ServerError()
|
||||
return
|
||||
}
|
||||
|
||||
totalItem := len(fileInfo)
|
||||
|
||||
if skip < totalItem {
|
||||
if limit > 0 {
|
||||
fileInfo = fileInfo[skip : skip+limit]
|
||||
} else {
|
||||
fileInfo = fileInfo[skip:]
|
||||
}
|
||||
} else {
|
||||
fileInfo = nil
|
||||
}
|
||||
|
||||
resArr := make([]string, 0, len(fileInfo))
|
||||
for _, v := range fileInfo {
|
||||
resArr = append(resArr, v.Name())
|
||||
}
|
||||
|
||||
c.Success(map[string]interface{}{
|
||||
"files": resArr,
|
||||
"total": totalItem,
|
||||
})
|
||||
}
|
||||
+12
-4
@@ -1,12 +1,15 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/http"
|
||||
|
||||
"git.trj.tw/golang/go-file-serve/module/context"
|
||||
"git.trj.tw/golang/go-file-serve/routes/api"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// NewEngine -
|
||||
func NewEngine () *gin.Engine {
|
||||
func NewEngine() *gin.Engine {
|
||||
engine := gin.New()
|
||||
engine.Use(gin.Logger())
|
||||
engine.Use(gin.Recovery())
|
||||
@@ -16,9 +19,14 @@ func NewEngine () *gin.Engine {
|
||||
|
||||
// SetRoutes -
|
||||
func SetRoutes(r *gin.Engine) {
|
||||
r.GET("/", func (c *gin.Context) {
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "success",
|
||||
})
|
||||
})
|
||||
|
||||
apiGroup := r.Group("/api")
|
||||
{
|
||||
apiGroup.GET("/files", context.PatchContext(api.GetFileList))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user