2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
24 "github.com/gin-gonic/gin"
25 "github.com/iotbzh/xds-agent/lib/xdsconfig"
28 const apiBaseURL = "/api/v1"
31 type APIService struct {
33 apiRouter *gin.RouterGroup
37 // NewAPIV1 creates a new instance of API service
38 func NewAPIV1(ctx *Context) *APIService {
41 apiRouter: ctx.webServer.router.Group(apiBaseURL),
45 s.apiRouter.GET("/version", s.getVersion)
47 s.apiRouter.GET("/config", s.getConfig)
48 s.apiRouter.POST("/config", s.setConfig)
50 s.apiRouter.GET("/browse", s.browseFS)
52 s.apiRouter.GET("/projects", s.getProjects)
53 s.apiRouter.GET("/projects/:id", s.getProject)
54 s.apiRouter.PUT("/projects/:id", s.updateProject)
55 s.apiRouter.POST("/projects", s.addProject)
56 s.apiRouter.POST("/projects/sync/:id", s.syncProject)
57 s.apiRouter.DELETE("/projects/:id", s.delProject)
59 s.apiRouter.POST("/exec", s.execCmd)
60 s.apiRouter.POST("/exec/:id", s.execCmd)
61 s.apiRouter.POST("/signal", s.execSignalCmd)
63 s.apiRouter.GET("/events", s.eventsList)
64 s.apiRouter.POST("/events/register", s.eventsRegister)
65 s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
70 // Stop Used to stop/close created services
71 func (s *APIService) Stop() {
72 for _, svr := range s.xdsServers {
77 // AddXdsServer Add a new XDS Server to the list of a server
78 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
80 var exist, tempoID bool
83 // First check if not already exist and update it
84 if svr, exist = s.xdsServers[cfg.ID]; exist {
86 // Update: Found, so just update some settings
87 svr.ConnRetry = cfg.ConnRetry
89 tempoID = svr.IsTempoID()
90 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
94 // URL differ or not connected, so need to reconnect
99 // Create a new server object
100 if cfg.APIBaseURL == "" {
101 cfg.APIBaseURL = apiBaseURL
103 if cfg.APIPartialURL == "" {
104 cfg.APIPartialURL = "/servers/" + strconv.Itoa(s.serverIndex)
105 s.serverIndex = s.serverIndex + 1
108 // Create a new XDS Server
109 svr = NewXdsServer(s.Context, cfg)
111 svr.SetLoggerOutput(s.Config.LogVerboseOut)
113 // Passthrough routes (handle by XDS Server)
114 grp := s.apiRouter.Group(svr.PartialURL)
115 svr.SetAPIRouterGroup(grp)
117 // Declare passthrough routes
118 s.sdksPassthroughInit(svr)
121 // Established connection
124 // Delete temporary ID with it has been replaced by right Server ID
125 if tempoID && !svr.IsTempoID() {
126 delete(s.xdsServers, cfg.ID)
130 s.xdsServers[svr.ID] = svr
133 if err == nil && svr.Connected {
134 err = s.projects.Init(svr)
140 // DelXdsServer Delete an XDS Server from the list of a server
141 func (s *APIService) DelXdsServer(id string) error {
142 if _, exist := s.xdsServers[id]; !exist {
143 return fmt.Errorf("Unknown Server ID %s", id)
145 // Don't really delete, just disable it
146 s.xdsServers[id].Close()