7 "github.com/gin-gonic/gin"
8 "github.com/iotbzh/xds-agent/lib/xdsconfig"
11 const apiBaseURL = "/api/v1"
14 type APIService struct {
16 apiRouter *gin.RouterGroup
20 // NewAPIV1 creates a new instance of API service
21 func NewAPIV1(ctx *Context) *APIService {
24 apiRouter: ctx.webServer.router.Group(apiBaseURL),
28 s.apiRouter.GET("/version", s.getVersion)
30 s.apiRouter.GET("/config", s.getConfig)
31 s.apiRouter.POST("/config", s.setConfig)
33 s.apiRouter.GET("/browse", s.browseFS)
35 s.apiRouter.GET("/projects", s.getProjects)
36 s.apiRouter.GET("/projects/:id", s.getProject)
37 s.apiRouter.PUT("/projects/:id", s.updateProject)
38 s.apiRouter.POST("/projects", s.addProject)
39 s.apiRouter.POST("/projects/sync/:id", s.syncProject)
40 s.apiRouter.DELETE("/projects/:id", s.delProject)
42 s.apiRouter.POST("/exec", s.execCmd)
43 s.apiRouter.POST("/exec/:id", s.execCmd)
44 s.apiRouter.POST("/signal", s.execSignalCmd)
46 s.apiRouter.GET("/events", s.eventsList)
47 s.apiRouter.POST("/events/register", s.eventsRegister)
48 s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
53 // Stop Used to stop/close created services
54 func (s *APIService) Stop() {
55 for _, svr := range s.xdsServers {
60 // AddXdsServer Add a new XDS Server to the list of a server
61 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
63 var exist, tempoID bool
66 // First check if not already exist and update it
67 if svr, exist = s.xdsServers[cfg.ID]; exist {
69 // Update: Found, so just update some settings
70 svr.ConnRetry = cfg.ConnRetry
72 tempoID = svr.IsTempoID()
73 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
77 // URL differ or not connected, so need to reconnect
82 // Create a new server object
83 if cfg.APIBaseURL == "" {
84 cfg.APIBaseURL = apiBaseURL
86 if cfg.APIPartialURL == "" {
87 cfg.APIPartialURL = "/server/" + strconv.Itoa(s.serverIndex)
88 s.serverIndex = s.serverIndex + 1
91 // Create a new XDS Server
92 svr = NewXdsServer(s.Context, cfg)
94 svr.SetLoggerOutput(s.Config.LogVerboseOut)
96 // Passthrough routes (handle by XDS Server)
97 grp := s.apiRouter.Group(svr.PartialURL)
98 svr.SetAPIRouterGroup(grp)
100 // Declare passthrough routes
101 s.sdksPassthroughInit(svr)
104 // Established connection
107 // Delete temporary ID with it has been replaced by right Server ID
108 if tempoID && !svr.IsTempoID() {
109 delete(s.xdsServers, cfg.ID)
113 s.xdsServers[svr.ID] = svr
116 if err == nil && svr.Connected {
117 err = s.projects.Init(svr)
123 // DelXdsServer Delete an XDS Server from the list of a server
124 func (s *APIService) DelXdsServer(id string) error {
125 if _, exist := s.xdsServers[id]; !exist {
126 return fmt.Errorf("Unknown Server ID %s", id)
128 // Don't really delete, just disable it
129 s.xdsServers[id].Close()