2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2018-08-16 05:17:57 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2018-08-16 05:17:57 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
2018-08-16 05:17:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
master := gobot.NewMaster()
|
|
|
|
|
|
|
|
a := api.NewAPI(master)
|
2018-08-16 17:22:22 +08:00
|
|
|
|
|
|
|
// creates routes/handlers for the custom API
|
2018-08-16 05:17:57 +08:00
|
|
|
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))
|
|
|
|
})
|
2018-08-16 17:22:22 +08:00
|
|
|
|
2018-08-17 16:32:06 +08:00
|
|
|
// starts the API without the default C2PIO API and Robeaux web interface.
|
|
|
|
a.StartWithoutDefaults()
|
2018-08-16 05:17:57 +08:00
|
|
|
|
|
|
|
master.AddRobot(gobot.NewRobot("hello"))
|
|
|
|
|
|
|
|
master.Start()
|
|
|
|
}
|