aboutsummaryrefslogtreecommitdiffstats
path: root/lib/apiv1/config.go
blob: 662ec8e85401177ed4da306c894ae14d7bd81e9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package apiv1

import (
	"net/http"
	"sync"

	"github.com/gin-gonic/gin"
	common "github.com/iotbzh/xds-common/golib"
	"github.com/iotbzh/xds-server/lib/xdsconfig"
)

var confMut sync.Mutex

// GetConfig returns server configuration
func (s *APIService) getConfig(c *gin.Context) {
	confMut.Lock()
	defer confMut.Unlock()

	c.JSON(http.StatusOK, s.cfg)
}

// SetConfig sets server configuration
func (s *APIService) setConfig(c *gin.Context) {
	// FIXME - must be tested
	c.JSON(http.StatusNotImplemented, "Not implemented")

	var cfgArg xdsconfig.Config

	if c.BindJSON(&cfgArg) != nil {
		common.APIError(c, "Invalid arguments")
		return
	}

	confMut.Lock()
	defer confMut.Unlock()

	s.log.Debugln("SET config: ", cfgArg)

	if err := s.mfolder.UpdateAll(cfgArg); err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, s.cfg)
}