7 "github.com/gin-gonic/gin"
8 common "github.com/iotbzh/xds-common/golib"
9 "github.com/iotbzh/xds-server/lib/xsapiv1"
12 // getFolders returns all folders configuration
13 func (s *APIService) getFolders(c *gin.Context) {
14 c.JSON(http.StatusOK, s.mfolders.GetConfigArr())
17 // getFolder returns a specific folder configuration
18 func (s *APIService) getFolder(c *gin.Context) {
19 id, err := s.mfolders.ResolveID(c.Param("id"))
21 common.APIError(c, err.Error())
24 f := s.mfolders.Get(id)
26 common.APIError(c, "Invalid id")
30 c.JSON(http.StatusOK, (*f).GetConfig())
33 // addFolder adds a new folder to server config
34 func (s *APIService) addFolder(c *gin.Context) {
35 var cfgArg xsapiv1.FolderConfig
36 if c.BindJSON(&cfgArg) != nil {
37 common.APIError(c, "Invalid arguments")
41 s.Log.Debugln("Add folder config: ", cfgArg)
43 newFld, err := s.mfolders.Add(cfgArg)
45 common.APIError(c, err.Error())
49 // Create xds-project.conf file
50 // FIXME: move to folders.createUpdate func (but gin context needed)
51 fld := s.mfolders.Get(newFld.ID)
52 prjConfFile := (*fld).GetFullPath("xds-project.conf")
53 if !common.Exists(prjConfFile) {
54 fd, err := os.OpenFile(prjConfFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
56 common.APIError(c, err.Error())
59 fd.WriteString("# XDS project settings\n")
60 fd.WriteString("export XDS_SERVER_URL=" + c.Request.Host + "\n")
61 fd.WriteString("export XDS_PROJECT_ID=" + newFld.ID + "\n")
62 if newFld.DefaultSdk == "" {
63 sdks := s.sdks.GetAll()
64 newFld.DefaultSdk = sdks[0].ID
66 fd.WriteString("export XDS_SDK_ID=" + newFld.DefaultSdk + "\n")
70 c.JSON(http.StatusOK, newFld)
73 // syncFolder force synchronization of folder files
74 func (s *APIService) syncFolder(c *gin.Context) {
75 id, err := s.mfolders.ResolveID(c.Param("id"))
77 common.APIError(c, err.Error())
80 s.Log.Debugln("Sync folder id: ", id)
82 err = s.mfolders.ForceSync(id)
84 common.APIError(c, err.Error())
88 c.JSON(http.StatusOK, "")
91 // delFolder deletes folder from server config
92 func (s *APIService) delFolder(c *gin.Context) {
93 id, err := s.mfolders.ResolveID(c.Param("id"))
95 common.APIError(c, err.Error())
99 s.Log.Debugln("Delete folder id ", id)
101 delEntry, err := s.mfolders.Delete(id)
103 common.APIError(c, err.Error())
106 c.JSON(http.StatusOK, delEntry)
109 // updateFolder update some field of a folder
110 func (s *APIService) updateFolder(c *gin.Context) {
111 id, err := s.mfolders.ResolveID(c.Param("id"))
113 common.APIError(c, err.Error())
117 s.Log.Debugln("Update folder id ", id)
119 var cfgArg xsapiv1.FolderConfig
120 if c.BindJSON(&cfgArg) != nil {
121 common.APIError(c, "Invalid arguments")
125 upFld, err := s.mfolders.Update(id, cfgArg)
127 common.APIError(c, err.Error())
130 c.JSON(http.StatusOK, upFld)