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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package agent
import (
"fmt"
"strconv"
"github.com/gin-gonic/gin"
"github.com/iotbzh/xds-agent/lib/xdsconfig"
)
const apiBaseUrl = "/api/v1"
// APIService .
type APIService struct {
*Context
apiRouter *gin.RouterGroup
serverIndex int
}
// NewAPIV1 creates a new instance of API service
func NewAPIV1(ctx *Context) *APIService {
s := &APIService{
Context: ctx,
apiRouter: ctx.webServer.router.Group(apiBaseUrl),
serverIndex: 0,
}
s.apiRouter.GET("/version", s.getVersion)
s.apiRouter.GET("/config", s.getConfig)
s.apiRouter.POST("/config", s.setConfig)
s.apiRouter.GET("/browse", s.browseFS)
s.apiRouter.GET("/projects", s.getProjects)
s.apiRouter.GET("/project/:id", s.getProject)
s.apiRouter.POST("/project", s.addProject)
s.apiRouter.POST("/project/sync/:id", s.syncProject)
s.apiRouter.DELETE("/project/:id", s.delProject)
s.apiRouter.POST("/exec", s.execCmd)
s.apiRouter.POST("/exec/:id", s.execCmd)
s.apiRouter.POST("/signal", s.execSignalCmd)
s.apiRouter.GET("/events", s.eventsList)
s.apiRouter.POST("/events/register", s.eventsRegister)
s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
return s
}
// Stop Used to stop/close created services
func (s *APIService) Stop() {
for _, svr := range s.xdsServers {
svr.Close()
}
}
// AddXdsServer Add a new XDS Server to the list of a server
func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
var svr *XdsServer
var exist, tempoID bool
tempoID = false
// First check if not already exist and update it
if svr, exist = s.xdsServers[cfg.ID]; exist {
// Update: Found, so just update some settings
svr.ConnRetry = cfg.ConnRetry
tempoID = svr.IsTempoID()
if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
return svr, nil
}
// URL differ or not connected, so need to reconnect
svr.BaseURL = cfg.URL
} else {
// Create a new server object
if cfg.APIBaseURL == "" {
cfg.APIBaseURL = apiBaseUrl
}
if cfg.APIPartialURL == "" {
cfg.APIPartialURL = "/server/" + strconv.Itoa(s.serverIndex)
s.serverIndex = s.serverIndex + 1
}
// Create a new XDS Server
svr = NewXdsServer(s.Context, cfg)
svr.SetLoggerOutput(s.Config.LogVerboseOut)
// Passthrough routes (handle by XDS Server)
grp := s.apiRouter.Group(svr.PartialURL)
svr.SetAPIRouterGroup(grp)
svr.PassthroughGet("/sdks")
svr.PassthroughGet("/sdk/:id")
}
// Established connection
err := svr.Connect()
// Delete temporary ID with it has been replaced by right Server ID
if tempoID && !svr.IsTempoID() {
delete(s.xdsServers, cfg.ID)
}
// Add to map
s.xdsServers[svr.ID] = svr
// Load projects
if err == nil && svr.Connected {
err = s.projects.Init(svr)
}
return svr, err
}
// DelXdsServer Delete an XDS Server from the list of a server
func (s *APIService) DelXdsServer(id string) error {
if _, exist := s.xdsServers[id]; !exist {
return fmt.Errorf("Unknown Server ID %s", id)
}
// Don't really delete, just disable it
s.xdsServers[id].Close()
return nil
}
|