api: further improvement of the modular API changes

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2018-08-16 11:22:22 +02:00
parent 1e581364ed
commit 6eec38c652
3 changed files with 18 additions and 6 deletions

View File

@ -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)
@ -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"
@ -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)
})

View File

@ -16,6 +16,8 @@ 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"))
})
@ -23,6 +25,8 @@ func main() {
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"))

View File

@ -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"))
@ -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()
}