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("/project/:id", s.getProject)
37 s.apiRouter.POST("/project", s.addProject)
38 s.apiRouter.POST("/project/sync/:id", s.syncProject)
39 s.apiRouter.DELETE("/project/:id", s.delProject)
41 s.apiRouter.POST("/exec", s.execCmd)
42 s.apiRouter.POST("/exec/:id", s.execCmd)
43 s.apiRouter.POST("/signal", s.execSignalCmd)
45 s.apiRouter.GET("/events", s.eventsList)
46 s.apiRouter.POST("/events/register", s.eventsRegister)
47 s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
52 // Stop Used to stop/close created services
53 func (s *APIService) Stop() {
54 for _, svr := range s.xdsServers {
59 // AddXdsServer Add a new XDS Server to the list of a server
60 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
62 var exist, tempoID bool
65 // First check if not already exist and update it
66 if svr, exist = s.xdsServers[cfg.ID]; exist {
68 // Update: Found, so just update some settings
69 svr.ConnRetry = cfg.ConnRetry
71 tempoID = svr.IsTempoID()
72 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
76 // URL differ or not connected, so need to reconnect
81 // Create a new server object
82 if cfg.APIBaseURL == "" {
83 cfg.APIBaseURL = apiBaseUrl
85 if cfg.APIPartialURL == "" {
86 cfg.APIPartialURL = "/server/" + strconv.Itoa(s.serverIndex)
87 s.serverIndex = s.serverIndex + 1
90 // Create a new XDS Server
91 svr = NewXdsServer(s.Context, cfg)
93 svr.SetLoggerOutput(s.Config.LogVerboseOut)
95 // Passthrough routes (handle by XDS Server)
96 grp := s.apiRouter.Group(svr.PartialURL)
97 svr.SetAPIRouterGroup(grp)
98 svr.PassthroughGet("/sdks")
99 svr.PassthroughGet("/sdk/:id")
102 // Established connection
105 // Delete temporary ID with it has been replaced by right Server ID
106 if tempoID && !svr.IsTempoID() {
107 delete(s.xdsServers, cfg.ID)
111 s.xdsServers[svr.ID] = svr
114 if err == nil && svr.Connected {
115 err = s.projects.Init(svr)
121 // DelXdsServer Delete an XDS Server from the list of a server
122 func (s *APIService) DelXdsServer(id string) error {
123 if _, exist := s.xdsServers[id]; !exist {
124 return fmt.Errorf("Unknown Server ID %s", id)
126 // Don't really delete, just disable it
127 s.xdsServers[id].Close()