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) {
|
2024-02-11 22:34:50 +08:00
|
|
|
if _, err := res.Write([]byte("OK")); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2018-08-16 05:17:57 +08:00
|
|
|
})
|
|
|
|
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)
|
2024-02-11 22:34:50 +08:00
|
|
|
if _, err := res.Write([]byte(msg)); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2018-08-16 05:17:57 +08:00
|
|
|
})
|
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"))
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := master.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-16 05:17:57 +08:00
|
|
|
}
|