47155ed5e849112477b97098464021c8be1aa6fa
[src/xds/xds-agent.git] / lib / apiv1 / config.go
1 package apiv1
2
3 import (
4         "net/http"
5         "sync"
6
7         "github.com/gin-gonic/gin"
8         "github.com/iotbzh/xds-agent/lib/xdsconfig"
9         common "github.com/iotbzh/xds-common/golib"
10 )
11
12 var confMut sync.Mutex
13
14 // GetConfig returns the configuration
15 func (s *APIService) getConfig(c *gin.Context) {
16         confMut.Lock()
17         defer confMut.Unlock()
18
19         c.JSON(http.StatusOK, s.cfg)
20 }
21
22 // SetConfig sets configuration
23 func (s *APIService) setConfig(c *gin.Context) {
24         // FIXME - must be tested
25         c.JSON(http.StatusNotImplemented, "Not implemented")
26
27         var cfgArg xdsconfig.Config
28
29         if c.BindJSON(&cfgArg) != nil {
30                 common.APIError(c, "Invalid arguments")
31                 return
32         }
33
34         confMut.Lock()
35         defer confMut.Unlock()
36
37         s.log.Debugln("SET config: ", cfgArg)
38
39         if err := s.cfg.UpdateAll(cfgArg); err != nil {
40                 common.APIError(c, err.Error())
41                 return
42         }
43
44         c.JSON(http.StatusOK, s.cfg)
45 }