Skip to content

Commit

Permalink
api: further improvement of the modular API changes
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Aug 16, 2018
1 parent 1e58136 commit 6eec38c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 8 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request)) {
a.handlers = append(a.handlers, f)
}

// Start initializes the api by setting up c3pio routes and robeaux
// Start initializes the api by setting up Robeaux web interface.
func (a *API) Start() {
a.AddC3PIORoutes()
a.AddRobeauxRoutes()

a.start(a)
Expand All @@ -114,6 +113,9 @@ func (a *API) StartRaw() {
}

// AddC3PIORoutes adds all of the standard C3PIO routes to the API.
// For more information, please see:
// http://cppp.io/
//
func (a *API) AddC3PIORoutes() {
mcpCommandRoute := "/api/commands/:command"
robotDeviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
Expand All @@ -139,7 +141,11 @@ func (a *API) AddC3PIORoutes() {
}

// AddRobeauxRoutes adds all of the robeaux web interface routes to the API.
// The Robeaux web interface requires the C3PIO API, so it is also
// activated when you call this method.
func (a *API) AddRobeauxRoutes() {
a.AddC3PIORoutes()

a.Get("/", func(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/index.html", http.StatusMovedPermanently)
})
Expand Down
4 changes: 4 additions & 0 deletions examples/hello_api_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ func main() {
master := gobot.NewMaster()

a := api.NewAPI(master)

// creates routes/handlers for the custom API
a.Get("/", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("OK"))
})
a.Get("/api/hello", func(res http.ResponseWriter, req *http.Request) {
msg := fmt.Sprintf("This command is attached to the robot %v", master.Robot("hello").Name)
res.Write([]byte(msg))
})

// starts the API without the "standard" C2PIO API or Robeaux web interface.
a.StartRaw()

master.AddRobot(gobot.NewRobot("hello"))
Expand Down
10 changes: 6 additions & 4 deletions examples/hello_api_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ func main() {
master := gobot.NewMaster()

a := api.NewAPI(master)

// add the standard C3PIO API routes manually.
a.AddC3PIORoutes()
a.AddRobeauxRoutes()

// starts the API without adding the Robeaux web interface. However, since the C3PIO API was
// already added manually above, that will be available.
a.StartRaw()

hello := master.AddRobot(gobot.NewRobot("hello"))
Expand All @@ -49,13 +53,11 @@ func main() {

// create the mjpeg stream
stream = mjpeg.NewStream()
http.Handle("/video", stream)

// start capturing
go mjpegCapture()

// handle video stream
http.Handle("/video", stream)

master.Start()
}

Expand Down

0 comments on commit 6eec38c

Please sign in to comment.