Add folder interface and support native pathmap folder type.
[src/xds/xds-server.git] / lib / apiv1 / folders.go
1 package apiv1
2
3 import (
4         "net/http"
5
6         "github.com/gin-gonic/gin"
7         common "github.com/iotbzh/xds-common/golib"
8         "github.com/iotbzh/xds-server/lib/folder"
9 )
10
11 // getFolders returns all folders configuration
12 func (s *APIService) getFolders(c *gin.Context) {
13         c.JSON(http.StatusOK, s.mfolders.GetConfigArr())
14 }
15
16 // getFolder returns a specific folder configuration
17 func (s *APIService) getFolder(c *gin.Context) {
18         f := s.mfolders.Get(c.Param("id"))
19         if f == nil {
20                 common.APIError(c, "Invalid id")
21                 return
22         }
23
24         c.JSON(http.StatusOK, (*f).GetConfig())
25 }
26
27 // addFolder adds a new folder to server config
28 func (s *APIService) addFolder(c *gin.Context) {
29         var cfgArg folder.FolderConfig
30         if c.BindJSON(&cfgArg) != nil {
31                 common.APIError(c, "Invalid arguments")
32                 return
33         }
34
35         s.log.Debugln("Add folder config: ", cfgArg)
36
37         newFld, err := s.mfolders.Add(cfgArg)
38         if err != nil {
39                 common.APIError(c, err.Error())
40                 return
41         }
42
43         c.JSON(http.StatusOK, newFld)
44 }
45
46 // delFolder deletes folder from server config
47 func (s *APIService) delFolder(c *gin.Context) {
48         id := c.Param("id")
49
50         s.log.Debugln("Delete folder id ", id)
51
52         delEntry, err := s.mfolders.Delete(id)
53         if err != nil {
54                 common.APIError(c, err.Error())
55                 return
56         }
57         c.JSON(http.StatusOK, delEntry)
58
59 }